mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-09-02 15:25: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:
32
README.md
32
README.md
@@ -73,6 +73,10 @@ TelegramBot
|
|||||||
* [new TelegramBot(token, [options])](#new_TelegramBot_new)
|
* [new TelegramBot(token, [options])](#new_TelegramBot_new)
|
||||||
* [.initPolling()](#TelegramBot+initPolling)
|
* [.initPolling()](#TelegramBot+initPolling)
|
||||||
* [.stopPolling()](#TelegramBot+stopPolling) ⇒ <code>Promise</code>
|
* [.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>
|
* [.getMe()](#TelegramBot+getMe) ⇒ <code>Promise</code>
|
||||||
* [.setWebHook(url, [cert])](#TelegramBot+setWebHook)
|
* [.setWebHook(url, [cert])](#TelegramBot+setWebHook)
|
||||||
* [.getUpdates([timeout], [limit], [offset])](#TelegramBot+getUpdates) ⇒ <code>Promise</code>
|
* [.getUpdates([timeout], [limit], [offset])](#TelegramBot+getUpdates) ⇒ <code>Promise</code>
|
||||||
@@ -139,7 +143,7 @@ Emits `message` when a message arrives.
|
|||||||
### telegramBot.initPolling()
|
### telegramBot.initPolling()
|
||||||
Start polling
|
Start polling
|
||||||
|
|
||||||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
||||||
<a name="TelegramBot+stopPolling"></a>
|
<a name="TelegramBot+stopPolling"></a>
|
||||||
|
|
||||||
### telegramBot.stopPolling() ⇒ <code>Promise</code>
|
### 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>
|
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
||||||
**Returns**: <code>Promise</code> - promise Promise, of last polling request
|
**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>
|
<a name="TelegramBot+getMe"></a>
|
||||||
|
|
||||||
### telegramBot.getMe() ⇒ <code>Promise</code>
|
### telegramBot.getMe() ⇒ <code>Promise</code>
|
||||||
|
@@ -70,7 +70,7 @@ class TelegramBot extends EventEmitter {
|
|||||||
if (options.webHook) {
|
if (options.webHook) {
|
||||||
const autoOpen = options.webHook.autoOpen;
|
const autoOpen = options.webHook.autoOpen;
|
||||||
if (typeof autoOpen === 'undefined' || autoOpen === true) {
|
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 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.
|
* Returns basic information about the bot in form of a `User` object.
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
|
@@ -3,6 +3,7 @@ const https = require('https');
|
|||||||
const http = require('http');
|
const http = require('http');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const bl = require('bl');
|
const bl = require('bl');
|
||||||
|
const Promise = require('bluebird');
|
||||||
|
|
||||||
|
|
||||||
class TelegramBotWebHook {
|
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;
|
module.exports = TelegramBotWebHook;
|
||||||
|
Reference in New Issue
Block a user