From 0d20565bd5fa1e1eff1c5ebef86c691992056951 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Wed, 11 Jan 2017 11:06:41 +0300 Subject: [PATCH] [telegram] Add constructor option, 'filepath' Feature: The constructor option, 'filepath', allows us to remove the TelegramBot's behaviour of allowing users to pass in file-paths as arguments, as it involves some operations that might be (are) strongly against what a programmer wishes to achieve with the library. Expect this to be documented better in the near future. Implementation: * Backwards compatible: The default behavior is not changed! --- README.md | 1 + src/telegram.js | 9 +++++++++ test/telegram.js | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index 0dbd431..30afaba 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ Emits `message` when a message arrives. | [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 | +| [options.filepath] | Boolean | true | Allow passing file-paths as arguments when sending files, such as photos using `TelegramBot#sendPhoto()`. | diff --git a/src/telegram.js b/src/telegram.js index f3da590..238587d 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -63,6 +63,8 @@ class TelegramBot extends EventEmitter { * @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 https://core.telegram.org/bots/api */ constructor(token, options = {}) { @@ -70,6 +72,7 @@ class TelegramBot extends EventEmitter { this.token = token; this.options = options; this.options.baseApiUrl = options.baseApiUrl || 'https://api.telegram.org'; + this.options.filepath = typeof options.filepath === 'undefined' ? true : options.filepath; this._textRegexpCallbacks = []; this._onReplyToMessages = []; this._polling = null; @@ -210,6 +213,12 @@ class TelegramBot extends EventEmitter { contentType: filetype.mime } }; + } else if (!this.options.filepath) { + /** + * When the constructor option 'filepath' is set to + * 'false', we do not support passing file-paths. + */ + fileId = data; } else if (fs.existsSync(data)) { fileName = path.basename(data); formData = {}; diff --git a/test/telegram.js b/test/telegram.js index 2bc343f..0928a5d 100644 --- a/test/telegram.js +++ b/test/telegram.js @@ -971,5 +971,13 @@ describe('TelegramBot', function telegramSuite() { assert.ok(is.array(resp.photo)); }); }); + it('should not accept file-paths if disallowed with constructor option', function test() { + const tgbot = new TelegramBot(TOKEN, { filepath: false }); + const photo = `${__dirname}/data/photo.gif`; + return tgbot.sendPhoto(USERID, photo).catch(err => { + // TODO: check for error in a better way + assert.ok(err.response.body.indexOf('Bad Request') !== -1); + }); + }); }); }); // End Telegram