UNPKG

grammy-edit-or-reply

Version:

Exports utilities for grammy to edit a message or reply to a message based on the context

80 lines (79 loc) 2.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeInlineResult = void 0; const edit_or_reply_1 = require("./edit-or-reply"); const types_1 = require("./types"); /** * Create an InlineResult for the given message specification. * * If a media is present it must be a `fileId`, as `InputFile`s are not * supported in inline results, and URLs would require extra metadata. * Use `messageDataHasNoInputFile` to help narrowing the type. */ function makeInlineResult(messageData) { if (!(0, types_1.messageDataHasMedia)(messageData)) { return { type: 'article', input_message_content: { message_text: messageData.text, ...(0, edit_or_reply_1.makeOther)(messageData, [ 'parse_mode', 'entities', 'link_preview_options', ]), }, ...(0, edit_or_reply_1.makeOther)(messageData, ['reply_markup']), }; } const mediaData = messageData.media; const { type: mediaType } = mediaData; const mediaFileId = mediaData.media; const defaultOther = [ 'caption', 'parse_mode', 'caption_entities', 'reply_markup', ]; if (mediaType === 'animation') { return { type: 'gif', gif_file_id: mediaFileId, ...(0, edit_or_reply_1.makeOther)(messageData, defaultOther), }; } else if (mediaType === 'document') { return { type: 'document', document_file_id: mediaFileId, ...(0, edit_or_reply_1.makeOther)(messageData, defaultOther), }; } else if (mediaType === 'audio') { return { // NOTE: even using files that are already uploaded on telegram // it can happen that 'Bad Request: AUDIO_TITLE_EMPTY' is returned, // to avoid this issue we send the media as document. type: 'document', document_file_id: mediaFileId, ...(0, edit_or_reply_1.makeOther)(messageData, defaultOther), }; } else if (mediaType === 'photo') { return { type: 'photo', photo_file_id: mediaFileId, ...(0, edit_or_reply_1.makeOther)(messageData, [...defaultOther, 'show_caption_above_media']), }; } else if (mediaType === 'video') { return { type: 'video', video_file_id: mediaFileId, ...(0, edit_or_reply_1.makeOther)(messageData, [...defaultOther, 'show_caption_above_media']), }; } else { throw Error(`Unsupported media type: ${mediaType}`); } } exports.makeInlineResult = makeInlineResult;