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

1.8 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { timelinesAiAuth, timelinesAiCommon } from '../common';
import { WebhookInformation } from '../common/types';

export const newWhatsappAccount = createTrigger({
  auth: timelinesAiAuth,
  name: 'newWhatsappAccount',
  classification: 'READ',
  displayName: 'New WhatsApp Account',
  description: 'Fires when a new WhatsApp account is added/registered.',
  aiMetadata: {
    description: 'Fires when a WhatsApp account is connected to TimelinesAI. Represents a newly registered/connected account, carrying its id, phone number, status, and owner details.',
  },
  props: {},
  sampleData: {
    event_type: 'whatsapp:account:connected',
    whatsapp_account: {
      id: '[email protected]',
      phone: '+123456789',
      connected_on: '2024-01-08 10:35:18 +0200',
      disconnected_on: '2024-01-08 10:35:18 +0200',
      status: 'active',
      account_name: 'Smith',
      owner_name: 'John Smith',
      owner_email: '[email protected]',
    },
  },
  type: TriggerStrategy.WEBHOOK,
  async onEnable(context) {
    const response = await timelinesAiCommon.createWebhook({
      apiKey: context.auth,
      event_type: 'whatsapp:account:connected',
      url: context.webhookUrl,
      enabled: true,
    });
    await context.store.put<WebhookInformation>('_new_whatsapp_account', {
      webhook_id: response.data.id,
    });
  },
  async onDisable(context) {
    const webhookInfo = await context.store.get<WebhookInformation>(
      '_new_whatsapp_account'
    );
    const webhook_id = webhookInfo?.webhook_id;
    if (webhook_id) {
      await timelinesAiCommon.deleteWebhook({
        apiKey: context.auth,
        webhook_id: webhook_id,
      });
    }
  },
  async run(context) {
    return [context.payload.body];
  },
});