From 130f6940ceb9972a7b0ac0636ac16e7e53984a46 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Fri, 10 Feb 2017 12:40:47 +0300 Subject: [PATCH] src/polling: Fix bug #284 Bug: During polling, deleting the already-set webhook, caused the `TelegramBotPolling#_getUpdates()` return an unexpected value. We expect the method to return an array (in the `.then()` clause). However, deleting the webhook returns its value, which is an object, from the method `_getUpdates()`. Fix: Simply retry the polling request and return the promise. Notes: Should we use recursion? I do not think so. Why? The chances of getting the error (having a webhook set) AGAIN is quite rare. And if it happens, there must be some problem with different instances invoking polling and webhook simultaneously. In that case, we wont struggle to recover from such a scenario. User is on their own! Isht! References: * Bug report: https://github.com/yagop/node-telegram-bot-api/issues/284 * Reported by: @dcparga --- src/telegramPolling.js | 5 ++++- test/telegram.js | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/telegramPolling.js b/src/telegramPolling.js index 43ed751..abd4a7c 100644 --- a/src/telegramPolling.js +++ b/src/telegramPolling.js @@ -125,6 +125,7 @@ class TelegramBotPolling { * @private */ _unsetWebHook() { + debug('unsetting webhook'); return this.bot._request('setWebHook'); } @@ -136,7 +137,9 @@ class TelegramBotPolling { return this.bot.getUpdates(this.options.params) .catch(err => { if (err.response && err.response.statusCode === ANOTHER_WEB_HOOK_USED) { - return this._unsetWebHook(); + return this._unsetWebHook().then(() => { + return this.bot.getUpdates(this.options.params); + }); } throw err; }); diff --git a/test/telegram.js b/test/telegram.js index a099c97..288fe02 100644 --- a/test/telegram.js +++ b/test/telegram.js @@ -30,6 +30,7 @@ const webHookPort2 = portindex++; const badTgServerPort = portindex++; const staticUrl = `http://127.0.0.1:${staticPort}`; const key = `${__dirname}/../examples/key.pem`; +const ip = '216.58.210.174'; // Google IP ¯\_(ツ)_/¯ const cert = `${__dirname}/../examples/crt.pem`; let FILE_ID; let GAME_CHAT_ID; @@ -122,6 +123,21 @@ describe('TelegramBot', function telegramSuite() { return utils.hasOpenWebHook(webHookPort, true); }); + it('correctly deletes the webhook if polling', function test() { + const myBot = new TelegramBot(TOKEN, { + polling: { autoStart: false, params: { timeout: 0 } }, + }); + utils.handleRatelimit(myBot, 'setWebHook', this); + myBot.on('polling_error', (error) => { + assert.ifError(error); + }); + return myBot.setWebHook(ip).then(() => { + return myBot.startPolling(); + }).then(() => { + return myBot.stopPolling(); + }); + }); + describe('Events', function eventsSuite() { it('(polling) emits "message" on receiving message', function test(done) { botPolling.once('message', () => { @@ -353,12 +369,10 @@ describe('TelegramBot', function telegramSuite() { }); describe('#setWebHook', function setWebHookSuite() { - const ip = '216.58.210.174'; before(function before() { utils.handleRatelimit(bot, 'setWebHook', this); }); it('should set a webHook', function test() { - // Google IP ¯\_(ツ)_/¯ return bot .setWebHook(ip) .then(resp => {