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.7 KB
Raw Blame
import {
  AppConnectionValueForAuthProperty,
  createTrigger,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import {
  AuthenticationType,
  DedupeStrategy,
  httpClient,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import { twilioAuth } from '../..';

// The full structure of an IncomingPhoneNumber object from the Twilio API
interface TwilioPhoneNumber {
  sid: string;
  account_sid: string;
  friendly_name: string;
  phone_number: string;
  voice_url: string;
  voice_method: string;
  sms_url: string;
  sms_method: string;
  date_created: string;
  date_updated: string;
  capabilities: {
    voice: boolean;
    sms: boolean;
    mms: boolean;
  };
  uri: string;
}

interface IncomingPhoneNumbersResponse {
  incoming_phone_numbers: TwilioPhoneNumber[];
  next_page_uri:string
}

const polling: Polling<
  AppConnectionValueForAuthProperty<typeof twilioAuth>,
  Record<string, unknown>
> = {
  strategy: DedupeStrategy.TIMEBASED,
  async items({ auth, lastFetchEpochMS }) {
    const isTest = lastFetchEpochMS === 0;
    const account_sid = auth.username;
    const auth_token = auth.password;

    let currentUri:
      | string
      | null = `/2010-04-01/Accounts/${account_sid}/IncomingPhoneNumbers.json?PageSize=${
      isTest ? 10 : 1000
    }`;

    const results = [];
    let stop = false;

    do {
      const response: any = await httpClient.sendRequest({
        method: HttpMethod.GET,
        url: `https://api.twilio.com${currentUri}`,
        authentication: {
          type: AuthenticationType.BASIC,
          username: account_sid,
          password: auth_token,
        },
      });

      const payload = response.body as IncomingPhoneNumbersResponse;

      const numbers = payload.incoming_phone_numbers ?? [];

      for (const num of numbers) {
        const ts = new Date(num.date_created).getTime();

        if (isTest || ts > lastFetchEpochMS) {
          results.push(num);
        } else {
          stop = true;
          break;
        }
      }

      if (isTest) break;
      currentUri = payload?.next_page_uri ?? null;
    } while (currentUri && !stop);

    return results.map((num) => {
      return {
        epochMilliSeconds: new Date(num.date_created).getTime(),
        data: num,
      };
    });
  },
};

export const twilioNewPhoneNumber = createTrigger({
  auth: twilioAuth,
  name: 'new_phone_number',
  classification: 'READ',
  displayName: 'New Phone Number',
  description: 'Triggers when you add a new phone number to your account.',
  aiMetadata: {
    description: 'Fires when a new phone number is provisioned on the Twilio account. Each event represents one newly added incoming phone number with its capabilities and configuration.',
  },
  props: {},
  sampleData: {
    sid: 'PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    account_sid: 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    friendly_name: '(555) 123-4567',
    phone_number: '+15551234567',
    voice_url: 'https://demo.twilio.com/welcome/voice/',
    voice_method: 'POST',
    sms_url: 'https://demo.twilio.com/welcome/sms/',
    sms_method: 'POST',
    date_created: 'Thu, 28 Aug 2025 11:44:10 +0000',
    date_updated: 'Thu, 28 Aug 2025 11:44:10 +0000',
    capabilities: {
      voice: true,
      sms: true,
      mms: true,
    },
    uri: '/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/IncomingPhoneNumbers/PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json',
  },
  type: TriggerStrategy.POLLING,

  async onEnable(context) {
    await pollingHelper.onEnable(polling, context);
  },
  async onDisable(context) {
    await pollingHelper.onDisable(polling, context);
  },
  async test(context) {
    return await pollingHelper.test(polling, context);
  },
  async run(context) {
    return await pollingHelper.poll(polling, context);
  },
});