Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.4 KB
Raw Blame
import { TriggerStrategy, createTrigger } from '@activepieces/pieces-framework';
import {
  HttpRequest,
  HttpMethod,
  AuthenticationType,
  httpClient,
} from '@activepieces/pieces-common';
import { calendlyCommon, CalendlyWebhookInformation } from '../common';
import { calendlyAuth } from '../auth';
import { inviteeTriggerOutputSchema } from '../output-schemas';

const triggerNameInStore = 'calendly_invitee_created_trigger';

export const calendlyInviteeCreated = createTrigger({
  auth: calendlyAuth,
  name: 'invitee_created',
  classification: 'READ',
  displayName: 'Event Scheduled',
  description: 'Triggers when someone books a Calendly event.',
  aiMetadata: {
    description:
      'Fires when an invitee schedules a new Calendly event (a booking is created). Emits the new event including the invitee details (name, email), the event URI, scheduling timestamps, and the event status set to "active".',
  },
  props: {
    scope: calendlyCommon.scope,
  },
  sampleData: {
    created_at: '2023-01-29T13:50:13.000000Z',
    created_by: 'https://api.calendly.com/users/AAAAAAAAAAAA',
    payload: {
      cancel_url: 'https://calendly.com/cancellations/AAAAAAAAAAA',
      created_at: '2023-01-29T13:50:13.072950Z',
      email: '[email protected]',
      event: 'https://api.calendly.com/scheduled_events/AAAAAAAAAAAA',
      first_name: null,
      last_name: null,
      name: 'abdul',
      new_invitee: null,
      no_show: null,
      old_invitee: null,
      payment: null,
      questions_and_answers: [],
      reconfirmation: null,
      reschedule_url: 'https://calendly.com/reschedulings/AAAAAAAAAAAA',
      rescheduled: false,
      routing_form_submission: null,
      status: 'active',
      text_reminder_number: null,
      timezone: 'Asia/Baghdad',
      updated_at: '2023-01-29T13:50:13.072950Z',
      uri: 'https://api.calendly.com/scheduled_events/AAAAAAAAAAAaA/invitees/AAAAAAAAAAAA',
    },
  },
  outputSchema: inviteeTriggerOutputSchema,
  type: TriggerStrategy.WEBHOOK,
  async onEnable(context) {
    const calendlyUser = await calendlyCommon.getUser(context.auth.secret_text);
    const request: HttpRequest = {
      method: HttpMethod.POST,
      url: `${calendlyCommon.baseUrl}/webhook_subscriptions`,
      body: {
        url: context.webhookUrl,
        organization: calendlyUser.current_organization,
        user: calendlyUser.uri,
        scope: context.propsValue['scope'],
        events: ['invitee.created'],
      },
      authentication: {
        token: context.auth.secret_text,
        type: AuthenticationType.BEARER_TOKEN,
      },
      queryParams: {},
    };
    const { body } = await httpClient.sendRequest<{
      resource: { uri: string };
    }>(request);
    await context.store?.put<CalendlyWebhookInformation>(triggerNameInStore, {
      webhookId: calendlyCommon.UuidFromUri(body.resource.uri)!,
    });
  },
  async onDisable(context) {
    const response = await context.store?.get<CalendlyWebhookInformation>(
      triggerNameInStore
    );
    if (response !== null && response !== undefined) {
      const request: HttpRequest = {
        method: HttpMethod.DELETE,
        url: `${calendlyCommon.baseUrl}/webhook_subscriptions/${response.webhookId}`,
        authentication: {
          token: context.auth.secret_text,
          type: AuthenticationType.BEARER_TOKEN,
        },
      };
      await httpClient.sendRequest(request);
    }
  },
  async run(context) {
    return [context.payload.body];
  },
});