From 91dcb2b9d8c610dcd6c66b43cbd32ecd9de70616 Mon Sep 17 00:00:00 2001 From: sasali Date: Mon, 2 May 2016 12:39:02 -0700 Subject: [PATCH] Fix: If use switch between webHook or Polling you get an 409 (http://telegram.wiki/bots:boterrorcode) telegram error. --- src/telegramPolling.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/telegramPolling.js b/src/telegramPolling.js index 11e2eb6..f53874e 100644 --- a/src/telegramPolling.js +++ b/src/telegramPolling.js @@ -2,6 +2,7 @@ const Promise = require('bluebird'); const debug = require('debug')('node-telegram-bot-api'); const request = require('request-promise'); const URL = require('url'); +const ANOTHER_WEB_HOOK_USED = 409; class TelegramBotPolling { @@ -62,6 +63,25 @@ class TelegramBotPolling { } } + _unsetWebHook() { + return request({ + url: URL.format({ + protocol: 'https', + host: 'api.telegram.org', + pathname: `/bot${this.token}/setWebHook` + }), + simple: false, + resolveWithFullResponse: true + }) + .promise() + .then(resp => { + if (!resp) { + throw new Error(resp); + } + return []; + }); + } + _getUpdates() { const opts = { qs: { @@ -84,6 +104,10 @@ class TelegramBotPolling { .promise() .timeout((10 + this.timeout) * 1000) .then(resp => { + if (resp.statusCode === ANOTHER_WEB_HOOK_USED) { + return this._unsetWebHook(); + } + if (resp.statusCode !== 200) { throw new Error(`${resp.statusCode} ${resp.body}`); }