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 { createAction, Property } from '@activepieces/pieces-framework';
import { timelinesAiAuth, timelinesAiCommon } from '../common';
import { chatDropdown } from '../common/properties';

export const sendMessageToExistingChat = createAction({
  auth: timelinesAiAuth,
  name: 'sendMessageToExistingChat',
  classification: 'WRITE',
  displayName: 'Send Message to Existing Chat',
  description: 'Sends a text message in a chat identified by chat_id',
  audience: 'both',
  aiMetadata: { description: 'Sends a text message into an existing TimelinesAI WhatsApp chat identified by its numeric chat_id, optionally attaching an uploaded file and a label. Use to reply within a conversation you already have the chat_id for; use Send Message to New Chat instead when starting a conversation by phone or JID. Not idempotent: each call posts a new message.', idempotent: false },
  props: {
    chat_id: chatDropdown({ required: true }),
    text: Property.LongText({
      displayName: 'Message Text',
      description: 'The text content of the message to be sent.',
      required: true,
    }),
    file: Property.File({
      displayName: 'File',
      description: 'Optional file to send with the message.',
      required: false,
    }),
    label: Property.ShortText({
      displayName: 'Label',
      description: 'Optional label to categorize or tag the message.',
      required: false,
    }),
    attachment_template_id: Property.Number({
      displayName: 'Attachment Template ID',
      description:
        'Optional ID of the attachment template to use for the message.',
      required: false,
    }),
  },
  async run({ auth: apiKey, propsValue }) {
    const { chat_id, file, ...rest } = propsValue;
    if (chat_id === undefined) {
      throw new Error('chat_id is required');
    }
    const messageParams = {
      chat_id: Number(chat_id),
      file_uid: undefined as string | undefined,
      ...rest,
    };
    if (file !== undefined) {
      const fileUploadResponse = await timelinesAiCommon.uploadFile({
        apiKey,
        file: file.data,
        filename: file.filename,
      });
      if (fileUploadResponse.status !== 'ok') {
        throw new Error('File upload failed');
      }
      messageParams['file_uid'] = fileUploadResponse.data.uid;
    }

    const response = await timelinesAiCommon.sendMessageToExistingChat({
      apiKey,
      ...messageParams,
    });

    if (response.status !== 'ok') {
      throw new Error(response.message || 'Failed to send message');
    }

    return response.data;
  },
});