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) diff --git a/src/telegram.js b/src/telegram.js index 2914e98e..2135db77 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 + * @return {Promise} On success all the sent messages are returned as an array + */ + sendLargeMessage(chatId, text, form = {}) { + form.chat_id = chatId; + + const subMessages = sliceIntoChunks(text, MAX_MESSAGE_SIZE); + const responses = []; + + for (const message of subMessages) { + form.text = message; + const resPromise = this._request('sendMessage', { form }); + responses.push(resPromise); + } + + 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`)