Skip to content

Commit

Permalink
src/polling: Add constructor option 'options.polling.params'
Browse files Browse the repository at this point in the history
Feature:

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

Side-effects:

  * `options.timeout` is deprecated!

References:

  * "Feature request": yagop/node-telegram-bot-api#243
  • Loading branch information
passion-27 committed Feb 9, 2017
1 parent 5f1a177 commit 08fb066
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
5 changes: 4 additions & 1 deletion src/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 13 additions & 14 deletions src/telegramPolling.js
Original file line number Diff line number Diff line change
@@ -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;


Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 08fb066

Please sign in to comment.