From 6b4ff126f48dd7b75e1b7682cf51cd0272fca1a7 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Mon, 2 Jan 2017 14:05:10 +0300 Subject: [PATCH] [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/). --- README.md | 1 + src/telegram.js | 14 ++++---------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 84539ed..383ebc4 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ Emits `message` when a message arrives. | [options.webHook.autoOpen] | Boolean | true | Open webHook immediately | | [options.onlyFirstMatch] | Boolean | false | Set to true to stop after first match. Otherwise, all regexps are executed | | [options.request] | Object | | Options which will be added for all requests to telegram api. See https://github.com/request/request#requestoptions-callback for more information. | +| [options.baseApiUrl] | String | https://api.telegram.org | API Base URl; useful for proxying and testing | diff --git a/src/telegram.js b/src/telegram.js index 3846e5c..4156d65 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -51,12 +51,14 @@ class TelegramBot extends EventEmitter { * @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 {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 */ constructor(token, options = {}) { super(); this.token = token; this.options = options; + this.options.baseApiUrl = options.baseApiUrl || 'https://api.telegram.org'; this._textRegexpCallbacks = []; this._onReplyToMessages = []; @@ -169,11 +171,7 @@ class TelegramBot extends EventEmitter { * @see https://core.telegram.org/bots/api#making-requests */ _buildURL(_path) { - return URL.format({ - protocol: 'https', - host: 'api.telegram.org', - pathname: `/bot${this.token}/${_path}` - }); + return `${this.options.baseApiUrl}/bot${this.token}/${_path}`; } /** @@ -830,11 +828,7 @@ class TelegramBot extends EventEmitter { */ getFileLink(fileId) { return this.getFile(fileId) - .then(resp => URL.format({ - protocol: 'https', - host: 'api.telegram.org', - pathname: `/file/bot${this.token}/${resp.file_path}` - })); + .then(resp => `${this.options.baseApiUrl}/file/bot${this.token}/${resp.file_path}`); } /**