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.7 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { common } from '../common';
import { googlePubsubAuth } from '../..';

export const publishToTopic = createAction({
  name: 'publish_to_topic',
  classification: 'WRITE',
  auth: googlePubsubAuth,
  displayName: 'Publish to topic',
  description: 'Publish message to topic',
  audience: 'both',
  aiMetadata: { description: 'Publishes a message to a Google Cloud Pub/Sub topic, base64-encoding the provided JSON object as the message payload. Use to emit an event or hand data to downstream Pub/Sub subscribers. The topic must already exist (selected from the project), and each call delivers a new message, so it is not idempotent.', idempotent: false },
  props: {
    message: Property.Object({
      displayName: 'Message',
      required: true,
    }),
    topic: Property.Dropdown({
      displayName: 'Topic',
      required: true,
      refreshers: ['auth'], 
      auth: googlePubsubAuth,
      options: async ({ auth }) => {
        if (!auth) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Please authenticate first',
          };
        }
        const json = auth.props.json;
        return common.getTopics(json);
      },
    }),
  },
  async run(context) {
    const client = common.getClient(context.auth.props.json);
    const topic = context.propsValue.topic;

    const url = `https://pubsub.googleapis.com/v1/${topic}:publish`;
    const json = JSON.stringify(context.propsValue.message);
    const body = JSON.stringify({
      messages: [{ data: Buffer.from(json).toString('base64') }],
    });

    await client.request<{ messageIds: string[] }>({
      url,
      method: 'POST',
      body,
    });

    return json;
  },
});