diff --git a/CHANGELOG.md b/CHANGELOG.md
index 908382f..f32fb43 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
diff --git a/doc/api.md b/doc/api.md
index 5a08492..2112e5f 100644
--- a/doc/api.md
+++ b/doc/api.md
@@ -47,7 +47,8 @@ TelegramBot
* [.getFileLink(fileId)](#TelegramBot+getFileLink) ⇒ Promise
* [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ Promise
* [.onText(regexp, callback)](#TelegramBot+onText)
- * [.onReplyToMessage(chatId, messageId, callback)](#TelegramBot+onReplyToMessage)
+ * [.onReplyToMessage(chatId, messageId, callback)](#TelegramBot+onReplyToMessage) ⇒ Number
+ * [.removeReplyListener(replyListenerId)](#TelegramBot+removeReplyListener) ⇒ Object
* [.getChat(chatId)](#TelegramBot+getChat) ⇒ Promise
* [.getChatAdministrators(chatId)](#TelegramBot+getChatAdministrators) ⇒ Promise
* [.getChatMembersCount(chatId)](#TelegramBot+getChatMembersCount) ⇒ Promise
@@ -594,16 +595,31 @@ Register a RegExp to test against an incomming text message.
-### telegramBot.onReplyToMessage(chatId, messageId, callback)
+### telegramBot.onReplyToMessage(chatId, messageId, callback) ⇒ Number
Register a reply to wait for a message response.
**Kind**: instance method of [TelegramBot](#TelegramBot)
+**Returns**: Number
- id The ID of the inserted reply listener.
| Param | Type | Description |
| --- | --- | --- |
| chatId | Number
| String
| The chat id where the message cames from. |
| messageId | Number
| String
| The message id to be replied. |
-| callback | function
| Callback will be called with the reply message. |
+| callback | function
| Callback will be called with the reply message. |
+
+
+
+### telegramBot.removeReplyListener(replyListenerId) ⇒ Object
+Removes a reply that has been prev. registered for a message response.
+
+**Kind**: instance method of [TelegramBot](#TelegramBot)
+**Returns**: Object
- 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 | Number
| The ID of the reply listener. |
diff --git a/src/telegram.js b/src/telegram.js
index ffbb419..405131d 100644
--- a/src/telegram.js
+++ b/src/telegram.js
@@ -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];
}
/**
diff --git a/test/telegram.js b/test/telegram.js
index 6cd243c..a509376 100644
--- a/test/telegram.js
+++ b/test/telegram.js
@@ -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);