From 69b059a4d7c1968c5a7c0345fff89fca2f17e94f Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Mon, 2 Jan 2017 13:43:56 +0300 Subject: [PATCH] [webhook] Allow enabling/disabling webhook auto-open Feature: Currently, if the constructor option 'options.webHook' is passed, the bot opens the webHook immediately! There's NO way to disable this behavior, which might be useful in cases such as: * providing custom webhook parameters without opening the webhook immediately The new boolean option, 'autoOpen', can now be used to control this behavior. For example, ```js const bot = new TelegramBot(token, { webHook: { autoOpen: false, }, }); ``` If set to 'false', the bot does NOT open the web-hook immediately. Currently, there's NO way to open the web-hook in this case. I'm working on that. Expect a feature to add a method to open the web-hook manually! If not provided, its value defaults to 'true'. Implementation: * Backwards-compatible: the behavior of opening the web-hook 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 5f515ec..8c53ac1 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ Emits `message` when a message arrives. | [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!) | | [options.webHook.cert] | String | | Path to file with PEM certificate (public) for webHook server. (Read synchronously!) | +| [options.webHook.autoOpen] | Boolean | true | Open webHook immediately | | [options.onlyFirstMatch] | Boolean | false | Set to true to stop after first match. Otherwise, all regexps are executed | | [options.request] | Object | | Options which will be added for all requests to telegram api. See https://github.com/request/request#requestoptions-callback for more information. | diff --git a/src/telegram.js b/src/telegram.js index 760ff21..cb68fb0 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -48,6 +48,7 @@ class TelegramBot extends EventEmitter { * @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!) * @param {String} [options.webHook.cert] Path to file with PEM certificate (public) for webHook server. (Read synchronously!) + * @param {Boolean} [options.webHook.autoOpen=true] Open webHook immediately * @param {Boolean} [options.onlyFirstMatch=false] Set to true to stop after first match. Otherwise, all regexps are executed * @param {Object} [options.request] Options which will be added for all requests to telegram api. See https://github.com/request/request#requestoptions-callback for more information. * @see https://core.telegram.org/bots/api @@ -67,7 +68,10 @@ class TelegramBot extends EventEmitter { } if (options.webHook) { - this._WebHook = new TelegramBotWebHook(token, options.webHook, this.processUpdate.bind(this)); + const autoOpen = options.webHook.autoOpen; + if (typeof autoOpen === 'undefined' || autoOpen === true) { + this._WebHook = new TelegramBotWebHook(token, options.webHook, this.processUpdate.bind(this)); + } } }