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

4.4 KB
Raw Blame
import {
  createAction,
  DynamicPropsValue,
  Property,
} from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { makeRequest } from '../common/client';
import { webexAuth } from '../common/auth';
import { fetchRooms, roomIdDropdown } from '../common/props';

export const createMessage = createAction({
  auth: webexAuth,
  name: 'createMessage',
  classification: 'WRITE',
  displayName: 'Create Message',
  description:
    'Post a plain text or rich text message to a Webex room or send a direct message',
  audience: 'both',
  aiMetadata: {
    description:
      'Sends a message via Webex, in one of two modes selected by the message type: post to a room (by room ID) or send a direct message to a person (by person ID or email). Use it to deliver notifications, replies, or rich-text/markdown content with an optional file attachment. Each call posts a new message, so it is not idempotent; for direct messages either a person ID or email is required.',
    idempotent: false,
  },
  props: {
    messageType: Property.StaticDropdown({
      displayName: 'Message Type',
      description:
        'Choose between sending to a room or a direct message to a person',
      required: true,
      options: {
        options: [
          { label: 'Room Message', value: 'room' },
          { label: 'Direct Message', value: 'direct' },
        ],
      },
    }),
    destination: Property.DynamicProperties({
      displayName: 'Destination',
      required: true,
      refreshers: ['messageType'],
      auth:webexAuth,
      async props({ messageType, auth }): Promise<DynamicPropsValue> {
        const type = messageType as unknown as string;
        if (type === 'room') {
          const rooms = await fetchRooms((auth as any).access_token);
          return {
            roomId: roomIdDropdown(rooms),
          };
        } else {
          return {
            toPersonId: Property.ShortText({
              displayName: 'Person ID',
              description: 'The person ID of the recipient',
              required: false,
            }),
            toPersonEmail: Property.ShortText({
              displayName: 'Person Email',
              description: 'The email address of the recipient',
              required: false,
            }),
          };
        }
      },
    }),
    text: Property.LongText({
      displayName: 'Message Text',
      description: 'The message to send',
      required: true,
    }),
    markdown: Property.LongText({
      displayName: 'Message Markdown',
      description:
        'The message in Markdown format for rich text. If provided, text will be used as alternate text',
      required: false,
    }),
    parentId: Property.ShortText({
      displayName: 'Parent Message ID',
      description: 'The parent message ID to reply to (optional)',
      required: false,
    }),
    files: Property.Array({
      displayName: 'File URLs',
      description: 'Public URLs to files to attach (only one file per message)',
      properties: {
        url: Property.ShortText({
          displayName: 'File URL',
          description: 'Public URL to the file',
          required: true,
        }),
      },
      required: false,
    }),
  },
  async run(context) {
    const { messageType, destination, text, markdown, parentId } =
      context.propsValue;

    const files = context.propsValue.files as { url: string }[] | undefined;

    const body: Record<string, unknown> = {
      text,
    };

    if (messageType === 'room') {
      const roomId = destination['roomId'] as string;
      body['roomId'] = roomId;
    } else {
      const toPersonId = destination['toPersonId'] as string | undefined;
      const toPersonEmail = destination['toPersonEmail'] as string | undefined;

      if (!toPersonId && !toPersonEmail) {
        throw new Error(
          'Either Person ID or Person Email is required for direct messages'
        );
      }

      if (toPersonId) {
        body['toPersonId'] = toPersonId;
      }
      if (toPersonEmail) {
        body['toPersonEmail'] = toPersonEmail;
      }
    }

    if (markdown) {
      body['markdown'] = markdown;
    }

    if (parentId) {
      body['parentId'] = parentId;
    }

    if (files && files.length > 0) {
      body['files'] = [files[0].url];
    }

    const response = await makeRequest(
      context.auth.access_token,
      HttpMethod.POST,
      '/messages',
      body
    );

    return response;
  },
});