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 { createTrigger, Property, TriggerStrategy } from '@activepieces/pieces-framework';
import { famulorAuth } from '../..';
import { famulorCommon } from '../common';
import type { ConversationEndedWebhookPayload } from '../common/types';

const assistantDropdownForConversationWebhook = () =>
  Property.Dropdown({
    auth: famulorAuth,
    displayName: 'Assistant',
    description:
      'Select the assistant whose chat conversations (WhatsApp or web widget) should send this webhook when a conversation ends',
    required: true,
    refreshers: ['auth'],
    options: async ({ auth }) => {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'Please authenticate first',
          options: [],
        };
      }

      try {
        const allAssistants = await famulorCommon.fetchAllAssistantPages({
          auth: auth.secret_text,
        });

        if (allAssistants.length === 0) {
          return {
            disabled: true,
            placeholder: 'No assistants found. Create one first.',
            options: [],
          };
        }

        return {
          options: allAssistants.map((assistant: any) => ({
            label: `${assistant.name} (${assistant.type} - ${assistant.status})`,
            value: assistant.id,
          })),
        };
      } catch {
        return {
          disabled: true,
          placeholder: 'Failed to fetch assistants',
          options: [],
        };
      }
    },
  });

export const conversationEnded = createTrigger({
  auth: famulorAuth,
  name: 'conversationEnded',
  classification: 'READ',
  displayName: 'Conversation Ended',
  description: 'Triggers when a chat conversation ends.',
  aiMetadata: {
    description: 'Fires when a Famulor chat conversation (WhatsApp or web widget) for the selected assistant ends, delivering the full result via webhook. The payload includes the conversation and assistant IDs, channel type, message count, status, AI-extracted variables and summary, input variables, the raw and formatted transcript, customer name/phone, and start and end timestamps. Use to act on the outcome of a completed text conversation.',
  },
  props: {
    assistant_id: assistantDropdownForConversationWebhook(),
  },
  sampleData: {
    conversation_id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
    assistant_id: 'f9e8d7c6-b5a4-3210-fedc-ba9876543210',
    type: 'widget',
    message_count: 8,
    status: 'ended',
    extracted_variables: {
      status: true,
      summary:
        'Customer asked about pricing plans and was interested in the Pro plan',
    },
    input_variables: {
      name: 'John Doe',
      email: '[email protected]',
    },
    transcript: [
      {
        role: 'assistant',
        content: 'Hi! How can I help you today?',
      },
      {
        role: 'user',
        content: 'I have a question about your service.',
      },
    ],
    formatted_transcript:
      'AI: Hi! How can I help you today?\nCustomer: I have a question about your service.',
    customer_phone: null,
    customer_name: 'John Doe',
    sender: null,
    created_at: '2026-02-23T09:30:00+01:00',
    ended_at: '2026-02-23T10:00:00+01:00',
  } satisfies ConversationEndedWebhookPayload,
  type: TriggerStrategy.WEBHOOK,
  async onEnable(context) {
    await famulorCommon.enableConversationEndedWebhook({
      auth: context.auth.secret_text,
      assistant_id: context.propsValue.assistant_id as number,
      webhook_url: context.webhookUrl,
    });
  },
  async onDisable(context) {
    await famulorCommon.disableConversationEndedWebhook({
      auth: context.auth.secret_text,
      assistant_id: context.propsValue.assistant_id as number,
    });
  },
  async run(context) {
    return [context.payload.body];
  },
});