From a5fec3a15700dd75a060271c3287b3175aede18a Mon Sep 17 00:00:00 2001 From: daniele Date: Fri, 12 Aug 2022 22:34:17 +0200 Subject: [PATCH 1/4] Added a new function for send message larger than 4096 characters --- src/telegram.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/telegram.js b/src/telegram.js index 2914e98e..1af442a8 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -17,6 +17,7 @@ const URL = require('url'); const fs = require('fs'); const pump = require('pump'); const deprecate = require('./utils').deprecate; +const MAX_MESSAGE_SIZE = 4096; const _messageTypes = [ 'text', @@ -74,6 +75,21 @@ function stringify(data) { return JSON.stringify(data); } +/** + * Slice an array in sub-array of the same size + * @private + * @param arr array + * @param chunkSize number + * @return Array[] + */ + function sliceIntoChunks (arr, chunkSize) { + const res = []; + for (let i = 0; i < arr.length; i += chunkSize) { + const chunk = arr.slice(i, i + chunkSize); + res.push(chunk); + } + return res; +} class TelegramBot extends EventEmitter { /** @@ -869,6 +885,28 @@ class TelegramBot extends EventEmitter { return this._request('sendMessage', { form }); } + /** + * Send text message larger than 4096 characters. + * @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} form Additional Telegram query options + * @returns {Promise} On success all the sent messages are returned as an array + */ + sendLargeMessage(chatId, text, form = {}){ + form.chat_id = chatId; + + const sub_messages = sliceIntoChunks(text, MAX_MESSAGE_SIZE); + let responses = []; + + for ( const message of sub_messages ){ + form.text = message; + const response_promise = this._request('sendMessage', { form }); + responses.push(response_promise); + } + + return Promise.all(responses); + } + /** * 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`) From 4d6bd7ea82e458e4a265bbd1e8d84425686b8d42 Mon Sep 17 00:00:00 2001 From: daniele Date: Fri, 12 Aug 2022 22:36:11 +0200 Subject: [PATCH 2/4] Updated documentation --- doc/api.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/api.md b/doc/api.md index e1e309c4..455fb14a 100644 --- a/doc/api.md +++ b/doc/api.md @@ -39,6 +39,7 @@ TelegramBot * [.logOut([options])](#TelegramBot+logOut) ⇒ Promise * [.close([options])](#TelegramBot+close) ⇒ Promise * [.sendMessage(chatId, text, [options])](#TelegramBot+sendMessage) ⇒ Promise + * [.sendLargeMessage(chatId, text, form)](#TelegramBot+sendLargeMessage) ⇒ Promise * [.forwardMessage(chatId, fromChatId, messageId, [options])](#TelegramBot+forwardMessage) ⇒ Promise * [.copyMessage(chatId, fromChatId, messageId, [options])](#TelegramBot+copyMessage) ⇒ Promise * [.sendPhoto(chatId, photo, [options], [fileOptions])](#TelegramBot+sendPhoto) ⇒ Promise @@ -120,7 +121,7 @@ TelegramBot * [.getGameHighScores(userId, [options])](#TelegramBot+getGameHighScores) ⇒ Promise * _static_ * [.errors](#TelegramBot.errors) : Object - * [.messageTypes](#TelegramBot.messageTypes) : [ 'Array' ].<String> + * [.messageTypes](#TelegramBot.messageTypes) : Array.<String> @@ -501,6 +502,20 @@ Send text message. | text | String | Text of the message to be sent | | [options] | Object | Additional Telegram query options | + + +### telegramBot.sendLargeMessage(chatId, text, form) ⇒ Promise +Send text message larger than 4096 characters. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**Returns**: Promise - On success all the sent messages are returned as an array + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number \| String | Unique identifier for the target chat or username of the target channel (in the format `@channelusername`) | +| text | String | Text of the message to be sent | +| form | Object | Additional Telegram query options | + ### telegramBot.forwardMessage(chatId, fromChatId, messageId, [options]) ⇒ Promise @@ -1677,7 +1692,7 @@ Animated stickers can be added to animated sticker sets and only to them: | --- | --- | --- | --- | | userId | Number | | User identifier of sticker set owner | | name | String | | Sticker set name | -| sticker | String \| stream.Stream \| Buffer | | Png image with the sticker (must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px), [TGS animation](https://core.telegram.org/stickers#animated-sticker-requirements) with the sticker or [WEBM video](https://core.telegram.org/stickers#video-sticker-requirements) with the sticker. | +| sticker | String \| stream.Stream \| Buffer | | Png image with the sticker (must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px, [TGS animation](https://core.telegram.org/stickers#animated-sticker-requirements) with the sticker or [WEBM video](https://core.telegram.org/stickers#video-sticker-requirements) with the sticker. | | emojis | String | | One or more emoji corresponding to the sticker | | stickerType | String | png_sticker | Allow values: `png_sticker`, `tgs_sticker`, or `webm_sticker`. | | [options] | Object | | Additional Telegram query options | @@ -1749,7 +1764,7 @@ Note: No more than 50 results per query are allowed. | Param | Type | Description | | --- | --- | --- | | inlineQueryId | String | Unique identifier of the query | -| results | [ 'Array' ].<InlineQueryResult> | An array of results for the inline query | +| results | Array.<InlineQueryResult> | An array of results for the inline query | | [options] | Object | Additional Telegram query options | @@ -1899,7 +1914,7 @@ The different errors the library uses. **Kind**: static property of [TelegramBot](#TelegramBot) -### TelegramBot.messageTypes : [ 'Array' ].<String> +### TelegramBot.messageTypes : Array.<String> The types of message updates the library handles. **Kind**: static property of [TelegramBot](#TelegramBot) From f904da104146120b5ec0d6e93c98dc5329ebf79b Mon Sep 17 00:00:00 2001 From: daniele Date: Thu, 18 Aug 2022 18:44:25 +0200 Subject: [PATCH 3/4] Fixed error in function documentation --- src/telegram.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/telegram.js b/src/telegram.js index 1af442a8..9f891f47 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -887,10 +887,10 @@ class TelegramBot extends EventEmitter { /** * Send text message larger than 4096 characters. - * @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} form Additional Telegram query options - * @returns {Promise} On success all the sent messages are returned as an array + * @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} form Additional Telegram query options + * @return {Promise} On success all the sent messages are returned as an array */ sendLargeMessage(chatId, text, form = {}){ form.chat_id = chatId; From 066bf54604f9723246173f3c96976e276650dcad Mon Sep 17 00:00:00 2001 From: daniele Date: Thu, 18 Aug 2022 19:13:59 +0200 Subject: [PATCH 4/4] code formatting and fixing eslint issue --- src/telegram.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/telegram.js b/src/telegram.js index 9f891f47..2135db77 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -82,11 +82,11 @@ function stringify(data) { * @param chunkSize number * @return Array[] */ - function sliceIntoChunks (arr, chunkSize) { +function sliceIntoChunks(arr, chunkSize) { const res = []; for (let i = 0; i < arr.length; i += chunkSize) { - const chunk = arr.slice(i, i + chunkSize); - res.push(chunk); + const chunk = arr.slice(i, i + chunkSize); + res.push(chunk); } return res; } @@ -892,18 +892,18 @@ class TelegramBot extends EventEmitter { * @param {Object} form Additional Telegram query options * @return {Promise} On success all the sent messages are returned as an array */ - sendLargeMessage(chatId, text, form = {}){ + sendLargeMessage(chatId, text, form = {}) { form.chat_id = chatId; - const sub_messages = sliceIntoChunks(text, MAX_MESSAGE_SIZE); - let responses = []; + const subMessages = sliceIntoChunks(text, MAX_MESSAGE_SIZE); + const responses = []; - for ( const message of sub_messages ){ + for (const message of subMessages) { form.text = message; - const response_promise = this._request('sendMessage', { form }); - responses.push(response_promise); + const resPromise = this._request('sendMessage', { form }); + responses.push(resPromise); } - + return Promise.all(responses); }