2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-23 02:17:16 +00:00

[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.
This commit is contained in:
GochoMugo 2017-01-11 17:06:04 +03:00
parent 061d11a109
commit a9d6e93622
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
3 changed files with 22 additions and 0 deletions

View File

@ -150,6 +150,7 @@ Emits `message` when a message arrives.
### telegramBot.startPolling([options]) ⇒ <code>Promise</code> ### telegramBot.startPolling([options]) ⇒ <code>Promise</code>
Start polling. Start polling.
Rejects returned promise if a WebHook is being used by this instance.
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> **Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
@ -190,6 +191,7 @@ Return true if polling. Otherwise, false.
### telegramBot.openWebHook() ⇒ <code>Promise</code> ### telegramBot.openWebHook() ⇒ <code>Promise</code>
Open webhook. Open webhook.
Multiple invocations do nothing if webhook is already open. Multiple invocations do nothing if webhook is already open.
Rejects returned promise if Polling is being used by this instance.
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> **Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
<a name="TelegramBot+closeWebHook"></a> <a name="TelegramBot+closeWebHook"></a>

View File

@ -237,11 +237,15 @@ class TelegramBot extends EventEmitter {
/** /**
* Start polling. * Start polling.
* Rejects returned promise if a WebHook is being used by this instance.
* @param {Object} [options] * @param {Object} [options]
* @param {Boolean} [options.restart=true] Consecutive calls to this method causes polling to be restarted * @param {Boolean} [options.restart=true] Consecutive calls to this method causes polling to be restarted
* @return {Promise} * @return {Promise}
*/ */
startPolling(options = {}) { 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; options.restart = typeof options.restart === 'undefined' ? true : options.restart;
if (!this._polling) { if (!this._polling) {
this._polling = new TelegramBotPolling(this._request.bind(this), this.options.polling, this.processUpdate.bind(this)); 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. * Open webhook.
* Multiple invocations do nothing if webhook is already open. * Multiple invocations do nothing if webhook is already open.
* Rejects returned promise if Polling is being used by this instance.
* @return {Promise} * @return {Promise}
*/ */
openWebHook() { openWebHook() {
if (this.isPolling()) {
return Promise.reject(new Error('WebHook and Polling are mutually exclusive'));
}
if (!this._webHook) { if (!this._webHook) {
this._webHook = new TelegramBotWebHook(this.token, this.options.webHook, this.processUpdate.bind(this)); this._webHook = new TelegramBotWebHook(this.token, this.options.webHook, this.processUpdate.bind(this));
} }

View File

@ -185,6 +185,12 @@ describe('TelegramBot', function telegramSuite() {
return utils.isPollingMockServer(pollingPort); 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() { describe('#isPolling', function isPollingSuite() {
@ -219,6 +225,12 @@ describe('TelegramBot', function telegramSuite() {
return utils.hasOpenWebHook(webHookPort); 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() { describe('#hasOpenWebHook', function hasOpenWebHookSuite() {