From a9d6e936220c3ce774e5a48162a8e890f46c352c Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Wed, 11 Jan 2017 17:06:04 +0300 Subject: [PATCH] [telegram] Return error on trying to use Polling, WebHook together Feature: Polling and WebHook are mutually exclusive. Therefore, return an error whenever the user tries to start polling, and the instance has an open webhook, or user tries to open a webhook, and the instance is already polling. --- README.md | 2 ++ src/telegram.js | 8 ++++++++ test/telegram.js | 12 ++++++++++++ 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index f366b31..bd84e57 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,7 @@ Emits `message` when a message arrives. ### telegramBot.startPolling([options]) ⇒ Promise Start polling. +Rejects returned promise if a WebHook is being used by this instance. **Kind**: instance method of [TelegramBot](#TelegramBot) @@ -190,6 +191,7 @@ Return true if polling. Otherwise, false. ### telegramBot.openWebHook() ⇒ Promise Open webhook. Multiple invocations do nothing if webhook is already open. +Rejects returned promise if Polling is being used by this instance. **Kind**: instance method of [TelegramBot](#TelegramBot) diff --git a/src/telegram.js b/src/telegram.js index d7c2245..b5a51cc 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -237,11 +237,15 @@ class TelegramBot extends EventEmitter { /** * Start polling. + * Rejects returned promise if a WebHook is being used by this instance. * @param {Object} [options] * @param {Boolean} [options.restart=true] Consecutive calls to this method causes polling to be restarted * @return {Promise} */ startPolling(options = {}) { + if (this.hasOpenWebHook()) { + return Promise.reject(new Error('Polling and WebHook are mutually exclusive')); + } options.restart = typeof options.restart === 'undefined' ? true : options.restart; if (!this._polling) { this._polling = new TelegramBotPolling(this._request.bind(this), this.options.polling, this.processUpdate.bind(this)); @@ -284,9 +288,13 @@ class TelegramBot extends EventEmitter { /** * Open webhook. * Multiple invocations do nothing if webhook is already open. + * Rejects returned promise if Polling is being used by this instance. * @return {Promise} */ openWebHook() { + if (this.isPolling()) { + return Promise.reject(new Error('WebHook and Polling are mutually exclusive')); + } if (!this._webHook) { this._webHook = new TelegramBotWebHook(this.token, this.options.webHook, this.processUpdate.bind(this)); } diff --git a/test/telegram.js b/test/telegram.js index 0928a5d..40c5ad5 100644 --- a/test/telegram.js +++ b/test/telegram.js @@ -185,6 +185,12 @@ describe('TelegramBot', function telegramSuite() { return utils.isPollingMockServer(pollingPort); }); }); + it('returns error if using webhook', function test() { + return botWebHook.startPolling().catch((err) => { + // TODO: check for error in a better way + assert.ok(err.message.indexOf('mutually exclusive') !== -1); + }); + }); }); describe('#isPolling', function isPollingSuite() { @@ -219,6 +225,12 @@ describe('TelegramBot', function telegramSuite() { return utils.hasOpenWebHook(webHookPort); }); }); + it('returns error if using polling', function test() { + return botPolling.openWebHook().catch((err) => { + // TODO: check for error in a better way + assert.ok(err.message.indexOf('mutually exclusive') !== -1); + }); + }); }); describe('#hasOpenWebHook', function hasOpenWebHookSuite() {