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.7 KB
Raw Blame
import {
  ApFile,
  createAction,
  MarkdownVariant,
  Property,
} from '@activepieces/pieces-framework';
import {
  HttpMessageBody,
  HttpMethod,
  httpClient,
} from '@activepieces/pieces-common';
import FormData from 'form-data';
import { telegramCommons } from '../common';
import { telegramBotAuth } from '../..';
import { sendDocumentActionOutputSchema } from '../output-schemas';

export const telegramSendDocumentAction = createAction({
  auth: telegramBotAuth,
  name: 'send_document',
  classification: 'WRITE',
  displayName: 'Send Document',
  description: 'Send any file to a chat as a document.',
  audience: 'human',
  aiMetadata: { description: 'Uploads and sends a generic file (up to 50 MB) as a document to a Telegram chat, supplied as a file or a previously uploaded Telegram file_id. Use for arbitrary attachments (PDFs, archives, spreadsheets) rather than media shown inline. Not idempotent: each call sends a new document.', idempotent: false },
  propertyGroups: [
    { key: 'send_to', display: 'section', label: 'Send to', icon: 'send', props: ['instructions', 'chat_id'] },
    { key: 'file', display: 'section', label: 'File', icon: 'paperclip', props: ['file_info', 'document', 'document_id'] },
    { key: 'caption', display: 'section', label: 'Caption', icon: 'text', props: ['format', 'caption', 'instructions_format'] },
  ],
  props: {
    instructions: telegramCommons.form.chatIdInstructions(),
    chat_id: telegramCommons.form.chatIdProp(),
    file_info: Property.MarkDown({
      value: 'Upload a file or enter a file ID. If both are set, the upload is sent.',
      variant: MarkdownVariant.INFO,
    }),
    document: Property.File({
      displayName: 'Document',
      description: 'Any file type, up to 50 MB.',
      required: false,
    }),
    document_id: Property.ShortText({
      displayName: 'Document ID',
      description: 'file_id of a document already on Telegram, to send it again.',
      required: false,
    }),
    format: telegramCommons.form.parseModeProp(),
    caption: Property.LongText({
      displayName: 'Caption',
      description: 'Up to 1024 characters.',
      required: false,
    }),
    instructions_format: telegramCommons.form.formatLinkInstructions(),
    message_thread_id: telegramCommons.form.messageThreadIdProp(),
    disable_notification: telegramCommons.form.disableNotificationProp(),
    protect_content: telegramCommons.form.protectContentProp(),
    reply_to_message_id: telegramCommons.form.replyToMessageIdProp(),
    reply_markup: telegramCommons.form.replyMarkupProp(),
  },
  outputSchema: sendDocumentActionOutputSchema,
  async run(ctx) {
    const file = ctx.propsValue.document as ApFile | undefined;
    const documentId = ctx.propsValue.document_id;
    const parseMode = telegramCommons.resolveParseMode(ctx.propsValue.format);

    if (!file && !documentId) {
      throw new Error(
        'Either a document file or a document id must be provided.'
      );
    }

    const headers: Record<string, string> = {};
    let body: HttpMessageBody;

    if (file && file.data && file.filename) {
      const form = new FormData();
      form.append('document', file.data, file.filename);
      form.append('chat_id', ctx.propsValue.chat_id);
      if (ctx.propsValue.caption) form.append('caption', ctx.propsValue.caption);
      if (ctx.propsValue.message_thread_id) {
        form.append('message_thread_id', ctx.propsValue.message_thread_id);
      }
      if (parseMode) form.append('parse_mode', parseMode);
      if (ctx.propsValue.disable_notification) form.append('disable_notification', 'true');
      if (ctx.propsValue.protect_content) form.append('protect_content', 'true');
      if (ctx.propsValue.reply_to_message_id) {
        form.append('reply_to_message_id', String(ctx.propsValue.reply_to_message_id));
      }
      if (ctx.propsValue.reply_markup) {
        form.append('reply_markup', JSON.stringify(ctx.propsValue.reply_markup));
      }
      body = form;
      Object.assign(headers, form.getHeaders());
    } else {
      body = {
        chat_id: ctx.propsValue.chat_id,
        document: documentId,
        caption: ctx.propsValue.caption ?? undefined,
        message_thread_id: ctx.propsValue.message_thread_id ?? undefined,
        parse_mode: parseMode,
        disable_notification: ctx.propsValue.disable_notification ?? false,
        protect_content: ctx.propsValue.protect_content ?? false,
        reply_to_message_id: ctx.propsValue.reply_to_message_id ?? undefined,
        reply_markup: ctx.propsValue.reply_markup ?? undefined,
      };
    }

    return await httpClient.sendRequest<never>({
      method: HttpMethod.POST,
      url: telegramCommons.getApiUrl(ctx.auth, 'sendDocument'),
      headers,
      body,
    });
  },
});