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

src/polling: Add constructor option 'options.polling.params'

Feature:

  Please see the updated API reference for more information on this
  new option.

Side-effects:

  * `options.timeout` is deprecated!

References:

  * "Feature request": https://github.com/yagop/node-telegram-bot-api/issues/243
This commit is contained in:
GochoMugo 2017-02-09 17:24:11 +03:00
parent eed7c1e4d0
commit e75d51ca8f
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
4 changed files with 21 additions and 16 deletions

View File

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

View File

@ -71,9 +71,10 @@ Emits `message` when a message arrives.
| token | <code>String</code> | | Bot Token |
| [options] | <code>Object</code> | | |
| [options.polling] | <code>Boolean</code> &#124; <code>Object</code> | <code>false</code> | Set true to enable polling or set options. If a WebHook has been set, it will be deleted automatically. |
| [options.polling.timeout] | <code>String</code> &#124; <code>Number</code> | <code>10</code> | Timeout in seconds for long polling |
| [options.polling.timeout] | <code>String</code> &#124; <code>Number</code> | <code>10</code> | *Deprecated. Use `options.polling.params` instead*. Timeout in seconds for long polling. |
| [options.polling.interval] | <code>String</code> &#124; <code>Number</code> | <code>300</code> | Interval between requests in miliseconds |
| [options.polling.autoStart] | <code>Boolean</code> | <code>true</code> | Start polling immediately |
| [options.polling.params] | <code>Object</code> | | Parameters to be used in polling API requests. See https://core.telegram.org/bots/api#getupdates for more information. |
| [options.webHook] | <code>Boolean</code> &#124; <code>Object</code> | <code>false</code> | Set true to enable WebHook or set options |
| [options.webHook.host] | <code>String</code> | <code>0.0.0.0</code> | Host to bind to |
| [options.webHook.port] | <code>Number</code> | <code>8443</code> | Port to bind to |

View File

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

View File

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