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

4.0 KB
Raw Blame
import { createTrigger, Property, TriggerStrategy } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { autocallsAuth } from '../..';
import { baseApiUrl } from '../..';

export const phoneCallEnded = createTrigger({
    auth:autocallsAuth,
    name: 'phoneCallEnded',
    classification: 'READ',
    displayName: 'Phone Call Ended',
    description: 'Triggers when a phone call ends, with extracted variables.',
    aiMetadata: {
      description: 'Fires via webhook when a call handled by the selected Autocalls assistant ends, delivering the call outcome including duration, status, the input and extracted variables, and the full transcript.',
    },
    props: {
        assistant: Property.Dropdown({
            auth: autocallsAuth,
            displayName: 'Assistant',
            description: 'Select an assistant',
            required: true,
            refreshers: ['auth'],
            refreshOnSearch: false,
            options: async ({ auth }) => {
                const res = await httpClient.sendRequest({
                    method: HttpMethod.GET,
                    url: baseApiUrl + 'api/user/assistants',
                    headers: {
                        Authorization: "Bearer " + auth?.secret_text,
                    },
                });

                if (res.status !== 200) {
                    return {
                        disabled: true,
                        placeholder: 'Error fetching assistants',
                        options: [],
                    };
                } else if (res.body.length === 0) {
                    return {
                        disabled: true,
                        placeholder: 'No assistants found. Create one first.',
                        options: [],
                    };
                }

                return {
                    options: res.body.map((assistant: {id:number,name:string}) => ({
                        value: assistant.id,
                        label: assistant.name,
                    })),
                };
            }
        }),
    },
    sampleData: {
        customer_phone: '+16380991171',
        assistant_phone: '+16380991171',
        duration: 120,
        status: 'completed',
        extracted_variables: {
            status: false,
            summary: 'Call ended without clear objective being met.'
        },
        input_variables: {
            customer_name: 'John'
        },
        transcript: [
            {
                sender: 'bot',
                timestamp: 1722347063.574402,
                text: 'Hi! How are you, John?'
            },
            {
                sender: 'human',
                timestamp: 1722347068.886166,
                text: 'Im fine. How about you?'
            },
            {
                sender: 'bot',
                timestamp: 1722347069.76683,
                text: 'Im doing well, thank you for asking.'
            },
            {
                sender: 'bot',
                timestamp: 1722347071.577889,
                text: 'How can I assist you today?'
            },
        ]
    },
    type: TriggerStrategy.WEBHOOK,
    async onEnable(context) {
        await httpClient.sendRequest({
            method: HttpMethod.POST,
            url: baseApiUrl + 'api/user/assistants/enable-webhook',
            body: {
                assistant_id: context.propsValue['assistant'],
                webhook_url: context.webhookUrl,
            },
            headers: {
                Authorization: "Bearer " + context.auth.secret_text,
            },
        });
    },
    async onDisable(context) {
        await httpClient.sendRequest({
            method: HttpMethod.POST,
            url: baseApiUrl + 'api/user/assistants/disable-webhook',
            body: {
                assistant_id: context.propsValue['assistant'],
            },
            headers: {
                Authorization: "Bearer " + context.auth.secret_text,
            },
        });
    },
    async run(context) {
        return [context.payload.body]
    }
})