Skip to content

Commit

Permalink
src/polling: Fix bug #284
Browse files Browse the repository at this point in the history
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:   yagop/node-telegram-bot-api#284
  * Reported by:  @dcparga
  • Loading branch information
AILeaderCom committed Feb 10, 2017
1 parent 4dac5b1 commit 625b2fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/telegramPolling.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class TelegramBotPolling {
* @private
*/
_unsetWebHook() {
debug('unsetting webhook');
return this.bot._request('setWebHook');
}

Expand All @@ -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;
});
Expand Down
18 changes: 16 additions & 2 deletions test/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 625b2fe

Please sign in to comment.