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.5 KB
Raw Blame
import {
  createTrigger,
  TriggerStrategy,
  AppConnectionValueForAuthProperty,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import dayjs from 'dayjs';
import { letsCalendarAuth } from '../common/auth';
import { getAccessToken, makeRequest } from '../common/client';
import { HttpMethod } from '@activepieces/pieces-common';

interface Campaign {
  campaign_id: string;
  title: string;
  subject: string;
  start_datetime: string;
  end_datetime: string;
  timezone: string;
  created_on: string;
}

interface CampaignsResponse {
  campaigns: {
    data: Campaign[];
  };
}

const polling: Polling<
  AppConnectionValueForAuthProperty<typeof letsCalendarAuth>,
  Record<string, never>
> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth }) => {
    try {
      const accessToken = await getAccessToken(
        auth.props.client_key,
        auth.props.secret_key
      );

      const response = (await makeRequest(
        accessToken,
        HttpMethod.GET,
        '/campaigns?page=1'
      )) as CampaignsResponse;

      const campaigns = response.campaigns.data || [];

      return campaigns.map((campaign) => ({
        epochMilliSeconds: dayjs(campaign.created_on).valueOf(),
        data: campaign,
      }));
    } catch (error) {
      return [];
    }
  },
};

export const newCampaign = createTrigger({
  auth: letsCalendarAuth,
  name: 'newCampaign',
  displayName: 'New Campaign',
  description: 'Trigger when a new campaign is created',
  aiMetadata: {
    description: 'Fires when a new campaign is created in the connected Let\'s Calendar account, polling the campaigns list periodically and emitting each newly seen campaign by its creation time. Each event represents one newly created campaign with its title, subject, schedule, and timezone.',
  },
  props: {},
  sampleData: {
    campaign_id: 'Q2Q4g1rr333lcxZXkzW4',
    title: 'APIs Campaign',
    subject: 'APIs Campaign',
    start_datetime: '2026-04-23 13:00:00',
    end_datetime: '2026-04-23 14:00:00',
    timezone: 'Asia/Kolkata',
    created_on: '2025-04-24 10:47:22',
  },
  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);
  },
});