diff --git a/CHANGELOG.md b/CHANGELOG.md index cdbfb50..b80dca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). Added: +1. Add constructor options: + * `options.polling.params` (by @GochoMugo) 1. Add methods: * *TelegramBot#removeReplyListener()* (by @githugger) 1. Add proper error handling (by @GochoMugo) diff --git a/doc/api.md b/doc/api.md index 2112e5f..dd0d52b 100644 --- a/doc/api.md +++ b/doc/api.md @@ -71,9 +71,10 @@ Emits `message` when a message arrives. | token | String | | Bot Token | | [options] | Object | | | | [options.polling] | Boolean | Object | false | Set true to enable polling or set options. If a WebHook has been set, it will be deleted automatically. | -| [options.polling.timeout] | String | Number | 10 | Timeout in seconds for long polling | +| [options.polling.timeout] | String | Number | 10 | *Deprecated. Use `options.polling.params` instead*. Timeout in seconds for long polling. | | [options.polling.interval] | String | Number | 300 | Interval between requests in miliseconds | | [options.polling.autoStart] | Boolean | true | Start polling immediately | +| [options.polling.params] | Object | | Parameters to be used in polling API requests. See https://core.telegram.org/bots/api#getupdates for more information. | | [options.webHook] | Boolean | Object | false | Set true to enable WebHook or set options | | [options.webHook.host] | String | 0.0.0.0 | Host to bind to | | [options.webHook.port] | Number | 8443 | Port to bind to | diff --git a/src/telegram.js b/src/telegram.js index b44536b..5194180 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -51,9 +51,12 @@ class TelegramBot extends EventEmitter { * @param {Object} [options] * @param {Boolean|Object} [options.polling=false] Set true to enable polling or set options. * If a WebHook has been set, it will be deleted automatically. - * @param {String|Number} [options.polling.timeout=10] Timeout in seconds for long polling + * @param {String|Number} [options.polling.timeout=10] *Deprecated. Use `options.polling.params` instead*. + * Timeout in seconds for long polling. * @param {String|Number} [options.polling.interval=300] Interval between requests in miliseconds * @param {Boolean} [options.polling.autoStart=true] Start polling immediately + * @param {Object} [options.polling.params] Parameters to be used in polling API requests. + * See https://core.telegram.org/bots/api#getupdates for more information. * @param {Boolean|Object} [options.webHook=false] Set true to enable WebHook or set options * @param {String} [options.webHook.host=0.0.0.0] Host to bind to * @param {Number} [options.webHook.port=8443] Port to bind to diff --git a/src/telegramPolling.js b/src/telegramPolling.js index dafd217..bcc8311 100644 --- a/src/telegramPolling.js +++ b/src/telegramPolling.js @@ -1,4 +1,5 @@ const debug = require('debug')('node-telegram-bot-api'); +const deprecate = require('depd')('node-telegram-bot-api'); const ANOTHER_WEB_HOOK_USED = 409; @@ -11,9 +12,15 @@ class TelegramBotPolling { constructor(bot) { this.bot = bot; this.options = (typeof bot.options.polling === 'boolean') ? {} : bot.options.polling; - this.options.timeout = (typeof this.options.timeout === 'number') ? this.options.timeout : 10; this.options.interval = (typeof this.options.interval === 'number') ? this.options.interval : 300; - this._offset = 0; + this.options.params = (typeof this.options.params === 'object') ? this.options.params : {}; + this.options.params.offset = (typeof this.options.params.offset === 'number') ? this.options.params.offset : 0; + if (typeof this.options.timeout === 'number') { + deprecate('`options.polling.timeout` is deprecated. Use `options.polling.params` instead.'); + this.options.params.timeout = this.options.timeout; + } else { + this.options.params.timeout = 10; + } this._lastUpdate = 0; this._lastRequest = null; this._abort = false; @@ -85,8 +92,8 @@ class TelegramBotPolling { this._lastUpdate = Date.now(); debug('polling data %j', updates); updates.forEach(update => { - this._offset = update.update_id; - debug('updated offset: %s', this._offset); + this.options.params.offset = update.update_id + 1; + debug('updated offset: %s', this.options.params.offset); this.bot.processUpdate(update); }); return null; @@ -125,16 +132,8 @@ class TelegramBotPolling { * Retrieve updates */ _getUpdates() { - const opts = { - qs: { - offset: this._offset + 1, - limit: this.options.limit, - timeout: this.options.timeout - }, - }; - debug('polling with options: %j', opts); - - return this.bot._request('getUpdates', opts) + debug('polling with options: %j', this.options.params); + return this.bot._request('getUpdates', this.options.params) .catch(err => { if (err.response && err.response.statusCode === ANOTHER_WEB_HOOK_USED) { return this._unsetWebHook();