2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-29 05:17:41 +00:00

Merge pull request #117 from w-site/bug/#116-get-409-error

Fix: If use switch between webHook or Polling you get an 409 (http://telegram.wiki/bots:boterrorcode) telegram error. #116
This commit is contained in:
Yago 2016-06-07 00:28:05 +02:00
commit b28f30a2d2

View File

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