Skip to content

Commit

Permalink
src/telegram: Add TelegramBot#removeReplyListener()
Browse files Browse the repository at this point in the history
Feature:

  Please see the updated API Reference.

References:

  * Author: @githugger (Frederic Schneider <[email protected]>)
  * Original PR: yagop/node-telegram-bot-api#74
  • Loading branch information
passion-27 committed Feb 9, 2017
1 parent b332311 commit 1006aa8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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> &#124; <code>String</code> | The chat id where the message cames from. |
| messageId | <code>Number</code> &#124; <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>

Expand Down
30 changes: 26 additions & 4 deletions src/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];
}

/**
Expand Down
18 changes: 18 additions & 0 deletions test/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1006aa8

Please sign in to comment.