mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-10-17 14:36:28 +00:00
src: Minor reorganisation, fixes
This commit is contained in:
14
doc/usage.md
14
doc/usage.md
@@ -117,16 +117,16 @@ bot.sendPhoto(chatId, url);
|
||||
|
||||
If you wish to explicitly specify the filename or
|
||||
[MIME type](http://en.wikipedia.org/wiki/Internet_media_type),
|
||||
you may pass the an additional argument as file options, like so:
|
||||
you may pass an additional argument as file options, like so:
|
||||
|
||||
```js
|
||||
const fileOpts = {
|
||||
const fileOptions = {
|
||||
// Explicitly specify the file name.
|
||||
filename: 'customfilename',
|
||||
// Explicitly specify the MIME type.
|
||||
contentType: 'audio/mpeg'
|
||||
contentType: 'audio/mpeg',
|
||||
};
|
||||
bot.sendAudio(chatId, data, {}, fileOpts);
|
||||
bot.sendAudio(chatId, data, {}, fileOptions);
|
||||
```
|
||||
|
||||
<a name="sending-files-options"></a>
|
||||
@@ -140,6 +140,10 @@ variable `NTBA_FIX_350`.**
|
||||
In order of highest-to-lowest precedence in searching for
|
||||
a value, when resolving the `filename`:
|
||||
|
||||
*(`fileOptions` is the Object argument passed to the method.
|
||||
The "file" argument passed to the method can be a `Stream`,
|
||||
`Buffer` or `filepath`.)*
|
||||
|
||||
1. Is `fileOptions.filename` explictly defined?
|
||||
1. Does `Stream#path` exist?
|
||||
1. Is `filepath` provided?
|
||||
@@ -152,7 +156,7 @@ And the `contentType`:
|
||||
1. Try detecting file-type from the `Buffer`
|
||||
1. Is `filepath` provided?
|
||||
1. Is `fileOptions.filename` explicitly defined?
|
||||
1. Default to `"application/octet-stream`
|
||||
1. Default to `"application/octet-stream"`
|
||||
|
||||
<a name="sending-files-performance"></a>
|
||||
### Performance Issue
|
||||
|
@@ -1332,33 +1332,7 @@ describe('TelegramBot', function telegramSuite() {
|
||||
});
|
||||
|
||||
describe('#_formatSendData', function _formatSendDataSuite() {
|
||||
it('should handle buffer path from fs.readStream', function test() {
|
||||
let photo;
|
||||
try {
|
||||
photo = fs.createReadStream(Buffer.from(`${__dirname}/data/photo.gif`));
|
||||
} catch (ex) {
|
||||
// Older Node.js versions do not support passing a Buffer
|
||||
// representation of the path to fs.createReadStream()
|
||||
if (ex instanceof TypeError) return Promise.resolve();
|
||||
}
|
||||
return bot.sendPhoto(USERID, photo).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
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.description.indexOf('Bad Request') !== -1);
|
||||
});
|
||||
});
|
||||
it('should allow stream.path that can not be parsed', function test() {
|
||||
const stream = fs.createReadStream(`${__dirname}/data/photo.gif`);
|
||||
stream.path = '/?id=123'; // for example, 'http://example.com/?id=666'
|
||||
return bot.sendPhoto(USERID, stream);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#sendInvoice', function sendInvoiceSuite() {
|
||||
|
@@ -8,8 +8,9 @@ const paths = {
|
||||
};
|
||||
|
||||
|
||||
describe('sending files', function sendfileSuite() {
|
||||
const bot = new TelegramBot('token ');
|
||||
describe('#_formatSendData', function sendfileSuite() {
|
||||
const bot = new TelegramBot('token');
|
||||
const type = 'file';
|
||||
|
||||
before(function beforeSuite() {
|
||||
process.env.NTBA_FIX_350 = 1;
|
||||
@@ -19,7 +20,6 @@ describe('sending files', function sendfileSuite() {
|
||||
});
|
||||
|
||||
describe('using fileOptions', function sendfileOptionsSuite() {
|
||||
const type = 'file';
|
||||
const stream = fs.createReadStream(paths.audio);
|
||||
const nonPathStream = fs.createReadStream(paths.audio);
|
||||
const buffer = fs.readFileSync(paths.audio);
|
||||
@@ -105,4 +105,35 @@ describe('sending files', function sendfileSuite() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle buffer path from fs.readStream', function test() {
|
||||
let file;
|
||||
try {
|
||||
file = fs.createReadStream(Buffer.from(paths.audio));
|
||||
} catch (ex) {
|
||||
// Older Node.js versions do not support passing a Buffer
|
||||
// representation of the path to fs.createReadStream()
|
||||
if (ex instanceof TypeError) {
|
||||
Promise.resolve();
|
||||
return;
|
||||
}
|
||||
}
|
||||
const [{ [type]: data }] = bot._formatSendData('file', file);
|
||||
assert.equal(data.options.filename, path.basename(paths.audio));
|
||||
});
|
||||
|
||||
it('should not accept file-paths if disallowed with constructor option', function test() {
|
||||
const tgbot = new TelegramBot('token', { filepath: false });
|
||||
const [formData, fileId] = tgbot._formatSendData('file', paths.audio);
|
||||
assert.ok(fileId);
|
||||
assert.ok(!formData);
|
||||
});
|
||||
|
||||
it('should allow stream.path that can not be parsed', function test() {
|
||||
const stream = fs.createReadStream(paths.audio);
|
||||
stream.path = '/?id=123'; // for example, 'http://example.com/?id=666'
|
||||
assert.doesNotThrow(function assertDoesNotThrow() {
|
||||
bot._formatSendData('file', stream);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user