From 8edd5f4c6d8a7cfc0d5adc906251e52cfacd1c93 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Mon, 2 Jan 2017 13:58:46 +0300 Subject: [PATCH] [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. --- README.md | 32 +++++++++++++++++++++++++++++++- src/telegram.js | 42 +++++++++++++++++++++++++++++++++++++++++- src/telegramWebHook.js | 14 ++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8c53ac1..84539ed 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,10 @@ TelegramBot * [new TelegramBot(token, [options])](#new_TelegramBot_new) * [.initPolling()](#TelegramBot+initPolling) * [.stopPolling()](#TelegramBot+stopPolling) ⇒ Promise + * [.isPolling()](#TelegramBot+isPolling) ⇒ Boolean + * [.openWebHook()](#TelegramBot+openWebHook) + * [.closeWebHook()](#TelegramBot+closeWebHook) ⇒ Promise + * [.hasOpenWebHook()](#TelegramBot+hasOpenWebHook) ⇒ Boolean * [.getMe()](#TelegramBot+getMe) ⇒ Promise * [.setWebHook(url, [cert])](#TelegramBot+setWebHook) * [.getUpdates([timeout], [limit], [offset])](#TelegramBot+getUpdates) ⇒ Promise @@ -139,7 +143,7 @@ Emits `message` when a message arrives. ### telegramBot.initPolling() Start polling -**Kind**: instance method of [TelegramBot](#TelegramBot) +**Kind**: instance method of [TelegramBot](#TelegramBot) ### telegramBot.stopPolling() ⇒ Promise @@ -147,6 +151,32 @@ Stops polling after the last polling request resolves **Kind**: instance method of [TelegramBot](#TelegramBot) **Returns**: Promise - promise Promise, of last polling request + + +### telegramBot.isPolling() ⇒ Boolean +Return true if polling. Otherwise, false. + +**Kind**: instance method of [TelegramBot](#TelegramBot) + + +### telegramBot.openWebHook() +Open webhook + +**Kind**: instance method of [TelegramBot](#TelegramBot) + + +### telegramBot.closeWebHook() ⇒ Promise +Close webhook after closing all current connections + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**Returns**: Promise - promise + + +### telegramBot.hasOpenWebHook() ⇒ Boolean +Return true if using webhook and it is open i.e. accepts connections. +Otherwise, false. + +**Kind**: instance method of [TelegramBot](#TelegramBot) ### telegramBot.getMe() ⇒ Promise diff --git a/src/telegram.js b/src/telegram.js index cb68fb0..3846e5c 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -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} diff --git a/src/telegramWebHook.js b/src/telegramWebHook.js index 5efa3c1..87feed6 100644 --- a/src/telegramWebHook.js +++ b/src/telegramWebHook.js @@ -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;