2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-22 18:07:16 +00:00

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
This commit is contained in:
GochoMugo 2017-02-10 12:40:47 +03:00
parent 9a9dfa9560
commit 130f6940ce
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
2 changed files with 20 additions and 3 deletions

View File

@ -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;
});

View File

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