[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.
experimental
GochoMugo 9 years ago
parent 061d11a109
commit a9d6e93622
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
  1. 2
      README.md
  2. 8
      src/telegram.js
  3. 12
      test/telegram.js

@ -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>

@ -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));
} }

@ -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() {

Loading…
Cancel
Save