mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-22 18:07: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:
parent
061d11a109
commit
a9d6e93622
@ -150,6 +150,7 @@ Emits `message` when a message arrives.
|
||||
|
||||
### telegramBot.startPolling([options]) ⇒ <code>Promise</code>
|
||||
Start polling.
|
||||
Rejects returned promise if a WebHook is being used by this instance.
|
||||
|
||||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
||||
|
||||
@ -190,6 +191,7 @@ Return true if polling. Otherwise, false.
|
||||
### telegramBot.openWebHook() ⇒ <code>Promise</code>
|
||||
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 <code>[TelegramBot](#TelegramBot)</code>
|
||||
<a name="TelegramBot+closeWebHook"></a>
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user