Skip to content

Commit

Permalink
feat: Add methods deleteMessages and copyMessages
Browse files Browse the repository at this point in the history
* Added setMessageReaction method
* Added setMessageReaction method

* Added and implemented _fixReplyParameters

---------

Co-authored-by: Sp3rick <[email protected]>
  • Loading branch information
Sp3rick and Sp3rick authored Feb 20, 2024
1 parent e81ec60 commit cceea22
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 2 deletions.
54 changes: 53 additions & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ TelegramBot
* [.sendGame(chatId, gameShortName, [options])](#TelegramBot+sendGame) ⇒ <code>Promise</code>
* [.setGameScore(userId, score, [options])](#TelegramBot+setGameScore) ⇒ <code>Promise</code>
* [.getGameHighScores(userId, [options])](#TelegramBot+getGameHighScores) ⇒ <code>Promise</code>
* [.setMessageReaction(chatId, messageId, [options])](#TelegramBot+setMessageReaction) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code>
* [.deleteMessages(chatId, messageIds, [options])](#TelegramBot+deleteMessages) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code>
* [.copyMessages(chatId, fromChatId, messageIds, [options])](#TelegramBot+copyMessages) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Array.&lt;TelegramBot.MessageId&gt;&gt;</code>
* _static_
* [.errors](#TelegramBot.errors) : <code>Object</code>
* [.messageTypes](#TelegramBot.messageTypes) : <code>[ &#x27;Array&#x27; ].&lt;String&gt;</code>
Expand Down Expand Up @@ -1918,7 +1921,7 @@ Use this method to delete a message, including service messages, with the follow

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

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

<a name="TelegramBot+setMessageReaction"></a>

### telegramBot.setMessageReaction(chatId, messageId, [options]) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code>
Use this method to change the chosen reactions on a message.
- Service messages can't be reacted to.
- Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel.
- In albums, bots must react to the first message.

**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**Returns**: <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code> - True on success
**See**: https://core.telegram.org/bots/api#setMessageReaction

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

<a name="TelegramBot+deleteMessages"></a>

### telegramBot.deleteMessages(chatId, messageIds, [options]) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code>
Use this method to delete multiple messages simultaneously. If some of the specified messages can't be found, they are skipped.

**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**Returns**: <code>[ &#x27;Promise&#x27; ].&lt;Boolean&gt;</code> - True on success
**See**: https://core.telegram.org/bots/api#deleteMessages

| Param | Type | Description |
| --- | --- | --- |
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier for the target chat or username of the target channel (in the format @channelusername) |
| 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 |
| [options] | <code>Object</code> | Additional Telegram query options |

<a name="TelegramBot+copyMessages"></a>

### telegramBot.copyMessages(chatId, fromChatId, messageIds, [options]) ⇒ <code>[ &#x27;Promise&#x27; ].&lt;Array.&lt;TelegramBot.MessageId&gt;&gt;</code>
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.

**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**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.
**See**: https://core.telegram.org/bots/api#copyMessages

| Param | Type | Description |
| --- | --- | --- |
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier for the target chat or username of the target channel (in the format @channelusername) |
| 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`) |
| 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. |
| [options] | <code>Object</code> | Additional Telegram query options |

<a name="TelegramBot.errors"></a>

### TelegramBot.errors : <code>Object</code>
Expand Down
69 changes: 68 additions & 1 deletion src/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ class TelegramBot extends EventEmitter {
}
}

/**
* Fix 'reply_parameters' parameter by making it JSON-serialized, as
* required by the Telegram Bot API
* @param {Object} obj Object; either 'form' or 'qs'
* @private
* @see https://core.telegram.org/bots/api#sendmessage
*/
_fixReplyParameters(obj) {
if (obj.hasOwnProperty('reply_parameters') && typeof obj.reply_parameters !== 'string') {
obj.reply_parameters = stringify(obj.reply_parameters);
}
}

/**
* Make request against the API
* @param {String} _path API endpoint
Expand All @@ -272,9 +285,11 @@ class TelegramBot extends EventEmitter {
if (options.form) {
this._fixReplyMarkup(options.form);
this._fixEntitiesField(options.form);
this._fixReplyParameters(options.form);
}
if (options.qs) {
this._fixReplyMarkup(options.qs);
this._fixReplyParameters(options.qs);
}

options.method = 'POST';
Expand Down Expand Up @@ -2441,7 +2456,7 @@ class TelegramBot extends EventEmitter {
* - If the bot is an administrator of a group, it can delete any message there.
* - If the bot has `can_delete_messages` permission in a supergroup, it can delete any message there.
*
* @param {Number|String} chatId Unique identifier of the target chat
* @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param {Number} messageId Unique identifier of the target message
* @param {Object} [options] Additional Telegram query options
* @return {Promise} True on success
Expand Down Expand Up @@ -2951,6 +2966,58 @@ class TelegramBot extends EventEmitter {
form.user_id = userId;
return this._request('getGameHighScores', { form });
}

/**
* Use this method to change the chosen reactions on a message.
* - Service messages can't be reacted to.
* - Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel.
* - In albums, bots must react to the first message.
*
* @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param {Number} messageId Unique identifier of the target message
* @param {Object} [options] Additional Telegram query options
* @return {Promise<Boolean>} True on success
* @see https://core.telegram.org/bots/api#setMessageReaction
*/
setMessageReaction(chatId, messageId, form = {}) {
form.chat_id = chatId;
form.message_id = messageId;
form.reaction = stringify(form.reaction);
return this._request('setMessageReaction', { form });
}

/**
* Use this method to delete multiple messages simultaneously. If some of the specified messages can't be found, they are skipped.
*
* @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param {Array<Number|String>} messageIds Identifiers of 1-100 messages to delete. See deleteMessage for limitations on which messages can be deleted
* @param {Object} [options] Additional Telegram query options
* @return {Promise<Boolean>} True on success
* @see https://core.telegram.org/bots/api#deleteMessages
*/
deleteMessages(chatId, messageIds, form = {}) {
form.chat_id = chatId;
form.message_ids = stringify(messageIds);
return this._request('deleteMessages', { form });
}

/**
* 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.
*
* @param {Number|String} chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param {Number|String} fromChatId Unique identifier for the chat where the
* original message was sent (or channel username in the format `@channelusername`)
* @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.
* @param {Object} [options] Additional Telegram query options
* @return {Promise<Array<TelegramBot.MessageId>>} On success, an array of MessageId of the sent messages is returned.
* @see https://core.telegram.org/bots/api#copyMessages
*/
copyMessages(chatId, from_chat_id, messageIds, form = {}) {
form.chat_id = chatId;
form.from_chat_id = from_chat_id;
form.message_ids = stringify(messageIds);
return this._request('copyMessages', { form });
}
}

module.exports = TelegramBot;
46 changes: 46 additions & 0 deletions test/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -2048,4 +2048,50 @@ describe('TelegramBot', function telegramSuite() {
});
});
});

describe('#setMessageReaction', function setMessageReactionSuite() {
let messageId;
const Reactions = [{ type: 'emoji', emoji: '👍' }];
before(function before() {
utils.handleRatelimit(bot, 'setMessageReaction', this);
return bot.sendMessage(USERID, 'To be reacted').then(resp => {
messageId = resp.message_id;
});
});
it('should add reactions to message', function test() {
return bot.setMessageReaction(USERID, messageId, {reaction: Reactions, is_big: true }).then(resp => {
assert.strictEqual(resp, true);
});
});
});

describe('#deleteMessages', function setMessageReactionSuite() {
let messageId;
before(function before() {
utils.handleRatelimit(bot, 'deleteMessages', this);
return bot.sendMessage(USERID, 'To be deleted').then(resp => {
messageId = resp.message_id;
});
});
it('should delete message from array', function test() {
return bot.deleteMessages(USERID, [messageId]).then(resp => {
assert.strictEqual(resp, true);
});
});
});

describe('#copyMessages', function setMessageReactionSuite() {
let messageId;
before(function before() {
utils.handleRatelimit(bot, 'copyMessages', this);
return bot.sendMessage(USERID, 'To be copyed').then(resp => {
messageId = resp.message_id;
});
});
it('should copy messages from array', function test() {
return bot.copyMessages(USERID, [messageId]).then(resp => {
assert.strictEqual(resp, true);
});
});
});
}); // End Telegram

0 comments on commit cceea22

Please sign in to comment.