mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-28 12:57:38 +00:00
Added downloadFile sugar method
This commit is contained in:
parent
6006ba4fd6
commit
14dbd12f68
13
README.md
13
README.md
@ -296,4 +296,17 @@ See: https://core.telegram.org/bots/api#getfile
|
||||
|
||||
* **Promise** *promise* Promise which will have *fileURI* in resolve callback
|
||||
|
||||
## downloadFile(fileId, downloadDir)
|
||||
|
||||
Downloads file in the specified folder.
|
||||
This is just a sugar for (getFile)[#getfilefiled] method
|
||||
|
||||
### Params:
|
||||
* **String** *fileId* File identifier to get info about
|
||||
* **String** *downloadDir* Absolute path to the folder in which file will be saved
|
||||
|
||||
### Return:
|
||||
|
||||
* **Promise** *promise* Promise, which will have *filePath* of downloaded file in resolve callback
|
||||
|
||||
<!-- End src/telegram.js -->
|
||||
|
@ -27,6 +27,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"coveralls": "^2.11.2",
|
||||
"del": "^2.0.2",
|
||||
"istanbul": "^0.3.17",
|
||||
"mocha": "^2.2.5",
|
||||
"mocha-lcov-reporter": "0.0.2",
|
||||
|
@ -434,4 +434,33 @@ TelegramBot.prototype.getFileLink = function(fileId) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Downloads file in the specified folder.
|
||||
* This is just a sugar for (getFile)[#getfilefiled] method
|
||||
*
|
||||
* @param {String} fileId File identifier to get info about
|
||||
* @param {String} downloadDir Absolute path to the folder in which file will be saved
|
||||
* @return {Promise} promise Promise, which will have *filePath* of downloaded file in resolve callback
|
||||
*/
|
||||
TelegramBot.prototype.downloadFile = function(fileId, downloadDir) {
|
||||
|
||||
var bot = this;
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
||||
bot.getFileLink(fileId).then(function(fileURI) {
|
||||
// Apparently, this is not the safest method to get the file name,
|
||||
// but I am pretty sure that Telegram will not include slashes when generating names
|
||||
var fileName = fileURI.slice(fileURI.lastIndexOf('/') + 1);
|
||||
var filePath = downloadDir + '/' + fileName;
|
||||
|
||||
request({uri: fileURI})
|
||||
.pipe(fs.createWriteStream(filePath))
|
||||
.on('close', function() {
|
||||
resolve(filePath);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = TelegramBot;
|
||||
|
@ -2,6 +2,7 @@ var TelegramPolling = require('../src/telegramPolling');
|
||||
var Telegram = require('../index');
|
||||
var request = require('request');
|
||||
var should = require('should');
|
||||
var del = require('del');
|
||||
var fs = require('fs');
|
||||
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
||||
@ -420,6 +421,38 @@ describe('Telegram', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#downloadFile', function () {
|
||||
|
||||
var fileId;
|
||||
var downloadPath = __dirname;
|
||||
|
||||
// To get a file we have to send some file first
|
||||
it('should send a photo from file', function (done) {
|
||||
|
||||
var bot = new Telegram(TOKEN);
|
||||
var photo = __dirname + '/bot.gif';
|
||||
|
||||
bot.sendPhoto(USERID, photo).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
fileId = resp.photo[0].file_id;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should download a file', function (done) {
|
||||
|
||||
var bot = new Telegram(TOKEN);
|
||||
|
||||
bot.downloadFile(fileId, downloadPath).then(function (filePath) {
|
||||
filePath.should.be.an.instanceOf(String);
|
||||
fs.existsSync(filePath).should.be.true();
|
||||
del(filePath); // Clean folder after test
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
}); // End Telegram
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user