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.1 KB
Raw Blame
import { createAction } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { callClickSendApi, clicksendCommon } from '../common';
import { clicksendAuth } from '../..';

function isValidPhone(phone: string) {
  return /^\+?[1-9]\d{1,14}$/.test(phone);
}

export const clicksendFindContactByPhoneAction = createAction({
  auth: clicksendAuth,
  name: 'find_contact_by_phone',
  classification: 'READ',
  description: 'Finds contact by phone number.',
  audience: 'both',
  aiMetadata: {
    description:
      'Searches a specific ClickSend contact list for a contact whose phone number exactly matches the given value, paging through the whole list until a match is found. Choose this to look up or verify a contact by phone before updating or messaging them. Requires a valid phone number and the target list id; read-only and idempotent.',
    idempotent: true,
  },
  displayName: 'Find Contact by Phone',
  props: {
    contact_list_id: clicksendCommon.contact_list_id,
    phone_number: clicksendCommon.phone_number,
  },
  async run(context) {
    const { contact_list_id, phone_number } = context.propsValue;
    if (!isValidPhone(phone_number)) {
      throw new Error('Invalid phone number.');
    }
    const username = context.auth.username;
    const password = context.auth.password;
    let currentPage = 1;
    let hasNext = true;

    do {
      const response = await callClickSendApi<{
        data: {
          next_page_url?: string;
          data: { phone_number: string }[];
        };
      }>({
        method: HttpMethod.GET,
        username,
        password,
        path: `/lists/${contact_list_id}/contacts`,
        query: { page: currentPage.toString(), limit: '100' },
      });

      const items = response.body.data?.data ?? [];

      const matched = items.find((item) => item.phone_number === phone_number);

      if (matched) {
        return {
          found: true,
          data: matched,
        };
      }

      currentPage++;
      hasNext = !!response.body.data?.next_page_url;
    } while (hasNext);

    return {
      found: false,
      data: {},
    };
  },
});