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