mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-28 21:07:39 +00:00
src/telegram: Add TelegramBot#getFileStream()
References: * FR: https://github.com/yagop/node-telegram-bot-api/issues/442
This commit is contained in:
parent
0870684d83
commit
d9692f45a9
@ -12,6 +12,8 @@ Added:
|
|||||||
* (#440) *TelegramBot#setChatStickerSet*, *TelegramBot#deleteChatStickerSet* (by @kamikazechaser)
|
* (#440) *TelegramBot#setChatStickerSet*, *TelegramBot#deleteChatStickerSet* (by @kamikazechaser)
|
||||||
1. Support Bot API v3.5:
|
1. Support Bot API v3.5:
|
||||||
* Support `provider_data` parameter in *TelegramBot#sendInvoice* (by @GochoMugo)
|
* Support `provider_data` parameter in *TelegramBot#sendInvoice* (by @GochoMugo)
|
||||||
|
1. Add methods:
|
||||||
|
* *TelegramBot#getFileStream* (#442) (by @GochoMugo, requested-by @Xaqron)
|
||||||
1. Add options to *TelegramBot#stopPolling()* (by @GochoMugo)
|
1. Add options to *TelegramBot#stopPolling()* (by @GochoMugo)
|
||||||
1. Add `metadata` argument in `message` event (and
|
1. Add `metadata` argument in `message` event (and
|
||||||
friends e.g. `text`, `audio`, etc.) (#409) (by @jlsjonas, @GochoMugo)
|
friends e.g. `text`, `audio`, etc.) (#409) (by @jlsjonas, @GochoMugo)
|
||||||
|
17
doc/api.md
17
doc/api.md
@ -61,6 +61,7 @@ TelegramBot
|
|||||||
* [.sendContact(chatId, phoneNumber, firstName, [options])](#TelegramBot+sendContact) ⇒ <code>Promise</code>
|
* [.sendContact(chatId, phoneNumber, firstName, [options])](#TelegramBot+sendContact) ⇒ <code>Promise</code>
|
||||||
* [.getFile(fileId, [options])](#TelegramBot+getFile) ⇒ <code>Promise</code>
|
* [.getFile(fileId, [options])](#TelegramBot+getFile) ⇒ <code>Promise</code>
|
||||||
* [.getFileLink(fileId, [options])](#TelegramBot+getFileLink) ⇒ <code>Promise</code>
|
* [.getFileLink(fileId, [options])](#TelegramBot+getFileLink) ⇒ <code>Promise</code>
|
||||||
|
* [.getFileStream(fileId, [options])](#TelegramBot+getFileStream) ⇒ <code>stream.Readable</code>
|
||||||
* [.downloadFile(fileId, downloadDir, [options])](#TelegramBot+downloadFile) ⇒ <code>Promise</code>
|
* [.downloadFile(fileId, downloadDir, [options])](#TelegramBot+downloadFile) ⇒ <code>Promise</code>
|
||||||
* [.onText(regexp, callback)](#TelegramBot+onText)
|
* [.onText(regexp, callback)](#TelegramBot+onText)
|
||||||
* [.removeTextListener(regexp)](#TelegramBot+removeTextListener) ⇒ <code>Object</code>
|
* [.removeTextListener(regexp)](#TelegramBot+removeTextListener) ⇒ <code>Object</code>
|
||||||
@ -841,6 +842,22 @@ which returns just path to file on remote server (you will have to manually buil
|
|||||||
| fileId | <code>String</code> | File identifier to get info about |
|
| fileId | <code>String</code> | File identifier to get info about |
|
||||||
| [options] | <code>Object</code> | Additional Telegram query options |
|
| [options] | <code>Object</code> | Additional Telegram query options |
|
||||||
|
|
||||||
|
<a name="TelegramBot+getFileStream"></a>
|
||||||
|
|
||||||
|
### telegramBot.getFileStream(fileId, [options]) ⇒ <code>stream.Readable</code>
|
||||||
|
Return a readable stream for file.
|
||||||
|
|
||||||
|
This method is a sugar extension of the [getFileLink](#TelegramBot+getFileLink) method,
|
||||||
|
which returns the full URI to the file on remote server.
|
||||||
|
|
||||||
|
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
||||||
|
**Returns**: <code>stream.Readable</code> - stream
|
||||||
|
|
||||||
|
| Param | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| fileId | <code>String</code> | File identifier to get info about |
|
||||||
|
| [options] | <code>Object</code> | Additional Telegram query options |
|
||||||
|
|
||||||
<a name="TelegramBot+downloadFile"></a>
|
<a name="TelegramBot+downloadFile"></a>
|
||||||
|
|
||||||
### telegramBot.downloadFile(fileId, downloadDir, [options]) ⇒ <code>Promise</code>
|
### telegramBot.downloadFile(fileId, downloadDir, [options]) ⇒ <code>Promise</code>
|
||||||
|
@ -53,6 +53,7 @@
|
|||||||
"babel-plugin-transform-strict-mode": "^6.6.5",
|
"babel-plugin-transform-strict-mode": "^6.6.5",
|
||||||
"babel-preset-es2015": "^6.6.0",
|
"babel-preset-es2015": "^6.6.0",
|
||||||
"babel-register": "^6.7.2",
|
"babel-register": "^6.7.2",
|
||||||
|
"concat-stream": "^1.6.0",
|
||||||
"contributor": "^0.1.25",
|
"contributor": "^0.1.25",
|
||||||
"eslint": "^2.13.1",
|
"eslint": "^2.13.1",
|
||||||
"eslint-config-airbnb": "^6.2.0",
|
"eslint-config-airbnb": "^6.2.0",
|
||||||
|
@ -1322,6 +1322,28 @@ class TelegramBot extends EventEmitter {
|
|||||||
.then(resp => `${this.options.baseApiUrl}/file/bot${this.token}/${resp.file_path}`);
|
.then(resp => `${this.options.baseApiUrl}/file/bot${this.token}/${resp.file_path}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a readable stream for file.
|
||||||
|
*
|
||||||
|
* This method is a sugar extension of the [getFileLink](#TelegramBot+getFileLink) method,
|
||||||
|
* which returns the full URI to the file on remote server.
|
||||||
|
*
|
||||||
|
* @param {String} fileId File identifier to get info about
|
||||||
|
* @param {Object} [options] Additional Telegram query options
|
||||||
|
* @return {stream.Readable} stream
|
||||||
|
*/
|
||||||
|
getFileStream(fileId, form = {}) {
|
||||||
|
const fileStream = new stream.PassThrough();
|
||||||
|
this.getFileLink(fileId, form)
|
||||||
|
.then((fileURI) => {
|
||||||
|
pump(streamedRequest({ uri: fileURI }), fileStream);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
fileStream.emit('error', error);
|
||||||
|
});
|
||||||
|
return fileStream;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads file in the specified folder.
|
* Downloads file in the specified folder.
|
||||||
* This is just a sugar for (getFile)[#getfilefiled] method
|
* This is just a sugar for (getFile)[#getfilefiled] method
|
||||||
|
@ -5,9 +5,11 @@ const assert = require('assert');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const stream = require('stream');
|
||||||
const is = require('is');
|
const is = require('is');
|
||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
const isCI = require('is-ci');
|
const isCI = require('is-ci');
|
||||||
|
const concat = require('concat-stream');
|
||||||
|
|
||||||
// Allows self-signed certificates to be used in our tests
|
// Allows self-signed certificates to be used in our tests
|
||||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
||||||
@ -41,6 +43,7 @@ const ip = '216.58.210.174'; // Google IP ¯\_(ツ)_/¯
|
|||||||
const cert = `${__dirname}/../examples/crt.pem`;
|
const cert = `${__dirname}/../examples/crt.pem`;
|
||||||
const lat = 47.5351072;
|
const lat = 47.5351072;
|
||||||
const long = -52.7508537;
|
const long = -52.7508537;
|
||||||
|
const FILE_PATH = `${__dirname}/data/photo.gif`;
|
||||||
let FILE_ID;
|
let FILE_ID;
|
||||||
let GAME_CHAT_ID;
|
let GAME_CHAT_ID;
|
||||||
let GAME_MSG_ID;
|
let GAME_MSG_ID;
|
||||||
@ -101,7 +104,7 @@ describe('TelegramBot', function telegramSuite() {
|
|||||||
utils.handleRatelimit(bot, 'sendPhoto', this);
|
utils.handleRatelimit(bot, 'sendPhoto', this);
|
||||||
utils.handleRatelimit(bot, 'sendMessage', this);
|
utils.handleRatelimit(bot, 'sendMessage', this);
|
||||||
utils.handleRatelimit(bot, 'sendGame', this);
|
utils.handleRatelimit(bot, 'sendGame', this);
|
||||||
return bot.sendPhoto(USERID, `${__dirname}/data/photo.gif`).then(resp => {
|
return bot.sendPhoto(USERID, FILE_PATH).then(resp => {
|
||||||
FILE_ID = resp.photo[0].file_id;
|
FILE_ID = resp.photo[0].file_id;
|
||||||
return bot.sendMessage(USERID, 'chat');
|
return bot.sendMessage(USERID, 'chat');
|
||||||
}).then(resp => {
|
}).then(resp => {
|
||||||
@ -1162,6 +1165,21 @@ describe('TelegramBot', function telegramSuite() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#getFileStream', function getFileStreamSuite() {
|
||||||
|
this.timeout(timeout);
|
||||||
|
before(function before() {
|
||||||
|
// utils.handleRatelimit(bot, 'getFileStream', this);
|
||||||
|
});
|
||||||
|
it('should get a file stream', function test(done) {
|
||||||
|
const fileStream = bot.getFileStream(FILE_ID);
|
||||||
|
assert.ok(fileStream instanceof stream.Readable);
|
||||||
|
fileStream.pipe(concat(function readFile(buffer) {
|
||||||
|
buffer.equals(fs.readFileSync(FILE_PATH)); // sync :(
|
||||||
|
return done();
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#downloadFile', function downloadFileSuite() {
|
describe('#downloadFile', function downloadFileSuite() {
|
||||||
const downloadPath = os.tmpdir();
|
const downloadPath = os.tmpdir();
|
||||||
this.timeout(timeout);
|
this.timeout(timeout);
|
||||||
@ -1362,9 +1380,9 @@ describe('TelegramBot', function telegramSuite() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should allow stream.path that can not be parsed', function test() {
|
it('should allow stream.path that can not be parsed', function test() {
|
||||||
const stream = fs.createReadStream(`${__dirname}/data/photo.gif`);
|
const fileStream = fs.createReadStream(`${__dirname}/data/photo.gif`);
|
||||||
stream.path = '/?id=123'; // for example, 'http://example.com/?id=666'
|
fileStream.path = '/?id=123'; // for example, 'http://example.com/?id=666'
|
||||||
return bot.sendPhoto(USERID, stream);
|
return bot.sendPhoto(USERID, fileStream);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user