2
0
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:
GochoMugo 2017-12-08 19:25:56 +03:00
parent b968e893d3
commit 04e8b892aa
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
4 changed files with 32 additions and 5 deletions

View File

@ -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> ### telegramBot.getFileStream(fileId, [options]) ⇒ <code>stream.Readable</code>
Return a readable stream for file. Return a readable stream for file.
`fileStream.path` is the specified file ID i.e. `fileId`. `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, This method is a sugar extension of the [getFileLink](#TelegramBot+getFileLink) method,
which returns the full URI to the file on remote server. which returns the full URI to the file on remote server.

View File

@ -1324,7 +1324,11 @@ class TelegramBot extends EventEmitter {
/** /**
* Return a readable stream for file. * Return a readable stream for file.
*
* `fileStream.path` is the specified file ID i.e. `fileId`. * `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, * This method is a sugar extension of the [getFileLink](#TelegramBot+getFileLink) method,
* which returns the full URI to the file on remote server. * which returns the full URI to the file on remote server.
@ -1338,6 +1342,9 @@ class TelegramBot extends EventEmitter {
fileStream.path = fileId; fileStream.path = fileId;
this.getFileLink(fileId, form) this.getFileLink(fileId, form)
.then((fileURI) => { .then((fileURI) => {
fileStream.emit('info', {
uri: fileURI,
});
pump(streamedRequest({ uri: fileURI }), fileStream); pump(streamedRequest({ uri: fileURI }), fileStream);
}) })
.catch((error) => { .catch((error) => {

View File

@ -1160,7 +1160,7 @@ describe('TelegramBot', function telegramSuite() {
return bot.getFileLink(FILE_ID) return bot.getFileLink(FILE_ID)
.then(fileURI => { .then(fileURI => {
assert.ok(is.string(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); const fileStream = bot.getFileStream(FILE_ID);
assert.ok(fileStream instanceof stream.Readable); assert.ok(fileStream instanceof stream.Readable);
assert.equal(fileStream.path, FILE_ID); assert.equal(fileStream.path, FILE_ID);
fileStream.pipe(concat(function readFile(buffer) { fileStream.on('info', (info) => {
buffer.equals(fs.readFileSync(FILE_PATH)); // sync :( assert.ok(info);
return done(); 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();
}));
});
}); });
}); });

View File

@ -30,6 +30,13 @@ exports = module.exports = {
* @return {Promise} * @return {Promise}
*/ */
isPollingMockServer, 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. * Send a message to the webhook at the specified port and path.
* @param {Number} port * @param {Number} port
@ -217,3 +224,8 @@ function handleRatelimit(bot, methodName, suite) {
}; };
return bot; return bot;
} }
function isTelegramFileURI(uri) {
return /https?:\/\/.*\/file\/bot.*\/.*/.test(uri);
}