diff --git a/doc/api.md b/doc/api.md index 5a19278..a5f7a68 100644 --- a/doc/api.md +++ b/doc/api.md @@ -49,6 +49,7 @@ TelegramBot * [.getFileLink(fileId)](#TelegramBot+getFileLink) ⇒ Promise * [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ Promise * [.onText(regexp, callback)](#TelegramBot+onText) + * [.removeTextListener(regexp)](#TelegramBot+removeTextListener) ⇒ Object * [.onReplyToMessage(chatId, messageId, callback)](#TelegramBot+onReplyToMessage) ⇒ Number * [.removeReplyListener(replyListenerId)](#TelegramBot+removeReplyListener) ⇒ Object * [.getChat(chatId)](#TelegramBot+getChat) ⇒ Promise @@ -597,6 +598,20 @@ Register a RegExp to test against an incomming text message. | regexp | RegExp | RegExp to be executed with `exec`. | | callback | function | Callback will be called with 2 parameters, the `msg` and the result of executing `regexp.exec` on message text. | + + +### telegramBot.removeTextListener(regexp) ⇒ Object +Remove a listener registered with `onText()`. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**Returns**: Object - deletedListener The removed reply listener if + found. This object has `regexp` and `callback` + properties. If not found, returns `null`. + +| Param | Type | Description | +| --- | --- | --- | +| regexp | RegExp | RegExp used previously in `onText()` | + ### telegramBot.onReplyToMessage(chatId, messageId, callback) ⇒ Number diff --git a/src/telegram.js b/src/telegram.js index d4ffa4f..ecf333e 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -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. diff --git a/test/telegram.js b/test/telegram.js index d29ee0d..6b2e852 100644 --- a/test/telegram.js +++ b/test/telegram.js @@ -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() {