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.5 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { aircallAuth } from '../common/auth';
import { makeRequest } from '../common/client';
import { HttpMethod } from '@activepieces/pieces-common';

const TRIGGER_KEY = 'trigger_new-contact'

export const newContact = createTrigger({
  auth: aircallAuth,
  name: 'newContact',
  classification: 'READ',
  displayName: 'New Contact',
  description: 'Triggers when a new contact is created.',
  aiMetadata: {
    description: 'Fires when a new contact is created in Aircall (the contact.created webhook event), emitting the newly created contact with its name, company, phone numbers, and emails.',
  },
  props: {},
  sampleData:  {
      id: 456,
      first_name: 'John',
      last_name: 'Doe',
      company_name: 'Acme Corp',
      information: 'Important client contact',
      is_shared: true,
      created_at: '2023-07-31T10:30:00Z',
      updated_at: '2023-07-31T10:30:00Z',
      phone_numbers: [
        {
          id: 789,
          label: 'Work',
          value: '+1234567890'
        },
        {
          id: 790,
          label: 'Mobile',
          value: '+1234567891'
        }
      ],
      emails: [
        {
          id: 123,
          label: 'Office',
          value: '[email protected]'
        }
      ]
  },
  type: TriggerStrategy.WEBHOOK,

  async onEnable(context) {
    const webhookUrl = context.webhookUrl;
   

    const response = await makeRequest(
      context.auth,
      HttpMethod.POST,
      '/webhooks',
      {
        url: webhookUrl,
        events: ['contact.created'],
      }
    );

  const { webhook } = response as { webhook: { webhook_id: string } };

    await context.store.put<string>(TRIGGER_KEY, webhook.webhook_id);  },

  async onDisable(context) {
    const webhookId = await context.store.get<string>(TRIGGER_KEY);
    

    if (webhookId) {
      await makeRequest(
        context.auth,
        HttpMethod.DELETE,
        `/webhooks/${webhookId}`
      );
    }
  },

  async run(context) {
 const payload = context.payload.body as {
      event: string;
      data: Record<string, any>;
    };    

    if (payload.event === 'contact.created') {
      return [payload.data];
    }
    
    return [];
  },
    async test(context) {
    const response = await makeRequest(
      context.auth,
      HttpMethod.GET,
      '/contacts?order=desc&per_page=10'
    );

    const { contacts } = response as { contacts: { id: number }[] };

    return contacts;
  },
});