From 58261d1be30c4759176334cdce379aed55500d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=C3=A9rez=20Fern=C3=A1ndez?= Date: Fri, 30 Dec 2022 20:47:36 +0100 Subject: [PATCH] feat: Telegram Bot API 6.4 Support (#1040) * feat: Telegram Bot API 6.4 * fix: Add Thumb sendVideo/... * docs: Regenerate * fix: set/get mycommands and suggested tip amounts --- CHANGELOG.md | 14 +++++ doc/api.md | 91 ++++++++++++++++++++++++++++++-- package.json | 4 +- src/telegram.js | 135 +++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 215 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b959b3b..3b9a508 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,20 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [0.61.0][0.61.0] - 2022-12-30 + +1. Support Telegram Bot API v6.4 (@danielperez9430) + * editGeneralForumTopic + * closeGeneralForumTopic + * reopenGeneralForumTopic + * hideGeneralForumTopic + * unhideGeneralForumTopic + +2. Minor changes: (@danielperez9430) + * The parameters `name` and `icon_custom_emoji_id` of the method `editForumTopic` are now optional. + * Fix add thumb in sendAudio, sendVideo and sendVideoNote + * Fix getMyCommands and setMyCommands + * Suggested tip amounts stringify in sendInvoice ## [0.60.0][0.60.0] - 2022-10-06 1. Support Telegram Bot API v6.3 (@danielperez9430) diff --git a/doc/api.md b/doc/api.md index 21e5fa0..5718653 100644 --- a/doc/api.md +++ b/doc/api.md @@ -89,11 +89,16 @@ TelegramBot * [.deleteChatStickerSet(chatId, [options])](#TelegramBot+deleteChatStickerSet) ⇒ Promise * [.getForumTopicIconStickers(chatId, [options])](#TelegramBot+getForumTopicIconStickers) ⇒ Promise * [.createForumTopic(chatId, name, [options])](#TelegramBot+createForumTopic) - * [.editForumTopic(chatId, messageThreadId, name, iconCustomEmojiId, [options])](#TelegramBot+editForumTopic) ⇒ Promise + * [.editForumTopic(chatId, messageThreadId, [options])](#TelegramBot+editForumTopic) ⇒ Promise * [.closeForumTopic(chatId, messageThreadId, [options])](#TelegramBot+closeForumTopic) ⇒ Promise * [.reopenForumTopic(chatId, messageThreadId, [options])](#TelegramBot+reopenForumTopic) ⇒ Promise * [.deleteForumTopic(chatId, messageThreadId, [options])](#TelegramBot+deleteForumTopic) ⇒ Promise * [.unpinAllForumTopicMessages(chatId, messageThreadId, [options])](#TelegramBot+unpinAllForumTopicMessages) ⇒ Promise + * [.editGeneralForumTopic(chatId, name, [options])](#TelegramBot+editGeneralForumTopic) ⇒ Promise + * [.closeGeneralForumTopic(chatId, [options])](#TelegramBot+closeGeneralForumTopic) ⇒ Promise + * [.reopenGeneralForumTopic(chatId, [options])](#TelegramBot+reopenGeneralForumTopic) ⇒ Promise + * [.hideGeneralForumTopic(chatId, [options])](#TelegramBot+hideGeneralForumTopic) ⇒ Promise + * [.unhideGeneralForumTopic(chatId, [options])](#TelegramBot+unhideGeneralForumTopic) ⇒ Promise * [.answerCallbackQuery(callbackQueryId, [options])](#TelegramBot+answerCallbackQuery) ⇒ Promise * [.setMyCommands(commands, [options])](#TelegramBot+setMyCommands) ⇒ Promise * [.deleteMyCommands([options])](#TelegramBot+deleteMyCommands) ⇒ Promise @@ -1405,7 +1410,7 @@ Returns information about the created topic as a [ForumTopic](https://core.teleg -### telegramBot.editForumTopic(chatId, messageThreadId, name, iconCustomEmojiId, [options]) ⇒ Promise +### telegramBot.editForumTopic(chatId, messageThreadId, [options]) ⇒ Promise Use this method to edit name and icon of a topic in a forum supergroup chat. The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights, unless it is the creator of the topic. @@ -1417,8 +1422,6 @@ The bot must be an administrator in the chat for this to work and must have can_ | --- | --- | --- | | chatId | Number \| String | Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) | | messageThreadId | Number | Unique identifier for the target message thread of the forum topic | -| name | String | New topic name, 1-128 characters | -| iconCustomEmojiId | String | New unique identifier of the custom emoji shown as the topic icon. Use [getForumTopicIconStickers](https://core.telegram.org/bots/api#getforumtopiciconstickers) to get all allowed custom emoji identifiers | | [options] | Object | Additional Telegram query options | @@ -1485,6 +1488,86 @@ The bot must be an administrator in the chat for this to work and must have the | messageThreadId | Number | Unique identifier for the target message thread of the forum topic | | [options] | Object | Additional Telegram query options | + + +### telegramBot.editGeneralForumTopic(chatId, name, [options]) ⇒ Promise +Use this method to edit the name of the 'General' topic in a forum supergroup chat. +The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. +The topic will be automatically unhidden if it was hidden. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**Returns**: Promise - True on success +**See**: https://core.telegram.org/bots/api#editgeneralforumtopic + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number \| String | Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) | +| name | String | New topic name, 1-128 characters | +| [options] | Object | Additional Telegram query options | + + + +### telegramBot.closeGeneralForumTopic(chatId, [options]) ⇒ Promise +Use this method to close an open 'General' topic in a forum supergroup chat. +The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. +The topic will be automatically unhidden if it was hidden. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**Returns**: Promise - True on success +**See**: https://core.telegram.org/bots/api#closegeneralforumtopic + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number \| String | Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) | +| [options] | Object | Additional Telegram query options | + + + +### telegramBot.reopenGeneralForumTopic(chatId, [options]) ⇒ Promise +Use this method to reopen a closed 'General' topic in a forum supergroup chat. +The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. +The topic will be automatically unhidden if it was hidden. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**Returns**: Promise - True on success +**See**: https://core.telegram.org/bots/api#reopengeneralforumtopic + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number \| String | Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) | +| [options] | Object | Additional Telegram query options | + + + +### telegramBot.hideGeneralForumTopic(chatId, [options]) ⇒ Promise +Use this method to hide the 'General' topic in a forum supergroup chat. +The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. +The topic will be automatically closed if it was open. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**Returns**: Promise - True on success +**See**: https://core.telegram.org/bots/api#hidegeneralforumtopic + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number \| String | Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) | +| [options] | Object | Additional Telegram query options | + + + +### telegramBot.unhideGeneralForumTopic(chatId, [options]) ⇒ Promise +Use this method to unhide the 'General' topic in a forum supergroup chat. +The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**Returns**: Promise - True on success +**See**: https://core.telegram.org/bots/api#unhidegeneralforumtopic + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number \| String | Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) | +| [options] | Object | Additional Telegram query options | + ### telegramBot.answerCallbackQuery(callbackQueryId, [options]) ⇒ Promise diff --git a/package.json b/package.json index 233da35..d26fecb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-telegram-bot-api", - "version": "0.60.0", + "version": "0.61.0", "description": "Telegram Bot API", "main": "./index.js", "directories": { @@ -71,4 +71,4 @@ "url": "https://github.com/yagop/node-telegram-bot-api/issues" }, "homepage": "https://github.com/yagop/node-telegram-bot-api" -} \ No newline at end of file +} diff --git a/src/telegram.js b/src/telegram.js index 5e7ef8e..1081624 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -237,6 +237,26 @@ class TelegramBot extends EventEmitter { } } + _fixAddFileThumb(options, opts) { + if (options.thumb) { + if (opts.formData === null) { + opts.formData = {}; + } + + try { + const attachName = 'photo'; + const [formData] = this._formatSendData(attachName, options.thumb.replace('attach://', '')); + + if (formData) { + opts.formData[attachName] = formData[attachName]; + opts.qs.thumb = `attach://${attachName}`; + } + } catch (ex) { + throw Promise.reject(ex); + } + } + } + /** * Make request against the API * @param {String} _path API endpoint @@ -987,28 +1007,11 @@ class TelegramBot extends EventEmitter { const sendData = this._formatSendData('audio', audio, fileOptions); opts.formData = sendData[0]; opts.qs.audio = sendData[1]; + this._fixAddFileThumb(options, opts); } catch (ex) { return Promise.reject(ex); } - if (options.thumb) { - if (opts.formData === null) { - opts.formData = {}; - } - - try { - const attachName = 'photo'; - const [formData] = this._formatSendData(attachName, options.thumb.replace('attach://', '')); - - if (formData) { - opts.formData[attachName] = formData[attachName]; - opts.qs.thumb = `attach://${attachName}`; - } - } catch (ex) { - return Promise.reject(ex); - } - } - return this._request('sendAudio', opts); } @@ -1032,9 +1035,11 @@ class TelegramBot extends EventEmitter { const sendData = this._formatSendData('document', doc, fileOptions); opts.formData = sendData[0]; opts.qs.document = sendData[1]; + this._fixAddFileThumb(options, opts); } catch (ex) { return Promise.reject(ex); } + return this._request('sendDocument', opts); } @@ -1059,6 +1064,7 @@ class TelegramBot extends EventEmitter { const sendData = this._formatSendData('video', video, fileOptions); opts.formData = sendData[0]; opts.qs.video = sendData[1]; + this._fixAddFileThumb(options, opts); } catch (ex) { return Promise.reject(ex); } @@ -1141,6 +1147,7 @@ class TelegramBot extends EventEmitter { const sendData = this._formatSendData('video_note', videoNote, fileOptions); opts.formData = sendData[0]; opts.qs.video_note = sendData[1]; + this._fixAddFileThumb(options, opts); } catch (ex) { return Promise.reject(ex); } @@ -1916,17 +1923,13 @@ class TelegramBot extends EventEmitter { * * @param {Number|String} chatId Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) * @param {Number} messageThreadId Unique identifier for the target message thread of the forum topic - * @param {String} name New topic name, 1-128 characters - * @param {String} iconCustomEmojiId New unique identifier of the custom emoji shown as the topic icon. Use getForumTopicIconStickers to get all allowed custom emoji identifiers * @param {Object} [options] Additional Telegram query options * @return {Promise} True on success * @see https://core.telegram.org/bots/api#editforumtopic */ - editForumTopic(chatId, messageThreadId, name, iconCustomEmojiId, form = {}) { + editForumTopic(chatId, messageThreadId, form = {}) { form.chat_id = chatId; form.message_thread_id = messageThreadId; - form.name = name; - form.icon_custom_emoji_id = iconCustomEmojiId; return this._request('editForumTopic', { form }); } @@ -1994,6 +1997,81 @@ class TelegramBot extends EventEmitter { return this._request('unpinAllForumTopicMessages', { form }); } + /** + * Use this method to edit the name of the 'General' topic in a forum supergroup chat. + * The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. + * The topic will be automatically unhidden if it was hidden. + * + * @param {Number|String} chatId Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) + * @param {String} name New topic name, 1-128 characters + * @param {Object} [options] Additional Telegram query options + * @return {Promise} True on success + * @see https://core.telegram.org/bots/api#editgeneralforumtopic + */ + editGeneralForumTopic(chatId, name, form = {}) { + form.chat_id = chatId; + form.name = name; + return this._request('editGeneralForumTopic', { form }); + } + + /** + * Use this method to close an open 'General' topic in a forum supergroup chat. + * The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. + * The topic will be automatically unhidden if it was hidden. + * + * @param {Number|String} chatId Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) + * @param {Object} [options] Additional Telegram query options + * @return {Promise} True on success + * @see https://core.telegram.org/bots/api#closegeneralforumtopic + */ + closeGeneralForumTopic(chatId, form = {}) { + form.chat_id = chatId; + return this._request('closeGeneralForumTopic', { form }); + } + + /** + * Use this method to reopen a closed 'General' topic in a forum supergroup chat. + * The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. + * The topic will be automatically unhidden if it was hidden. + * + * @param {Number|String} chatId Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) + * @param {Object} [options] Additional Telegram query options + * @return {Promise} True on success + * @see https://core.telegram.org/bots/api#reopengeneralforumtopic + */ + reopenGeneralForumTopic(chatId, form = {}) { + form.chat_id = chatId; + return this._request('reopenGeneralForumTopic', { form }); + } + + /** + * Use this method to hide the 'General' topic in a forum supergroup chat. + * The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. + * The topic will be automatically closed if it was open. + * + * @param {Number|String} chatId Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) + * @param {Object} [options] Additional Telegram query options + * @return {Promise} True on success + * @see https://core.telegram.org/bots/api#hidegeneralforumtopic + */ + hideGeneralForumTopic(chatId, form = {}) { + form.chat_id = chatId; + return this._request('hideGeneralForumTopic', { form }); + } + + /** + * Use this method to unhide the 'General' topic in a forum supergroup chat. + * The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights + * + * @param {Number|String} chatId Unique identifier for the target group or username of the target supergroup (in the format @supergroupusername) + * @param {Object} [options] Additional Telegram query options + * @return {Promise} True on success + * @see https://core.telegram.org/bots/api#unhidegeneralforumtopic + */ + unhideGeneralForumTopic(chatId, form = {}) { + form.chat_id = chatId; + return this._request('unhideGeneralForumTopic', { form }); + } /** * Use this method to send answers to callback queries sent from @@ -2049,6 +2127,11 @@ class TelegramBot extends EventEmitter { */ setMyCommands(commands, form = {}) { form.commands = stringify(commands); + + if (form.scope) { + form.scope = stringify(form.scope); + } + return this._request('setMyCommands', { form }); } @@ -2074,6 +2157,9 @@ class TelegramBot extends EventEmitter { * @see https://core.telegram.org/bots/api#getmycommands */ getMyCommands(form = {}) { + if (form.scope) { + form.scope = stringify(form.scope); + } return this._request('getMyCommands', { form }); } @@ -2544,6 +2630,9 @@ class TelegramBot extends EventEmitter { form.currency = currency; form.prices = stringify(prices); form.provider_data = stringify(form.provider_data); + if (form.suggested_tip_amounts) { + form.suggested_tip_amounts = stringify(form.suggested_tip_amounts); + } return this._request('sendInvoice', { form }); }