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.3 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod, httpClient } from '@activepieces/pieces-common';
import { telegramCommons } from '../common';
import { telegramBotAuth } from '../..';
import { sendMediaGroupActionOutputSchema } from '../output-schemas';

const MEDIA_GROUP_LIMIT = 10;

type MediaItem = {
  type: 'photo' | 'video' | 'audio' | 'document';
  media: string;
  caption?: string;
  parse_mode?: string;
};

export const telegramSendMediaGroupAction = createAction({
  auth: telegramBotAuth,
  name: 'send_media_group',
  classification: 'WRITE',
  displayName: 'Send Media Group',
  description:
    'Send 2 to 10 photos, videos, files or audio tracks as one album.',
  audience: 'human',
  aiMetadata: { description: 'Sends 2–10 media items (each a public URL or a Telegram file_id) as a single grouped album to a chat. Use to post several photos/videos together; photo and video may be mixed, but audio-only and document-only groups cannot be combined with other types. Not idempotent: each call posts a new album.', idempotent: false },
  propertyGroups: [
    { key: 'send_to', display: 'section', label: 'Send to', icon: 'send', props: ['instructions', 'chat_id'] },
    { key: 'album', display: 'section', label: 'Album', icon: 'file', props: ['media', 'parse_mode', 'instructions_format'] },
  ],
  props: {
    instructions: telegramCommons.form.chatIdInstructions(),
    chat_id: telegramCommons.form.chatIdProp(),
    media: Property.Array({
      displayName: 'Media',
      description:
        'Photos and videos can mix. Audio and files go in albums of their own.',
      required: true,
      properties: {
        type: Property.StaticDropdown({
          displayName: 'Type',
          required: true,
          options: {
            options: [
              { label: 'Photo', value: 'photo' },
              { label: 'Video', value: 'video' },
              { label: 'Audio', value: 'audio' },
              { label: 'Document', value: 'document' },
            ],
          },
          defaultValue: 'photo',
        }),
        media: Property.ShortText({
          displayName: 'URL or File ID',
          description: 'Public URL, or file_id of media already on Telegram.',
          required: true,
        }),
        caption: Property.LongText({
          displayName: 'Caption',
          description: 'Up to 1024 characters.',
          required: false,
        }),
      },
    }),
    parse_mode: telegramCommons.form.parseModeProp({
      displayName: 'Caption Format',
      description: 'How Telegram styles the captions.',
    }),
    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(),
  },
  outputSchema: sendMediaGroupActionOutputSchema,
  async run(ctx) {
    const items = (ctx.propsValue.media ?? []) as Array<Record<string, unknown>>;
    if (items.length < 2 || items.length > MEDIA_GROUP_LIMIT) {
      throw new Error('A media group must contain 2–10 media items.');
    }
    const parseMode = telegramCommons.resolveParseMode(ctx.propsValue.parse_mode);

    const media: MediaItem[] = items.map((item) => {
      const type = String(item['type'] ?? 'photo') as MediaItem['type'];
      const value = String(item['media'] ?? '');
      if (!value) {
        throw new Error('Each media item must define a URL or file_id.');
      }
      const caption = item['caption'] ? String(item['caption']) : undefined;
      const mediaItem: MediaItem = { type, media: value };
      if (caption) mediaItem.caption = caption;
      if (caption && parseMode) mediaItem.parse_mode = parseMode;
      return mediaItem;
    });

    return await httpClient.sendRequest<never>({
      method: HttpMethod.POST,
      url: telegramCommons.getApiUrl(ctx.auth, 'sendMediaGroup'),
      body: {
        chat_id: ctx.propsValue.chat_id,
        message_thread_id: ctx.propsValue.message_thread_id ?? undefined,
        media,
        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,
      },
    });
  },
});