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.7 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { openPhoneAuth } from '../auth';
import {
  openPhoneCommon,
  OpenPhoneMessageWebhookResponse,
  CreateOpenPhoneMessageWebhookRequest,
} from '../common';
import { phoneNumberDropdown } from '../common/props';

export const outgoingMessageDelivered = createTrigger({
  auth: openPhoneAuth,
  name: 'outgoing_message_delivered',
  classification: 'READ',
  displayName: 'Outgoing Message Delivered',
  description:
    'Fires when an outbound message is delivered successfully. Useful for message confirmation workflows.',
  aiMetadata: {
    description: 'Fires when an outbound SMS/MMS sent from an OpenPhone number is successfully delivered to the recipient, delivering the message details. Use to confirm delivery or trigger follow-up steps. Can be scoped to a specific OpenPhone number or all numbers.',
  },
  props: {
    phoneNumbers: phoneNumberDropdown,
  },
  sampleData: {
    event: 'message.delivered',
    data: {
      id: 'AC123abc',
      to: ['+15555555555'],
      from: '+15551234567',
      text: 'Hello, this is a test message',
      phoneNumberId: 'PN123abc',
      direction: 'outgoing',
      userId: 'US123abc',
      status: 'delivered',
      createdAt: '2022-01-01T00:00:00Z',
      updatedAt: '2022-01-01T00:00:30Z',
    },
  },
  type: TriggerStrategy.WEBHOOK,
  async onEnable(context) {
    const { phoneNumbers } = context.propsValue;
    const auth = context.auth;

    const webhookRequest: CreateOpenPhoneMessageWebhookRequest = {
      url: context.webhookUrl,
      events: ['message.delivered'],
      resourceIds: phoneNumbers ? [phoneNumbers as string] : ['*'],
      label: 'Activepieces Outgoing Message Delivered Trigger',
      status: 'enabled',
    };

    try {
      const response: OpenPhoneMessageWebhookResponse =
        await openPhoneCommon.makeRequest<OpenPhoneMessageWebhookResponse>(
          HttpMethod.POST,
          '/v1/webhooks/messages',
          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/messages/${webhookId}`,
        auth
      );
    } catch (error) {
      throw new Error(`Failed to delete webhook: ${error}`);
    }
  },
  async run(context) {
    return [context.payload.body];
  },
});