2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-30 22:05:28 +00:00

[polling,webhook] Add methods controlling polling, webhook

Feature:

  For finer control over bot's polling and web-hook, the
  following methods have been added:

  * TelegramBot#isPolling()
  * TelegramBot#openWebHook()
  * TelegramBot#closeWebHook()
  * TelegramBot#hasOpenWebHook()

  Please read README.md for more information on the
  new methods.
This commit is contained in:
GochoMugo
2017-01-02 13:58:46 +03:00
parent 69b059a4d7
commit 8edd5f4c6d
3 changed files with 86 additions and 2 deletions

View File

@@ -73,6 +73,10 @@ TelegramBot
* [new TelegramBot(token, [options])](#new_TelegramBot_new)
* [.initPolling()](#TelegramBot+initPolling)
* [.stopPolling()](#TelegramBot+stopPolling) ⇒ <code>Promise</code>
* [.isPolling()](#TelegramBot+isPolling) ⇒ <code>Boolean</code>
* [.openWebHook()](#TelegramBot+openWebHook)
* [.closeWebHook()](#TelegramBot+closeWebHook) ⇒ <code>Promise</code>
* [.hasOpenWebHook()](#TelegramBot+hasOpenWebHook) ⇒ <code>Boolean</code>
* [.getMe()](#TelegramBot+getMe) ⇒ <code>Promise</code>
* [.setWebHook(url, [cert])](#TelegramBot+setWebHook)
* [.getUpdates([timeout], [limit], [offset])](#TelegramBot+getUpdates) ⇒ <code>Promise</code>
@@ -139,7 +143,7 @@ Emits `message` when a message arrives.
### telegramBot.initPolling()
Start polling
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
<a name="TelegramBot+stopPolling"></a>
### telegramBot.stopPolling() ⇒ <code>Promise</code>
@@ -147,6 +151,32 @@ Stops polling after the last polling request resolves
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
**Returns**: <code>Promise</code> - promise Promise, of last polling request
<a name="TelegramBot+isPolling"></a>
### telegramBot.isPolling() ⇒ <code>Boolean</code>
Return true if polling. Otherwise, false.
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
<a name="TelegramBot+openWebHook"></a>
### telegramBot.openWebHook()
Open webhook
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
<a name="TelegramBot+closeWebHook"></a>
### telegramBot.closeWebHook() ⇒ <code>Promise</code>
Close webhook after closing all current connections
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
**Returns**: <code>Promise</code> - promise
<a name="TelegramBot+hasOpenWebHook"></a>
### telegramBot.hasOpenWebHook() ⇒ <code>Boolean</code>
Return true if using webhook and it is open i.e. accepts connections.
Otherwise, false.
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
<a name="TelegramBot+getMe"></a>
### telegramBot.getMe() ⇒ <code>Promise</code>

View File

@@ -70,7 +70,7 @@ class TelegramBot extends EventEmitter {
if (options.webHook) {
const autoOpen = options.webHook.autoOpen;
if (typeof autoOpen === 'undefined' || autoOpen === true) {
this._WebHook = new TelegramBotWebHook(token, options.webHook, this.processUpdate.bind(this));
this.openWebHook();
}
}
}
@@ -325,6 +325,46 @@ class TelegramBot extends EventEmitter {
return polling.stopPolling();
}
/**
* Return true if polling. Otherwise, false.
* @return {Boolean}
*/
isPolling() {
return !!this._polling;
}
/**
* Open webhook
*/
openWebHook() {
if (this._webHook) {
return;
}
this._webHook = new TelegramBotWebHook(this.token, this.options.webHook, this._processUpdate.bind(this));
}
/**
* Close webhook after closing all current connections
* @return {Promise} promise
*/
closeWebHook() {
if (!this._webHook) {
return Promise.resolve();
}
const webHook = this._webHook;
delete this._webHook;
return webHook.close();
}
/**
* Return true if using webhook and it is open i.e. accepts connections.
* Otherwise, false.
* @return {Boolean}
*/
hasOpenWebHook() {
return !!this._webHook;
}
/**
* Returns basic information about the bot in form of a `User` object.
* @return {Promise}

View File

@@ -3,6 +3,7 @@ const https = require('https');
const http = require('http');
const fs = require('fs');
const bl = require('bl');
const Promise = require('bluebird');
class TelegramBotWebHook {
@@ -98,6 +99,19 @@ class TelegramBotWebHook {
}
}
/**
* Close the webHook
* @return {Promise}
*/
close() {
const self = this;
return new Promise(function closePromise(resolve, reject) {
self._webServer.close(function closeCb(error) {
if (error) return reject(error);
return resolve();
});
});
}
}
module.exports = TelegramBotWebHook;