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

2.8 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { openPhoneAuth } from '../auth';
import {
  openPhoneCommon,
  OpenPhoneWebhookResponse,
  CreateOpenPhoneWebhookRequest,
} from '../common';
import { phoneNumberDropdown } from '../common/props';

export const outgoingCallCompleted = createTrigger({
  auth: openPhoneAuth,
  name: 'outgoing_call_completed',
  classification: 'READ',
  displayName: 'Outgoing Call Completed',
  description: 'Fires when an outbound call ends. Useful for call logging.',
  aiMetadata: {
    description: 'Fires when an outbound (workspace-initiated) call ends, delivering the completed call details. Incoming calls are filtered out, so this only represents calls placed from the OpenPhone number. Use for call logging or post-call automation. Can be scoped to a specific OpenPhone number or all numbers.',
  },
  props: {
    phoneNumbers: phoneNumberDropdown,
  },
  sampleData: {
    event: 'call.completed',
    data: {
      id: 'AC123abc',
      phoneNumberId: 'PN123abc',
      userId: 'US123abc',
      direction: 'outgoing',
      status: 'completed',
      participants: ['+15555555555'],
      duration: 120,
      initiatedBy: 'US123abc',
      answeredAt: '2022-01-01T00:00:30Z',
      completedAt: '2022-01-01T00:02:30Z',
      createdAt: '2022-01-01T00:00:00Z',
      updatedAt: '2022-01-01T00:02:30Z',
    },
  },
  type: TriggerStrategy.WEBHOOK,
  async onEnable(context) {
    const { phoneNumbers } = context.propsValue;
    const auth = context.auth;

    const webhookRequest: CreateOpenPhoneWebhookRequest = {
      url: context.webhookUrl,
      events: ['call.completed'],
      resourceIds: phoneNumbers ? [phoneNumbers as string] : ['*'],
      label: 'Activepieces Outgoing Call Completed Trigger',
      status: 'enabled',
    };

    try {
      const response: OpenPhoneWebhookResponse =
        await openPhoneCommon.makeRequest<OpenPhoneWebhookResponse>(
          HttpMethod.POST,
          '/v1/webhooks/calls',
          auth,
          webhookRequest
        );

      await context.store.put('webhookId', response.data.id);
    } catch (error) {
      throw new Error(`Failed to create webhook: ${error}`);
    }
  },
  async onDisable(context) {
    const auth = context.auth;
    const webhookId = await context.store.get('webhookId');

    if (!webhookId) {
      return;
    }

    try {
      await openPhoneCommon.makeRequest(
        HttpMethod.DELETE,
        `/v1/webhooks/calls/${webhookId}`,
        auth
      );
    } catch (error) {
      throw new Error(`Failed to delete webhook: ${error}`);
    }
  },
  async run(context) {
    const payload = context.payload.body as any;
    if (payload?.data && payload.data.direction === 'outgoing') {
      return [payload];
    }
    return [];
  },
});