Skip to content

Commit cceea22

Browse files
Sp3rickSp3rick
andauthored
feat: Add methods deleteMessages and copyMessages
* Added setMessageReaction method * Added setMessageReaction method * Added and implemented _fixReplyParameters --------- Co-authored-by: Sp3rick <your_email@example.com>
1 parent e81ec60 commit cceea22

File tree

3 files changed

+167
-2
lines changed

3 files changed

+167
-2
lines changed

doc/api.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ TelegramBot
145145
* [.sendGame(chatId, gameShortName, [options])](#TelegramBot+sendGame) ⇒ <code>Promise</code>
146146
* [.setGameScore(userId, score, [options])](#TelegramBot+setGameScore) ⇒ <code>Promise</code>
147147
* [.getGameHighScores(userId, [options])](#TelegramBot+getGameHighScores) ⇒ <code>Promise</code>
148+
* [.setMessageReaction(chatId, messageId, [options])](#TelegramBot+setMessageReaction) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code>
149+
* [.deleteMessages(chatId, messageIds, [options])](#TelegramBot+deleteMessages) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code>
150+
* [.copyMessages(chatId, fromChatId, messageIds, [options])](#TelegramBot+copyMessages) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Array.&lt;TelegramBot.MessageId&gt;&gt;</code>
148151
* _static_
149152
* [.errors](#TelegramBot.errors) : <code>Object</code>
150153
* [.messageTypes](#TelegramBot.messageTypes) : <code>[ &#x27;Array&#x27; ].&lt;String&gt;</code>
@@ -1918,7 +1921,7 @@ Use this method to delete a message, including service messages, with the follow
19181921

19191922
| Param | Type | Description |
19201923
| --- | --- | --- |
1921-
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier of the target chat |
1924+
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier for the target chat or username of the target channel (in the format @channelusername) |
19221925
| messageId | <code>Number</code> | Unique identifier of the target message |
19231926
| [options] | <code>Object</code> | Additional Telegram query options |
19241927

@@ -2339,6 +2342,55 @@ Will return the score of the specified user and several of their neighbors in a
23392342
| userId | <code>Number</code> | Unique identifier of the target user |
23402343
| [options] | <code>Object</code> | Additional Telegram query options |
23412344

2345+
<a name="TelegramBot+setMessageReaction"></a>
2346+
2347+
### telegramBot.setMessageReaction(chatId, messageId, [options]) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code>
2348+
Use this method to change the chosen reactions on a message.
2349+
- Service messages can't be reacted to.
2350+
- Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel.
2351+
- In albums, bots must react to the first message.
2352+
2353+
**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
2354+
**Returns**: <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code> - True on success
2355+
**See**: https://core.telegram.org/bots/api#setMessageReaction
2356+
2357+
| Param | Type | Description |
2358+
| --- | --- | --- |
2359+
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier for the target chat or username of the target channel (in the format @channelusername) |
2360+
| messageId | <code>Number</code> | Unique identifier of the target message |
2361+
| [options] | <code>Object</code> | Additional Telegram query options |
2362+
2363+
<a name="TelegramBot+deleteMessages"></a>
2364+
2365+
### telegramBot.deleteMessages(chatId, messageIds, [options]) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code>
2366+
Use this method to delete multiple messages simultaneously. If some of the specified messages can't be found, they are skipped.
2367+
2368+
**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
2369+
**Returns**: <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code> - True on success
2370+
**See**: https://core.telegram.org/bots/api#deleteMessages
2371+
2372+
| Param | Type | Description |
2373+
| --- | --- | --- |
2374+
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier for the target chat or username of the target channel (in the format @channelusername) |
2375+
| messageIds | <code>[ &#x27;Array&#x27; ].&lt;(Number\|String)&gt;</code> | Identifiers of 1-100 messages to delete. See deleteMessage for limitations on which messages can be deleted |
2376+
| [options] | <code>Object</code> | Additional Telegram query options |
2377+
2378+
<a name="TelegramBot+copyMessages"></a>
2379+
2380+
### telegramBot.copyMessages(chatId, fromChatId, messageIds, [options]) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Array.&lt;TelegramBot.MessageId&gt;&gt;</code>
2381+
Use this method to copy messages of any kind. If some of the specified messages can't be found or copied, they are skipped. Service messages, giveaway messages, giveaway winners messages, and invoice messages can't be copied. A quiz poll can be copied only if the value of the field correct_option_id is known to the bot. The method is analogous to the method forwardMessages, but the copied messages don't have a link to the original message. Album grouping is kept for copied messages.
2382+
2383+
**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
2384+
**Returns**: <code>[ &#x27;Promise&#x27; ].&lt;Array.&lt;TelegramBot.MessageId&gt;&gt;</code> - On success, an array of MessageId of the sent messages is returned.
2385+
**See**: https://core.telegram.org/bots/api#copyMessages
2386+
2387+
| Param | Type | Description |
2388+
| --- | --- | --- |
2389+
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier for the target chat or username of the target channel (in the format @channelusername) |
2390+
| fromChatId | <code>Number</code> \| <code>String</code> | Unique identifier for the chat where the original message was sent (or channel username in the format `@channelusername`) |
2391+
| messageIds | <code>[ &#x27;Array&#x27; ].&lt;(Number\|String)&gt;</code> | Identifiers of 1-100 messages in the chat from_chat_id to copy. The identifiers must be specified in a strictly increasing order. |
2392+
| [options] | <code>Object</code> | Additional Telegram query options |
2393+
23422394
<a name="TelegramBot.errors"></a>
23432395

23442396
### TelegramBot.errors : <code>Object</code>

src/telegram.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ class TelegramBot extends EventEmitter {
253253
}
254254
}
255255

256+
/**
257+
* Fix 'reply_parameters' parameter by making it JSON-serialized, as
258+
* required by the Telegram Bot API
259+
* @param {Object} obj Object; either 'form' or 'qs'
260+
* @private
261+
* @see https://core.telegram.org/bots/api#sendmessage
262+
*/
263+
_fixReplyParameters(obj) {
264+
if (obj.hasOwnProperty('reply_parameters') && typeof obj.reply_parameters !== 'string') {
265+
obj.reply_parameters = stringify(obj.reply_parameters);
266+
}
267+
}
268+
256269
/**
257270
* Make request against the API
258271
* @param {String} _path API endpoint
@@ -272,9 +285,11 @@ class TelegramBot extends EventEmitter {
272285
if (options.form) {
273286
this._fixReplyMarkup(options.form);
274287
this._fixEntitiesField(options.form);
288+
this._fixReplyParameters(options.form);
275289
}
276290
if (options.qs) {
277291
this._fixReplyMarkup(options.qs);
292+
this._fixReplyParameters(options.qs);
278293
}
279294

280295
options.method = 'POST';
@@ -2441,7 +2456,7 @@ class TelegramBot extends EventEmitter {
24412456
* - If the bot is an administrator of a group, it can delete any message there.
24422457
* - If the bot has `can_delete_messages` permission in a supergroup, it can delete any message there.
24432458
*
2444-
* @param {Number|String} chatId Unique identifier of the target chat
2459+
* @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
24452460
* @param {Number} messageId Unique identifier of the target message
24462461
* @param {Object} [options] Additional Telegram query options
24472462
* @return {Promise} True on success
@@ -2951,6 +2966,58 @@ class TelegramBot extends EventEmitter {
29512966
form.user_id = userId;
29522967
return this._request('getGameHighScores', { form });
29532968
}
2969+
2970+
/**
2971+
* Use this method to change the chosen reactions on a message.
2972+
* - Service messages can't be reacted to.
2973+
* - Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel.
2974+
* - In albums, bots must react to the first message.
2975+
*
2976+
* @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2977+
* @param {Number} messageId Unique identifier of the target message
2978+
* @param {Object} [options] Additional Telegram query options
2979+
* @return {Promise<Boolean>} True on success
2980+
* @see https://core.telegram.org/bots/api#setMessageReaction
2981+
*/
2982+
setMessageReaction(chatId, messageId, form = {}) {
2983+
form.chat_id = chatId;
2984+
form.message_id = messageId;
2985+
form.reaction = stringify(form.reaction);
2986+
return this._request('setMessageReaction', { form });
2987+
}
2988+
2989+
/**
2990+
* Use this method to delete multiple messages simultaneously. If some of the specified messages can't be found, they are skipped.
2991+
*
2992+
* @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2993+
* @param {Array<Number|String>} messageIds Identifiers of 1-100 messages to delete. See deleteMessage for limitations on which messages can be deleted
2994+
* @param {Object} [options] Additional Telegram query options
2995+
* @return {Promise<Boolean>} True on success
2996+
* @see https://core.telegram.org/bots/api#deleteMessages
2997+
*/
2998+
deleteMessages(chatId, messageIds, form = {}) {
2999+
form.chat_id = chatId;
3000+
form.message_ids = stringify(messageIds);
3001+
return this._request('deleteMessages', { form });
3002+
}
3003+
3004+
/**
3005+
* Use this method to copy messages of any kind. If some of the specified messages can't be found or copied, they are skipped. Service messages, giveaway messages, giveaway winners messages, and invoice messages can't be copied. A quiz poll can be copied only if the value of the field correct_option_id is known to the bot. The method is analogous to the method forwardMessages, but the copied messages don't have a link to the original message. Album grouping is kept for copied messages.
3006+
*
3007+
* @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
3008+
* @param {Number|String} fromChatId Unique identifier for the chat where the
3009+
* original message was sent (or channel username in the format `@channelusername`)
3010+
* @param {Array<Number|String>} messageIds Identifiers of 1-100 messages in the chat from_chat_id to copy. The identifiers must be specified in a strictly increasing order.
3011+
* @param {Object} [options] Additional Telegram query options
3012+
* @return {Promise<Array<TelegramBot.MessageId>>} On success, an array of MessageId of the sent messages is returned.
3013+
* @see https://core.telegram.org/bots/api#copyMessages
3014+
*/
3015+
copyMessages(chatId, from_chat_id, messageIds, form = {}) {
3016+
form.chat_id = chatId;
3017+
form.from_chat_id = from_chat_id;
3018+
form.message_ids = stringify(messageIds);
3019+
return this._request('copyMessages', { form });
3020+
}
29543021
}
29553022

29563023
module.exports = TelegramBot;

test/telegram.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,4 +2048,50 @@ describe('TelegramBot', function telegramSuite() {
20482048
});
20492049
});
20502050
});
2051+
2052+
describe('#setMessageReaction', function setMessageReactionSuite() {
2053+
let messageId;
2054+
const Reactions = [{ type: 'emoji', emoji: '👍' }];
2055+
before(function before() {
2056+
utils.handleRatelimit(bot, 'setMessageReaction', this);
2057+
return bot.sendMessage(USERID, 'To be reacted').then(resp => {
2058+
messageId = resp.message_id;
2059+
});
2060+
});
2061+
it('should add reactions to message', function test() {
2062+
return bot.setMessageReaction(USERID, messageId, {reaction: Reactions, is_big: true }).then(resp => {
2063+
assert.strictEqual(resp, true);
2064+
});
2065+
});
2066+
});
2067+
2068+
describe('#deleteMessages', function setMessageReactionSuite() {
2069+
let messageId;
2070+
before(function before() {
2071+
utils.handleRatelimit(bot, 'deleteMessages', this);
2072+
return bot.sendMessage(USERID, 'To be deleted').then(resp => {
2073+
messageId = resp.message_id;
2074+
});
2075+
});
2076+
it('should delete message from array', function test() {
2077+
return bot.deleteMessages(USERID, [messageId]).then(resp => {
2078+
assert.strictEqual(resp, true);
2079+
});
2080+
});
2081+
});
2082+
2083+
describe('#copyMessages', function setMessageReactionSuite() {
2084+
let messageId;
2085+
before(function before() {
2086+
utils.handleRatelimit(bot, 'copyMessages', this);
2087+
return bot.sendMessage(USERID, 'To be copyed').then(resp => {
2088+
messageId = resp.message_id;
2089+
});
2090+
});
2091+
it('should copy messages from array', function test() {
2092+
return bot.copyMessages(USERID, [messageId]).then(resp => {
2093+
assert.strictEqual(resp, true);
2094+
});
2095+
});
2096+
});
20512097
}); // End Telegram

0 commit comments

Comments
 (0)