mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-29 13:27:44 +00:00
src/telegram: Emit 'info' on stream from TelegramBot#getFileStream()
This commit is contained in:
parent
b968e893d3
commit
04e8b892aa
@ -846,7 +846,11 @@ which returns just path to file on remote server (you will have to manually buil
|
||||
|
||||
### telegramBot.getFileStream(fileId, [options]) ⇒ <code>stream.Readable</code>
|
||||
Return a readable stream for file.
|
||||
|
||||
`fileStream.path` is the specified file ID i.e. `fileId`.
|
||||
`fileStream` emits event `info` passing a single argument i.e.
|
||||
`info` with the interface `{ uri }` where `uri` is the URI of the
|
||||
file on Telegram servers.
|
||||
|
||||
This method is a sugar extension of the [getFileLink](#TelegramBot+getFileLink) method,
|
||||
which returns the full URI to the file on remote server.
|
||||
|
@ -1324,7 +1324,11 @@ class TelegramBot extends EventEmitter {
|
||||
|
||||
/**
|
||||
* Return a readable stream for file.
|
||||
*
|
||||
* `fileStream.path` is the specified file ID i.e. `fileId`.
|
||||
* `fileStream` emits event `info` passing a single argument i.e.
|
||||
* `info` with the interface `{ uri }` where `uri` is the URI of the
|
||||
* file on Telegram servers.
|
||||
*
|
||||
* This method is a sugar extension of the [getFileLink](#TelegramBot+getFileLink) method,
|
||||
* which returns the full URI to the file on remote server.
|
||||
@ -1338,6 +1342,9 @@ class TelegramBot extends EventEmitter {
|
||||
fileStream.path = fileId;
|
||||
this.getFileLink(fileId, form)
|
||||
.then((fileURI) => {
|
||||
fileStream.emit('info', {
|
||||
uri: fileURI,
|
||||
});
|
||||
pump(streamedRequest({ uri: fileURI }), fileStream);
|
||||
})
|
||||
.catch((error) => {
|
||||
|
@ -1160,7 +1160,7 @@ describe('TelegramBot', function telegramSuite() {
|
||||
return bot.getFileLink(FILE_ID)
|
||||
.then(fileURI => {
|
||||
assert.ok(is.string(fileURI));
|
||||
assert.ok(/https?:\/\/.*\/file\/bot.*\/.*/.test(fileURI));
|
||||
assert.ok(utils.isTelegramFileURI(fileURI));
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1174,10 +1174,14 @@ describe('TelegramBot', function telegramSuite() {
|
||||
const fileStream = bot.getFileStream(FILE_ID);
|
||||
assert.ok(fileStream instanceof stream.Readable);
|
||||
assert.equal(fileStream.path, FILE_ID);
|
||||
fileStream.pipe(concat(function readFile(buffer) {
|
||||
buffer.equals(fs.readFileSync(FILE_PATH)); // sync :(
|
||||
return done();
|
||||
}));
|
||||
fileStream.on('info', (info) => {
|
||||
assert.ok(info);
|
||||
assert.ok(utils.isTelegramFileURI(info.uri), `${info.uri} is not a file URI`);
|
||||
fileStream.pipe(concat(function readFile(buffer) {
|
||||
buffer.equals(fs.readFileSync(FILE_PATH)); // sync :(
|
||||
return done();
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -30,6 +30,13 @@ exports = module.exports = {
|
||||
* @return {Promise}
|
||||
*/
|
||||
isPollingMockServer,
|
||||
/**
|
||||
* Return true if the string is a URI to a file
|
||||
* on Telegram servers.
|
||||
* @param {String} uri
|
||||
* @return {Boolean}
|
||||
*/
|
||||
isTelegramFileURI,
|
||||
/**
|
||||
* Send a message to the webhook at the specified port and path.
|
||||
* @param {Number} port
|
||||
@ -217,3 +224,8 @@ function handleRatelimit(bot, methodName, suite) {
|
||||
};
|
||||
return bot;
|
||||
}
|
||||
|
||||
|
||||
function isTelegramFileURI(uri) {
|
||||
return /https?:\/\/.*\/file\/bot.*\/.*/.test(uri);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user