mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-30 05:48:00 +00:00
src/telegram: Add TelegramBot#removeTextListener()
Feature: This is the opposite action to `TelegramBot#onText()`. It allows removing any previously-registered listeners. It is similar to `TelegramBot#removeReplyListener()`.
This commit is contained in:
parent
9f3107b5ab
commit
11bcdd3b6a
15
doc/api.md
15
doc/api.md
@ -49,6 +49,7 @@ TelegramBot
|
||||
* [.getFileLink(fileId)](#TelegramBot+getFileLink) ⇒ <code>Promise</code>
|
||||
* [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ <code>Promise</code>
|
||||
* [.onText(regexp, callback)](#TelegramBot+onText)
|
||||
* [.removeTextListener(regexp)](#TelegramBot+removeTextListener) ⇒ <code>Object</code>
|
||||
* [.onReplyToMessage(chatId, messageId, callback)](#TelegramBot+onReplyToMessage) ⇒ <code>Number</code>
|
||||
* [.removeReplyListener(replyListenerId)](#TelegramBot+removeReplyListener) ⇒ <code>Object</code>
|
||||
* [.getChat(chatId)](#TelegramBot+getChat) ⇒ <code>Promise</code>
|
||||
@ -597,6 +598,20 @@ Register a RegExp to test against an incomming text message.
|
||||
| regexp | <code>RegExp</code> | RegExp to be executed with `exec`. |
|
||||
| callback | <code>function</code> | Callback will be called with 2 parameters, the `msg` and the result of executing `regexp.exec` on message text. |
|
||||
|
||||
<a name="TelegramBot+removeTextListener"></a>
|
||||
|
||||
### telegramBot.removeTextListener(regexp) ⇒ <code>Object</code>
|
||||
Remove a listener registered with `onText()`.
|
||||
|
||||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
||||
**Returns**: <code>Object</code> - deletedListener The removed reply listener if
|
||||
found. This object has `regexp` and `callback`
|
||||
properties. If not found, returns `null`.
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| regexp | <code>RegExp</code> | RegExp used previously in `onText()` |
|
||||
|
||||
<a name="TelegramBot+onReplyToMessage"></a>
|
||||
|
||||
### telegramBot.onReplyToMessage(chatId, messageId, callback) ⇒ <code>Number</code>
|
||||
|
@ -1006,6 +1006,23 @@ class TelegramBot extends EventEmitter {
|
||||
this._textRegexpCallbacks.push({ regexp, callback });
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a listener registered with `onText()`.
|
||||
* @param {RegExp} regexp RegExp used previously in `onText()`
|
||||
* @return {Object} deletedListener The removed reply listener if
|
||||
* found. This object has `regexp` and `callback`
|
||||
* properties. If not found, returns `null`.
|
||||
*/
|
||||
removeTextListener(regexp) {
|
||||
const index = this._textRegexpCallbacks.findIndex((textListener) => {
|
||||
return textListener.regexp === regexp;
|
||||
});
|
||||
if (index === -1) {
|
||||
return null;
|
||||
}
|
||||
return this._textRegexpCallbacks.splice(index, 1)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a reply to wait for a message response.
|
||||
* @param {Number|String} chatId The chat id where the message cames from.
|
||||
|
@ -987,6 +987,28 @@ describe('TelegramBot', function telegramSuite() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#removeTextListener', function removeTextListenerSuite() {
|
||||
const regexp = /\/onText/;
|
||||
const regexp2 = /\/onText/;
|
||||
const callback = function noop() {};
|
||||
after(function after() {
|
||||
bot.removeTextListener(regexp);
|
||||
bot.removeTextListener(regexp2);
|
||||
});
|
||||
it('removes the right text-listener', function test() {
|
||||
bot.onText(regexp, callback);
|
||||
bot.onText(regexp2, callback);
|
||||
const textListener = bot.removeTextListener(regexp);
|
||||
assert.equal(regexp, textListener.regexp);
|
||||
assert.equal(callback, textListener.callback);
|
||||
assert.notEqual(regexp2, textListener.regexp);
|
||||
assert.equal(null, bot.removeTextListener(regexp));
|
||||
});
|
||||
it('returns `null` if missing', function test() {
|
||||
assert.equal(null, bot.removeTextListener(/404/));
|
||||
});
|
||||
});
|
||||
|
||||
describe.skip('#onReplyToMessage', function onReplyToMessageSuite() {});
|
||||
|
||||
describe('#removeReplyListener', function removeReplyListenerSuite() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user