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.7 KB
Raw Blame
import { AppConnectionValueForAuthProperty, createTrigger, TriggerStrategy } from "@activepieces/pieces-framework";
import { leexiAuth } from "../common/auth";
import { AuthenticationType, DedupeStrategy, httpClient, HttpMethod, Polling, pollingHelper, QueryParams } from "@activepieces/pieces-common";
import dayjs from 'dayjs';
import { BASE_URL } from "../common/constants";
import { ListCallResponse } from "../common/types";
import { isEmpty } from '@activepieces/pieces-framework';

const polling: Polling<AppConnectionValueForAuthProperty<typeof leexiAuth>, Record<string, never>> = {
    strategy: DedupeStrategy.TIMEBASED,
    async items({ auth, lastFetchEpochMS }) {
        const isTestMode = lastFetchEpochMS === 0;

        let page = 1;
        let hasMore = true;

        const calls = [];

        do {
            const qs: QueryParams = {
                page: page.toString(),
                items: isTestMode ? '5' : '100',
            }

            if (!isTestMode) qs['from'] = dayjs(lastFetchEpochMS).toISOString();

            const response = await httpClient.sendRequest<ListCallResponse>({
                method: HttpMethod.GET,
                url: BASE_URL + '/calls',
                authentication: {
                    type: AuthenticationType.BASIC,
                    username: auth.username,
                    password: auth.password
                }
            });

            const callList = response.body.data ?? [];

            for (const call of callList) {
                calls.push(call)
            }

            if (isTestMode) break;

            page++;
            hasMore = !isEmpty(callList)

        } while (hasMore);


        return calls.map((call) => ({
            epochMilliSeconds: dayjs(call.created_at).valueOf(),
            data: call
        }))
    }
}

export const newCallCreatedTrigger = createTrigger({
    name: 'new-call-created',
    classification: 'READ',
    auth: leexiAuth,
    displayName: 'New Call Created',
    description: 'Triggers when a new call is created.',
    aiMetadata: {
        description: 'Fires when a new call (recording/transcription session) appears in the Leexi account. Polls the calls list on an interval and emits one event per newly created call.',
    },
    type: TriggerStrategy.POLLING,
    props: {},
    async onEnable(context) {
        await pollingHelper.onEnable(polling, context);
    },
    async onDisable(context) {
        await pollingHelper.onDisable(polling, context);
    },
    async test(context) {
        return await pollingHelper.test(polling, context);
    },
    async run(context) {
        return await pollingHelper.poll(polling, context);
    },
    sampleData: undefined
})