2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-09-01 06:45:41 +00:00

[webhook,polling] Improve starting, stopping of webhook, polling

Feature:

  The different mechanisms of fetching updates, i.e. polling
  and webhook, have had their implementations improved:

  * the TelegramBot instance needs to create the polling and
    webhook instances once, and when necessary
  * returning promises from TelegramBot#openWebHook() and
    TelegramBot#startPolling() allows more precise control

  Also,

  * TelegramBot#initPolling() is being deprecated in favor of
    TelegramBot#startPolling() to ensure consistency (as the
    opposite action of TelegramBot#stopPolling())
This commit is contained in:
GochoMugo
2017-01-09 15:57:34 +03:00
parent 4735518116
commit 97c8130d93
5 changed files with 159 additions and 61 deletions

View File

@@ -47,12 +47,47 @@ class TelegramBotWebHook {
debug('HTTP WebHook enabled');
this._webServer = http.createServer(this._requestListener);
}
}
this._webServer.listen(this.options.port, this.options.host, () => {
debug('WebHook listening on port %s', this.options.port);
/**
* Open WebHook by listening on the port
* @return {Promise}
*/
open() {
if (this._webServer.listening) {
return Promise.resolve();
}
return new Promise(resolve => {
this._webServer.listen(this.options.port, this.options.host, () => {
debug('WebHook listening on port %s', this.options.port);
return resolve();
});
});
}
/**
* Close the webHook
* @return {Promise}
*/
close() {
if (!this._webServer.listening) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
this._webServer.close(error => {
if (error) return reject(error);
return resolve();
});
});
}
/**
* Return `true` if server is listening. Otherwise, `false`.
*/
isOpen() {
return this._webServer.listening;
}
// used so that other funcs are not non-optimizable
_safeParse(json) {
try {
@@ -106,20 +141,6 @@ class TelegramBotWebHook {
res.end();
}
}
/**
* 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;