From 9303d0841a0c75ebc1e20a75b5a673f9d5b70d50 Mon Sep 17 00:00:00 2001 From: teidesu Date: Sat, 1 May 2021 00:30:53 +0300 Subject: [PATCH] feat(client): sendMediaGroup method --- packages/client/src/client.ts | 67 ++++++++++ .../src/methods/messages/send-media-group.ts | 121 ++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 packages/client/src/methods/messages/send-media-group.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 1005d886..a5c1ac72 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -71,6 +71,7 @@ import { searchGlobal } from './methods/messages/search-global' import { searchMessages } from './methods/messages/search-messages' import { sendDice } from './methods/messages/send-dice' import { sendLocation } from './methods/messages/send-location' +import { sendMediaGroup } from './methods/messages/send-media-group' import { sendMedia } from './methods/messages/send-media' import { sendText } from './methods/messages/send-text' import { unpinMessage } from './methods/messages/unpin-message' @@ -1600,6 +1601,71 @@ export interface TelegramClient extends BaseTelegramClient { replyMarkup?: ReplyMarkup } ): Promise + /** + * Send a group of media. + * + * @param chatId ID of the chat, its username, phone or `"me"` or `"self"` + * @param medias Medias contained in the message. + * @param params Additional sending parameters + * @see InputMedia + */ + sendMediaGroup( + chatId: InputPeerLike, + medias: InputMediaLike[], + params?: { + /** + * Message to reply to. Either a message object or message ID. + */ + replyTo?: number | Message + + /** + * Parse mode to use to parse entities before sending + * the message. Defaults to current default parse mode (if any). + * + * Passing `null` will explicitly disable formatting. + */ + parseMode?: string | null + + /** + * Whether to send this message silently. + */ + silent?: boolean + + /** + * If set, the message will be scheduled to this date. + * When passing a number, a UNIX time in ms is expected. + */ + schedule?: Date | number + + /** + * For bots: inline or reply markup or an instruction + * to hide a reply keyboard or to force a reply. + */ + replyMarkup?: ReplyMarkup + + /** + * Function that will be called after some part has been uploaded. + * Only used when a file that requires uploading is passed, + * and not used when uploading a thumbnail. + * + * @param index Index of the media in the original array + * @param uploaded Number of bytes already uploaded + * @param total Total file size + */ + progressCallback?: ( + index: number, + uploaded: number, + total: number + ) => void + + /** + * Whether to clear draft after sending this message. + * + * Defaults to `false` + */ + clearDraft?: boolean + } + ): Promise /** * Send a single media (a photo or a document-based media) * @@ -1964,6 +2030,7 @@ export class TelegramClient extends BaseTelegramClient { searchMessages = searchMessages sendDice = sendDice sendLocation = sendLocation + sendMediaGroup = sendMediaGroup sendMedia = sendMedia sendText = sendText unpinMessage = unpinMessage diff --git a/packages/client/src/methods/messages/send-media-group.ts b/packages/client/src/methods/messages/send-media-group.ts new file mode 100644 index 00000000..b15ba43e --- /dev/null +++ b/packages/client/src/methods/messages/send-media-group.ts @@ -0,0 +1,121 @@ +import { TelegramClient } from '../../client' +import { + BotKeyboard, + InputMediaLike, + InputPeerLike, + Message, + ReplyMarkup, +} from '../../types' +import { normalizeToInputPeer } from '../../utils/peer-utils' +import { normalizeDate, randomUlong } from '../../utils/misc-utils' +import { tl } from '@mtcute/tl' + +/** + * Send a group of media. + * + * @param chatId ID of the chat, its username, phone or `"me"` or `"self"` + * @param medias Medias contained in the message. + * @param params Additional sending parameters + * @see InputMedia + * @internal + */ +export async function sendMediaGroup( + this: TelegramClient, + chatId: InputPeerLike, + medias: InputMediaLike[], + params?: { + /** + * Message to reply to. Either a message object or message ID. + */ + replyTo?: number | Message + + /** + * Parse mode to use to parse entities before sending + * the message. Defaults to current default parse mode (if any). + * + * Passing `null` will explicitly disable formatting. + */ + parseMode?: string | null + + /** + * Whether to send this message silently. + */ + silent?: boolean + + /** + * If set, the message will be scheduled to this date. + * When passing a number, a UNIX time in ms is expected. + */ + schedule?: Date | number + + /** + * For bots: inline or reply markup or an instruction + * to hide a reply keyboard or to force a reply. + */ + replyMarkup?: ReplyMarkup + + /** + * Function that will be called after some part has been uploaded. + * Only used when a file that requires uploading is passed, + * and not used when uploading a thumbnail. + * + * @param index Index of the media in the original array + * @param uploaded Number of bytes already uploaded + * @param total Total file size + */ + progressCallback?: (index: number, uploaded: number, total: number) => void + + /** + * Whether to clear draft after sending this message. + * + * Defaults to `false` + */ + clearDraft?: boolean + } +): Promise { + if (!params) params = {} + + const peer = normalizeToInputPeer(await this.resolvePeer(chatId)) + const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup) + + const multiMedia: tl.RawInputSingleMedia[] = [] + + for (let i = 0; i < medias.length; i++) { + const media = medias[i] + const inputMedia = await this._normalizeInputMedia(media, { + progressCallback: params.progressCallback?.bind(null, i) + }) + + const [message, entities] = await this._parseEntities( + media.caption, + params.parseMode, + media.entities + ) + + multiMedia.push({ + _: 'inputSingleMedia', + randomId: randomUlong(), + media: inputMedia, + message, + entities + }) + } + + const res = await this.call({ + _: 'messages.sendMultiMedia', + peer, + multiMedia, + silent: params.silent, + replyToMsgId: params.replyTo + ? typeof params.replyTo === 'number' + ? params.replyTo + : params.replyTo.id + : undefined, + randomId: randomUlong(), + scheduleDate: normalizeDate(params.schedule), + replyMarkup, + clearDraft: params.clearDraft, + }) + + return this._findMessageInUpdate(res) +}