From ff7dc93104ca85fdf8da94e708bfe61b84c0b505 Mon Sep 17 00:00:00 2001 From: shahabhm Date: Tue, 14 Feb 2023 00:29:34 +0330 Subject: [PATCH] add sendLongMessage function to telegram.js --- src/telegram.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/telegram.js b/src/telegram.js index 7b9440a3..2be6160f 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -914,6 +914,38 @@ class TelegramBot extends EventEmitter { return this._request('sendMessage', { form }); } + /** + * Sends long text message. + * currently, telegram bots can send only 4000 characters per message. to send a message longer than this length, + * you need to split it into multiple messages and then send them separately. this function does that. + * @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format `@channelusername`) + * @param {String} text Text of the message to be sent + * @param {Object} [options] Additional Telegram query options + * @param {retries} optional, specifies the maximum number of retries when trying to send the message; + * @return {boolean} On success, the last chunk's message object is returned + * @see https://core.telegram.org/bots/api#sendmessage + */ + async sendLongMessage(chatId, text, form = {}, retries = 3) { + const MAX_MESSAGE_LENGTH = 4000; + let used_retries = 0; + let chunk_send_result; + let chunk_counts = Math.ceil(text.length/MAX_MESSAGE_LENGTH); + for (let chunk = 0; chunk < chunk_counts; chunk++) { + try{ + let chunk_start = MAX_MESSAGE_LENGTH * chunk; + let chunk_end = Math.max(MAX_MESSAGE_LENGTH * (chunk + 1), text.length); + chunk_send_result = await this.sendMessage(chatId, text.slice(chunk_start, chunk_end)); + }catch (error){ + used_retries += 1; + chunk -= 1; + if (used_retries > retries){ + throw error; + } + } + } + return chunk_send_result; + } + /** * Forward messages of any kind. * @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format `@channelusername`)