mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-22 18:07:16 +00:00
feat: Add methods deleteMessages and copyMessages
* Added setMessageReaction method * Added setMessageReaction method * Added and implemented _fixReplyParameters --------- Co-authored-by: Sp3rick <your_email@example.com>
This commit is contained in:
parent
e81ec60377
commit
cceea22a93
54
doc/api.md
54
doc/api.md
@ -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>[ 'Promise' ].<Boolean></code>
|
||||
* [.deleteMessages(chatId, messageIds, [options])](#TelegramBot+deleteMessages) ⇒ <code>[ 'Promise' ].<Boolean></code>
|
||||
* [.copyMessages(chatId, fromChatId, messageIds, [options])](#TelegramBot+copyMessages) ⇒ <code>[ 'Promise' ].<Array.<TelegramBot.MessageId>></code>
|
||||
* _static_
|
||||
* [.errors](#TelegramBot.errors) : <code>Object</code>
|
||||
* [.messageTypes](#TelegramBot.messageTypes) : <code>[ 'Array' ].<String></code>
|
||||
@ -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 |
|
||||
|
||||
@ -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>[ 'Promise' ].<Boolean></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>[ 'Promise' ].<Boolean></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>[ 'Promise' ].<Boolean></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>[ 'Promise' ].<Boolean></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>[ 'Array' ].<(Number\|String)></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>[ 'Promise' ].<Array.<TelegramBot.MessageId>></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>[ 'Promise' ].<Array.<TelegramBot.MessageId>></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>[ 'Array' ].<(Number\|String)></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>
|
||||
|
@ -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
|
||||
@ -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';
|
||||
@ -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
|
||||
@ -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;
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user