2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-29 13:27:44 +00:00

[telegram] Allow specifying API Base URL

Feature:

  We can now pass a custom API Base URL to
  be used during making HTTP requests. This is useful in
  proxying requests, or running tests.

  In particular, this library is (almost) ready to
  be used with PWRTelegram API (http://pwrtelegram.xyz/).
This commit is contained in:
GochoMugo 2017-01-02 14:05:10 +03:00
parent 8edd5f4c6d
commit 6b4ff126f4
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
2 changed files with 5 additions and 10 deletions

View File

@ -137,6 +137,7 @@ Emits `message` when a message arrives.
| [options.webHook.autoOpen] | <code>Boolean</code> | <code>true</code> | Open webHook immediately | | [options.webHook.autoOpen] | <code>Boolean</code> | <code>true</code> | Open webHook immediately |
| [options.onlyFirstMatch] | <code>Boolean</code> | <code>false</code> | Set to true to stop after first match. Otherwise, all regexps are executed | | [options.onlyFirstMatch] | <code>Boolean</code> | <code>false</code> | Set to true to stop after first match. Otherwise, all regexps are executed |
| [options.request] | <code>Object</code> | | Options which will be added for all requests to telegram api. See https://github.com/request/request#requestoptions-callback for more information. | | [options.request] | <code>Object</code> | | Options which will be added for all requests to telegram api. See https://github.com/request/request#requestoptions-callback for more information. |
| [options.baseApiUrl] | <code>String</code> | <code>https://api.telegram.org</code> | API Base URl; useful for proxying and testing |
<a name="TelegramBot+initPolling"></a> <a name="TelegramBot+initPolling"></a>

View File

@ -51,12 +51,14 @@ class TelegramBot extends EventEmitter {
* @param {Boolean} [options.webHook.autoOpen=true] Open webHook immediately * @param {Boolean} [options.webHook.autoOpen=true] Open webHook immediately
* @param {Boolean} [options.onlyFirstMatch=false] Set to true to stop after first match. Otherwise, all regexps are executed * @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 {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
* @see https://core.telegram.org/bots/api * @see https://core.telegram.org/bots/api
*/ */
constructor(token, options = {}) { constructor(token, options = {}) {
super(); super();
this.token = token; this.token = token;
this.options = options; this.options = options;
this.options.baseApiUrl = options.baseApiUrl || 'https://api.telegram.org';
this._textRegexpCallbacks = []; this._textRegexpCallbacks = [];
this._onReplyToMessages = []; this._onReplyToMessages = [];
@ -169,11 +171,7 @@ class TelegramBot extends EventEmitter {
* @see https://core.telegram.org/bots/api#making-requests * @see https://core.telegram.org/bots/api#making-requests
*/ */
_buildURL(_path) { _buildURL(_path) {
return URL.format({ return `${this.options.baseApiUrl}/bot${this.token}/${_path}`;
protocol: 'https',
host: 'api.telegram.org',
pathname: `/bot${this.token}/${_path}`
});
} }
/** /**
@ -830,11 +828,7 @@ class TelegramBot extends EventEmitter {
*/ */
getFileLink(fileId) { getFileLink(fileId) {
return this.getFile(fileId) return this.getFile(fileId)
.then(resp => URL.format({ .then(resp => `${this.options.baseApiUrl}/file/bot${this.token}/${resp.file_path}`);
protocol: 'https',
host: 'api.telegram.org',
pathname: `/file/bot${this.token}/${resp.file_path}`
}));
} }
/** /**