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.4 KB
Raw Blame
import {
  createTrigger,
  Property,
  TriggerStrategy,
  AppConnectionValueForAuthProperty,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import { bufferAuth } from '../common/auth';
import {
  bufferProps,
  bufferQueries,
  BufferPost,
} from '../common/props';

const polling: Polling<
  AppConnectionValueForAuthProperty<typeof bufferAuth>,
  { organizationId: string; channelIds?: string[] }
> = {
  strategy: DedupeStrategy.TIMEBASED,
  async items({ auth, propsValue }) {
    const posts = await bufferQueries.fetchPosts({
      accessToken: auth.secret_text,
      organizationId: propsValue.organizationId,
      channelIds: propsValue.channelIds,
      statusFilter: (status) => {
        const normalized = status?.toLowerCase();
        return normalized !== 'sent' && normalized !== 'error';
      },
    });
    return posts.map((post) => ({
      epochMilliSeconds: post.createdAt
        ? new Date(post.createdAt).getTime()
        : 0,
      data: post,
    }));
  },
};

export const newQueueItem = createTrigger({
  auth: bufferAuth,
  name: 'new_queue_item',
  classification: 'READ',
  displayName: 'New Queue Item',
  description: 'Triggers when a new post is queued in Buffer.',
  aiMetadata: {
    description:
      'Fires when a new not-yet-published post (any status except sent or error, e.g. pending, scheduled, or draft) appears in the selected Buffer organization, optionally limited to specific channels. Represents the queued post.',
  },
  type: TriggerStrategy.POLLING,
  props: {
    organizationId: bufferProps.organizationId(),
    channelIds: Property.MultiSelectDropdown<string, false, typeof bufferAuth>({
      auth: bufferAuth,
      displayName: 'Channels',
      description: 'Limit results to specific channels (optional).',
      required: false,
      refreshers: ['organizationId'],
      options: async ({ auth, organizationId }) => {
        if (!auth || !organizationId) {
          return {
            disabled: true,
            placeholder: 'Select an organization first.',
            options: [],
          };
        }
        try {
          const channels = await bufferQueries.fetchChannels(
            auth.secret_text,
            organizationId as string,
          );
          return {
            disabled: false,
            options: channels.map((channel) => ({
              label: `${channel.name} (${channel.service})`,
              value: channel.id,
            })),
          };
        } catch {
          return {
            disabled: true,
            placeholder: 'Failed to load channels.',
            options: [],
          };
        }
      },
    }),
  },
  sampleData: {
    id: 'post_id_example',
    text: 'Hello from Buffer!',
    status: 'pending',
    createdAt: '2026-05-25T10:00:00.000Z',
    dueAt: '2026-05-25T18:00:00.000Z',
    channelId: 'channel_id',
    channelService: 'facebook',
    channel: { id: 'channel_id', name: 'My Page', service: 'facebook' },
    tags: [],
  } satisfies BufferPost,
  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);
  },
});