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

const polling: Polling<
  AppConnectionValueForAuthProperty<typeof timelinesAiAuth>,
  Record<string, never>
> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth: apiKey, propsValue, lastFetchEpochMS }) => {
    const response = await timelinesAiCommon.getChats({
      apiKey: apiKey,
      closed: true,
    });
    const items = response.data.chats;
    return items.map((item) => ({
      epochMilliSeconds: dayjs(item.last_message_timestamp).valueOf(),
      data: item,
    }));
  },
};

export const chatClosed = createTrigger({
  auth: timelinesAiAuth,
  name: 'chatClosed',
  classification: 'READ',
  displayName: 'Chat Closed',
  description: 'Fires when a chat is closed.',
  aiMetadata: {
    description: 'Fires when a TimelinesAI WhatsApp chat is marked closed (resolved/archived). Polls for chats in the closed state and emits each newly closed conversation.',
  },
  props: {},
  sampleData: {},
  type: TriggerStrategy.POLLING,
  async test(context) {
    return await pollingHelper.test(polling, context);
  },
  async onEnable(context) {
    await pollingHelper.onEnable(polling, context);
  },

  async onDisable(context) {
    await pollingHelper.onDisable(polling, context);
  },

  async run(context) {
    return await pollingHelper.poll(polling, context);
  },
});