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.6 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { timelinesAiAuth, timelinesAiCommon } from '../common';
import { WebhookInformation } from '../common/types';

export const newIncomingChat = createTrigger({
  auth: timelinesAiAuth,
  name: 'newIncomingChat',
  classification: 'READ',
  displayName: 'New Incoming Chat',
  description: 'Fires when a new incoming chat (i.e. from a user) is created.',
  aiMetadata: {
    description: 'Fires when a new incoming TimelinesAI WhatsApp chat is created, i.e. a contact starts a conversation with the connected account. Represents the start of an inbound conversation, carrying the chat and WhatsApp account details.',
  },
  props: {},
  sampleData: {
    event_type: 'chat:new',
    chat: {
      id: 123456,
      name: 'John Smith',
      phone: '+123456789',
      jid: '[email protected]',
      is_group: false,
      closed: false,
      read: false,
      labels: ['string'],
      unattended_customer: false,
      chatgpt_autoresponse_enabled: false,
      whatsapp_account_id: '[email protected]',
      chat_url: 'https://app.timelines.ai/chat/123456/messages/',
      created_timestamp: '2024-01-08 10:35:18 +0200',
      last_message_uid: 'afa9d4dd-978d-4a14-aa1b-bd65c272e645',
      last_message_timestamp: '2024-01-08 10:35:18 +0200',
      responsible_name: 'Agent Brown',
      responsible_email: '[email protected]',
      previous_responsible_name: 'Agent Jones',
      previous_responsible_email: '[email protected]',
    },
    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: 'Some account',
      owner_name: 'Agent Smith',
      owner_email: '[email protected]',
    },
  },
  type: TriggerStrategy.WEBHOOK,
  async onEnable(context) {
    const response = await timelinesAiCommon.createWebhook({
      apiKey: context.auth,
      event_type: 'chat:incoming:new',
      url: context.webhookUrl,
      enabled: true,
    });
    await context.store.put<WebhookInformation>('_new_incoming_chat', {
      webhook_id: response.data.id,
    });
  },
  async onDisable(context) {
    const webhookInfo = await context.store.get<WebhookInformation>(
      '_new_incoming_chat'
    );
    const webhook_id = webhookInfo?.webhook_id;
    if (webhook_id) {
      await timelinesAiCommon.deleteWebhook({
        apiKey: context.auth,
        webhook_id: webhook_id as number,
      });
    }
  },
  async run(context) {
    return [context.payload.body];
  },
});