class TelegramBot extends EventEmitter {
static get errors() {
return errors;
}
static get messageTypes() {
return _messageTypes;
}
/**
* Both request method to obtain messages are implemented. To use standard polling, set `polling: true`
* on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a SSL certificate.
* Emits `message` when a message arrives.
*
* @class TelegramBot
* @constructor
* @param {String} token Bot Token
* @param {Object} [options]
* @param {Boolean|Object} [options.polling=false] Set true to enable polling or set options.
* If a WebHook has been set, it will be deleted automatically.
* @param {String|Number} [options.polling.timeout=10] *Deprecated. Use `options.polling.params` instead*.
* Timeout in seconds for long polling.
* @param {String|Number} [options.polling.interval=300] Interval between requests in miliseconds
* @param {Boolean} [options.polling.autoStart=true] Start polling immediately
* @param {Object} [options.polling.params] Parameters to be used in polling API requests.
* See https://core.telegram.org/bots/api#getupdates for more information.
* @param {Number} [options.polling.params.timeout=10] Timeout in seconds for long polling.
* @param {Boolean|Object} [options.webHook=false] Set true to enable WebHook or set options
* @param {String} [options.webHook.host=0.0.0.0] Host to bind to
* @param {Number} [options.webHook.port=8443] Port to bind to
* @param {String} [options.webHook.key] Path to file with PEM private key for webHook server.
* The file is read **synchronously**!
* @param {String} [options.webHook.cert] Path to file with PEM certificate (public) for webHook server.
* The file is read **synchronously**!
* @param {String} [options.webHook.pfx] Path to file with PFX private key and certificate chain for webHook server.
* The file is read **synchronously**!
* @param {Boolean} [options.webHook.autoOpen=true] Open webHook immediately
* @param {Object} [options.webHook.https] Options to be passed to `https.createServer()`.
* Note that `options.webHook.key`, `options.webHook.cert` and `options.webHook.pfx`, if provided, will be
* used to override `key`, `cert` and `pfx` in this object, respectively.
* See https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener for more information.
* @param {String} [options.webHook.healthEndpoint=/healthz] An endpoint for health checks that always responds with 200 OK
* @param {Boolean} [options.onlyFirstMatch=false] Set to true to stop after first match. Otherwise, all regexps are executed
* @param {Object} [options.request] Options which will be added for all requests to telegram api.
* See https://github.com/request/request#requestoptions-callback for more information.
* @param {String} [options.baseApiUrl=https://api.telegram.org] API Base URl; useful for proxying and testing
* @param {Boolean} [options.filepath=true] Allow passing file-paths as arguments when sending files,
* such as photos using `TelegramBot#sendPhoto()`. See [usage information][usage-sending-files-performance]
* for more information on this option and its consequences.
* @see https://core.telegram.org/bots/api
*/
constructor(token, options = {}) {
super();
this.token = token;
this.options = options;
this.options.polling = (typeof options.polling === 'undefined') ? false : options.polling;
this.options.webHook = (typeof options.webHook === 'undefined') ? false : options.webHook;
this.options.baseApiUrl = options.baseApiUrl || 'https://api.telegram.org';
this.options.filepath = (typeof options.filepath === 'undefined') ? true : options.filepath;
this._textRegexpCallbacks = [];
this._replyListenerId = 0;
this._replyListeners = [];
this._polling = null;
this._webHook = null;
if (options.polling) {
const autoStart = options.polling.autoStart;
if (typeof autoStart === 'undefined' || autoStart ...
n/a
function BaseError(code, message) { _classCallCheck(this, BaseError); var _this = _possibleConstructorReturn(this, (BaseError.__proto__ || Object.getPrototypeOf(BaseError)).call(this, code + ': ' + message)); _this.code = code; return _this; }
n/a
function FatalError(data) { _classCallCheck(this, FatalError); var error = typeof data === 'string' ? null : data; var message = error ? error.message : data; var _this2 = _possibleConstructorReturn(this, (FatalError.__proto__ || Object.getPrototypeOf(FatalError)).call(this, 'EFATAL', message)); if (error) _this2.stack = error.stack; return _this2; }
...
}, {
key: '_request',
value: function _request(_path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!this.token) {
return Promise.reject(new errors.FatalError('Telegram Bot Token not provided!
x27;));
}
if (this.options.request) {
Object.assign(options, this.options.request);
}
if (options.form) {
...
function ParseError(message, response) { _classCallCheck(this, ParseError); var _this3 = _possibleConstructorReturn(this, (ParseError.__proto__ || Object.getPrototypeOf(ParseError)).call(this, 'EPARSE', message)); _this3.response = response; return _this3; }
...
options.forever = true;
debug('HTTP request: %j', options);
return request(options).then(function (resp) {
var data = void 0;
try {
data = resp.body = JSON.parse(resp.body);
} catch (err) {
throw new errors.ParseError('Error parsing Telegram response: ' + resp.body
, resp);
}
if (data.ok) {
return data.result;
}
throw new errors.TelegramError(data.error_code + ' ' + data.description, resp);
...
function TelegramError(message, response) { _classCallCheck(this, TelegramError); var _this4 = _possibleConstructorReturn(this, (TelegramError.__proto__ || Object.getPrototypeOf(TelegramError)).call(this, 'ETELEGRAM ', message)); _this4.response = response; return _this4; }
...
throw new errors.ParseError('Error parsing Telegram response: ' + resp.body, resp);
}
if (data.ok) {
return data.result;
}
throw new errors.TelegramError(data.error_code + ' ' + data.description
, resp);
}).catch(function (error) {
// TODO: why can't we do `error instanceof errors.BaseError`?
if (error.response) throw error;
throw new errors.FatalError(error);
});
}
...