2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-28 21:07:39 +00:00

[polling] Allow enabling/disabling polling auto-start

Feature:

  Currently, if the constructor option 'options.polling' is
  passed, the bot begins polling immediately! There's NO
  way to disable this behavior, which might be useful in
  cases such as:

  * providing custom polling parameters without starting
    the polling immediately

  The boolean option, 'autoStart', can now be used to control this
  behavior. For example,

  ```js
  const bot = new TelegramBot(token, {
    polling: {
      autoStart: false,
    },
  });
  ```

  If set to 'false', the bot does NOT begin polling
  immediately. You'll have to use TelegramBot#initPolling().

  If not provided, its value defaults to 'true'.

Implementation:

  * Backwards-compatible: the behavior of starting polling
    immediately remains, when the parameter is NOT provided
This commit is contained in:
GochoMugo 2017-01-02 13:29:37 +03:00
parent daab34d98d
commit e2f095fc52
2 changed files with 6 additions and 1 deletions

View File

@ -125,6 +125,7 @@ Emits `message` when a message arrives.
| [options.polling] | <code>Boolean</code> &#124; <code>Object</code> | <code>false</code> | Set true to enable polling or set options |
| [options.polling.timeout] | <code>String</code> &#124; <code>Number</code> | <code>10</code> | 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.webHook] | <code>Boolean</code> &#124; <code>Object</code> | <code>false</code> | Set true to enable WebHook or set options |
| [options.webHook.port] | <code>Number</code> | <code>8443</code> | Port to bind to |
| [options.webHook.key] | <code>String</code> | | Path to file with PEM private key for webHook server. (Read synchronously!) |

View File

@ -43,6 +43,7 @@ class TelegramBot extends EventEmitter {
* @param {Boolean|Object} [options.polling=false] Set true to enable polling or set options
* @param {String|Number} [options.polling.timeout=10] 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 {Boolean|Object} [options.webHook=false] Set true to enable WebHook or set options
* @param {Number} [options.webHook.port=8443] Port to bind to
* @param {String} [options.webHook.key] Path to file with PEM private key for webHook server. (Read synchronously!)
@ -59,8 +60,11 @@ class TelegramBot extends EventEmitter {
this._onReplyToMessages = [];
if (options.polling) {
const autoStart = options.polling.autoStart;
if (typeof autoStart === 'undefined' || autoStart === true) {
this.initPolling();
}
}
if (options.webHook) {
this._WebHook = new TelegramBotWebHook(token, options.webHook, this.processUpdate.bind(this));