mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-22 18:07:16 +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:
|
Added:
|
||||||
|
|
||||||
|
1. Add methods:
|
||||||
|
* *TelegramBot#removeReplyListener()* (by @githugger)
|
||||||
1. Add health-check endpoint (by @mironov)
|
1. Add health-check endpoint (by @mironov)
|
||||||
* `options.webHook.healthEndpoint`
|
* `options.webHook.healthEndpoint`
|
||||||
1. Use *String#indexOf()*, instead of *RegExp#test()*, to
|
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>
|
* [.getFileLink(fileId)](#TelegramBot+getFileLink) ⇒ <code>Promise</code>
|
||||||
* [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ <code>Promise</code>
|
* [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ <code>Promise</code>
|
||||||
* [.onText(regexp, callback)](#TelegramBot+onText)
|
* [.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>
|
* [.getChat(chatId)](#TelegramBot+getChat) ⇒ <code>Promise</code>
|
||||||
* [.getChatAdministrators(chatId)](#TelegramBot+getChatAdministrators) ⇒ <code>Promise</code>
|
* [.getChatAdministrators(chatId)](#TelegramBot+getChatAdministrators) ⇒ <code>Promise</code>
|
||||||
* [.getChatMembersCount(chatId)](#TelegramBot+getChatMembersCount) ⇒ <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>
|
<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.
|
Register a reply to wait for a message response.
|
||||||
|
|
||||||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
||||||
|
**Returns**: <code>Number</code> - id The ID of the inserted reply listener.
|
||||||
|
|
||||||
| Param | Type | Description |
|
| Param | Type | Description |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| chatId | <code>Number</code> | <code>String</code> | The chat id where the message cames from. |
|
| 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. |
|
| 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>
|
<a name="TelegramBot+getChat"></a>
|
||||||
|
|
||||||
|
@ -79,7 +79,8 @@ class TelegramBot extends EventEmitter {
|
|||||||
this.options.baseApiUrl = options.baseApiUrl || 'https://api.telegram.org';
|
this.options.baseApiUrl = options.baseApiUrl || 'https://api.telegram.org';
|
||||||
this.options.filepath = (typeof options.filepath === 'undefined') ? true : options.filepath;
|
this.options.filepath = (typeof options.filepath === 'undefined') ? true : options.filepath;
|
||||||
this._textRegexpCallbacks = [];
|
this._textRegexpCallbacks = [];
|
||||||
this._onReplyToMessages = [];
|
this._replyListenerId = 0;
|
||||||
|
this._replyListeners = [];
|
||||||
this._polling = null;
|
this._polling = null;
|
||||||
this._webHook = null;
|
this._webHook = null;
|
||||||
|
|
||||||
@ -483,7 +484,7 @@ class TelegramBot extends EventEmitter {
|
|||||||
}
|
}
|
||||||
if (message.reply_to_message) {
|
if (message.reply_to_message) {
|
||||||
// Only callbacks waiting for this message
|
// Only callbacks waiting for this message
|
||||||
this._onReplyToMessages.forEach(reply => {
|
this._replyListeners.forEach(reply => {
|
||||||
// Message from the same chat
|
// Message from the same chat
|
||||||
if (reply.chatId === message.chat.id) {
|
if (reply.chatId === message.chat.id) {
|
||||||
// Responding to that message
|
// 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} chatId The chat id where the message cames from.
|
||||||
* @param {Number|String} messageId The message id to be replied.
|
* @param {Number|String} messageId The message id to be replied.
|
||||||
* @param {Function} callback Callback will be called with the reply
|
* @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) {
|
onReplyToMessage(chatId, messageId, callback) {
|
||||||
this._onReplyToMessages.push({
|
const id = ++this._replyListenerId;
|
||||||
|
this._replyListeners.push({
|
||||||
|
id,
|
||||||
chatId,
|
chatId,
|
||||||
messageId,
|
messageId,
|
||||||
callback
|
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.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() {
|
describe('#getChat', function getChatSuite() {
|
||||||
before(function before() {
|
before(function before() {
|
||||||
utils.handleRatelimit(bot, 'getChat', this);
|
utils.handleRatelimit(bot, 'getChat', this);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user