mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-28 12:57:38 +00:00
[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!
This commit is contained in:
parent
e009b60efa
commit
0d20565bd5
@ -144,6 +144,7 @@ Emits `message` when a message arrives.
|
||||
| [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.baseApiUrl] | <code>String</code> | <code>https://api.telegram.org</code> | API Base URl; useful for proxying and testing |
|
||||
| [options.filepath] | <code>Boolean</code> | <code>true</code> | Allow passing file-paths as arguments when sending files, such as photos using `TelegramBot#sendPhoto()`. |
|
||||
|
||||
<a name="TelegramBot+startPolling"></a>
|
||||
|
||||
|
@ -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 = {};
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user