From e2f095fc52cbbd943d8aa4304b8ac645bb97d0af Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Mon, 2 Jan 2017 13:29:37 +0300 Subject: [PATCH] [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 --- README.md | 1 + src/telegram.js | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b3e62f2..5f515ec 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ Emits `message` when a message arrives. | [options.polling] | Boolean | Object | false | Set true to enable polling or set options | | [options.polling.timeout] | String | Number | 10 | 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.webHook] | Boolean | Object | false | Set true to enable WebHook or set options | | [options.webHook.port] | Number | 8443 | Port to bind to | | [options.webHook.key] | String | | Path to file with PEM private key for webHook server. (Read synchronously!) | diff --git a/src/telegram.js b/src/telegram.js index 419b3d1..760ff21 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -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,7 +60,10 @@ class TelegramBot extends EventEmitter { this._onReplyToMessages = []; if (options.polling) { - this.initPolling(); + const autoStart = options.polling.autoStart; + if (typeof autoStart === 'undefined' || autoStart === true) { + this.initPolling(); + } } if (options.webHook) {