From 14dbd12f68caa25bd64f12d76e6e632c6a4f6295 Mon Sep 17 00:00:00 2001 From: Ivan Skorokhodov Date: Sat, 26 Sep 2015 19:52:17 +0300 Subject: [PATCH] Added downloadFile sugar method --- README.md | 13 +++++++++++++ package.json | 1 + src/telegram.js | 29 +++++++++++++++++++++++++++++ test/index.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/README.md b/README.md index 3d59a4f..defea77 100644 --- a/README.md +++ b/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 + diff --git a/package.json b/package.json index a8096df..4b19852 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/telegram.js b/src/telegram.js index 96e1e7a..c4c8c87 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -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; diff --git a/test/index.js b/test/index.js index 36858f5..8e9bb42 100644 --- a/test/index.js +++ b/test/index.js @@ -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