mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-22 09:57:10 +00:00
src/telegram: Add TelegramBot#removeReplyListener()
Feature: Please see the updated API Reference. References: * Author: @githugger (Frederic Schneider <fschneider1992@gmail.com>) * Original PR: https://github.com/yagop/node-telegram-bot-api/pull/74
This commit is contained in:
parent
0441c99b97
commit
79de62a96e
@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
Added:
|
||||
|
||||
1. Add methods:
|
||||
* *TelegramBot#removeReplyListener()* (by @githugger)
|
||||
1. Add health-check endpoint (by @mironov)
|
||||
* `options.webHook.healthEndpoint`
|
||||
1. Use *String#indexOf()*, instead of *RegExp#test()*, to
|
||||
|
22
doc/api.md
22
doc/api.md
@ -47,7 +47,8 @@ TelegramBot
|
||||
* [.getFileLink(fileId)](#TelegramBot+getFileLink) ⇒ <code>Promise</code>
|
||||
* [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ <code>Promise</code>
|
||||
* [.onText(regexp, callback)](#TelegramBot+onText)
|
||||
* [.onReplyToMessage(chatId, messageId, callback)](#TelegramBot+onReplyToMessage)
|
||||
* [.onReplyToMessage(chatId, messageId, callback)](#TelegramBot+onReplyToMessage) ⇒ <code>Number</code>
|
||||
* [.removeReplyListener(replyListenerId)](#TelegramBot+removeReplyListener) ⇒ <code>Object</code>
|
||||
* [.getChat(chatId)](#TelegramBot+getChat) ⇒ <code>Promise</code>
|
||||
* [.getChatAdministrators(chatId)](#TelegramBot+getChatAdministrators) ⇒ <code>Promise</code>
|
||||
* [.getChatMembersCount(chatId)](#TelegramBot+getChatMembersCount) ⇒ <code>Promise</code>
|
||||
@ -594,16 +595,31 @@ Register a RegExp to test against an incomming text message.
|
||||
|
||||
<a name="TelegramBot+onReplyToMessage"></a>
|
||||
|
||||
### telegramBot.onReplyToMessage(chatId, messageId, callback)
|
||||
### telegramBot.onReplyToMessage(chatId, messageId, callback) ⇒ <code>Number</code>
|
||||
Register a reply to wait for a message response.
|
||||
|
||||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
||||
**Returns**: <code>Number</code> - id The ID of the inserted reply listener.
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| chatId | <code>Number</code> | <code>String</code> | The chat id where the message cames from. |
|
||||
| messageId | <code>Number</code> | <code>String</code> | The message id to be replied. |
|
||||
| callback | <code>function</code> | Callback will be called with the reply message. |
|
||||
| callback | <code>function</code> | Callback will be called with the reply message. |
|
||||
|
||||
<a name="TelegramBot+removeReplyListener"></a>
|
||||
|
||||
### telegramBot.removeReplyListener(replyListenerId) ⇒ <code>Object</code>
|
||||
Removes a reply that has been prev. registered for a message response.
|
||||
|
||||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
||||
**Returns**: <code>Object</code> - deletedListener The removed reply listener if
|
||||
found. This object has `id`, `chatId`, `messageId` and `callback`
|
||||
properties. If not found, returns `null`.
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| replyListenerId | <code>Number</code> | The ID of the reply listener. |
|
||||
|
||||
<a name="TelegramBot+getChat"></a>
|
||||
|
||||
|
@ -79,7 +79,8 @@ class TelegramBot extends EventEmitter {
|
||||
this.options.baseApiUrl = options.baseApiUrl || 'https://api.telegram.org';
|
||||
this.options.filepath = (typeof options.filepath === 'undefined') ? true : options.filepath;
|
||||
this._textRegexpCallbacks = [];
|
||||
this._onReplyToMessages = [];
|
||||
this._replyListenerId = 0;
|
||||
this._replyListeners = [];
|
||||
this._polling = null;
|
||||
this._webHook = null;
|
||||
|
||||
@ -483,7 +484,7 @@ class TelegramBot extends EventEmitter {
|
||||
}
|
||||
if (message.reply_to_message) {
|
||||
// Only callbacks waiting for this message
|
||||
this._onReplyToMessages.forEach(reply => {
|
||||
this._replyListeners.forEach(reply => {
|
||||
// Message from the same chat
|
||||
if (reply.chatId === message.chat.id) {
|
||||
// Responding to that message
|
||||
@ -1011,14 +1012,35 @@ class TelegramBot extends EventEmitter {
|
||||
* @param {Number|String} chatId The chat id where the message cames from.
|
||||
* @param {Number|String} messageId The message id to be replied.
|
||||
* @param {Function} callback Callback will be called with the reply
|
||||
* message.
|
||||
* message.
|
||||
* @return {Number} id The ID of the inserted reply listener.
|
||||
*/
|
||||
onReplyToMessage(chatId, messageId, callback) {
|
||||
this._onReplyToMessages.push({
|
||||
const id = ++this._replyListenerId;
|
||||
this._replyListeners.push({
|
||||
id,
|
||||
chatId,
|
||||
messageId,
|
||||
callback
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a reply that has been prev. registered for a message response.
|
||||
* @param {Number} replyListenerId The ID of the reply listener.
|
||||
* @return {Object} deletedListener The removed reply listener if
|
||||
* found. This object has `id`, `chatId`, `messageId` and `callback`
|
||||
* properties. If not found, returns `null`.
|
||||
*/
|
||||
removeReplyListener(replyListenerId) {
|
||||
const index = this._replyListeners.findIndex((replyListener) => {
|
||||
return replyListener.id === replyListenerId;
|
||||
});
|
||||
if (index === -1) {
|
||||
return null;
|
||||
}
|
||||
return this._replyListeners.splice(index, 1)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -890,6 +890,24 @@ describe('TelegramBot', function telegramSuite() {
|
||||
|
||||
describe.skip('#onReplyToMessage', function onReplyToMessageSuite() {});
|
||||
|
||||
describe('#removeReplyListener', function removeReplyListenerSuite() {
|
||||
const chatId = -1234;
|
||||
const messageId = 1;
|
||||
const callback = function noop() {};
|
||||
it('returns the right reply-listener', function test() {
|
||||
const id = bot.onReplyToMessage(chatId, messageId, callback);
|
||||
const replyListener = bot.removeReplyListener(id);
|
||||
assert.equal(id, replyListener.id);
|
||||
assert.equal(chatId, replyListener.chatId);
|
||||
assert.equal(messageId, replyListener.messageId);
|
||||
assert.equal(callback, replyListener.callback);
|
||||
});
|
||||
it('returns `null` if missing', function test() {
|
||||
// NOTE: '0' is never a valid reply listener ID :)
|
||||
assert.equal(null, bot.removeReplyListener(0));
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getChat', function getChatSuite() {
|
||||
before(function before() {
|
||||
utils.handleRatelimit(bot, 'getChat', this);
|
||||
|
Loading…
x
Reference in New Issue
Block a user