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 => {