From 17cb062c9642313f17c3c5e070de2032031857d3 Mon Sep 17 00:00:00 2001 From: Yago Date: Sat, 30 Jan 2016 18:02:09 +0100 Subject: [PATCH] onReplyToMessage --- src/telegram.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/telegram.js b/src/telegram.js index 3aea15a..cde44af 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -43,6 +43,7 @@ var TelegramBot = function (token, options) { 'new_chat_photo', 'delete_chat_photo', 'group_chat_created' ]; // Telegram message events this.textRegexpCallbacks = []; + this.onReplyToMessages = []; this.processUpdate = this._processUpdate.bind(this); @@ -92,6 +93,19 @@ TelegramBot.prototype._processUpdate = function (update) { } }); } + if (message.reply_to_message) { + // Only callbacks waiting for this message + this.onReplyToMessages.forEach(function (reply) { + // Message from the same chat + if (reply.chatId === message.chat.id) { + // Responding to that message + if (reply.messageId === message.reply_to_message.message_id) { + // Resolve the promise + reply.callback(message); + } + } + }); + } } else if (inline_query) { debug('Process Update inline_query %j', inline_query); this.emit('inline_query', inline_query); @@ -549,4 +563,18 @@ TelegramBot.prototype.onText = function (regexp, callback) { this.textRegexpCallbacks.push({regexp: regexp, callback: callback}); }; +/** + * Register a reply to wait for a message response. + * @param {Number|String} messageId The ID of the message to be replied + * @param {Function} callback Callback will be called with the reply + * message. + */ +TelegramBot.prototype.onReplyToMessage = function (chatId, messageId, callback) { + this.onReplyToMessages.push({ + chatId: chatId, + messageId: messageId, + callback: callback + }); +}; + module.exports = TelegramBot;