diff --git a/README.md b/README.md index 275fc94..3d59a4f 100644 --- a/README.md +++ b/README.md @@ -279,4 +279,21 @@ See: https://core.telegram.org/bots/api#getfile * **Promise** +## getFileLink(fileId) + +Get link for file. +Use this method to get link for file for subsequent use. +Attention: link will be valid for 1 hour. + +This method is a sugar extension of the (getFile)[#getfilefileid] method, which returns just path to file on remote server (you will have to manually build full uri after that). + +See: https://core.telegram.org/bots/api#getfile + +### Params: +* **String** *fileId* File identifier to get info about + +### Return: + +* **Promise** *promise* Promise which will have *fileURI* in resolve callback + diff --git a/src/telegram.js b/src/telegram.js index e14972c..8a538be 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -82,11 +82,7 @@ TelegramBot.prototype._request = function (path, options) { throw new Error('Telegram Bot Token not provided!'); } options = options || {}; - options.url = URL.format({ - protocol: 'https', - host: 'api.telegram.org', - pathname: '/bot'+this.token+'/'+path - }); + options.url = this._buildURL(path); debug('HTTP request: %j', options); return requestPromise(options) .then(function (resp) { @@ -102,6 +98,20 @@ TelegramBot.prototype._request = function (path, options) { }); }; +/** + * Generates url with bot token and provided path/method you want to be got/executed by bot + * @return {String} url + * @param {String} path + * @see https://core.telegram.org/bots/api#making-requests + */ +TelegramBot.prototype._buildURL = function(path) { + return URL.format({ + protocol: 'https', + host: 'api.telegram.org', + pathname: '/bot' + this.token + '/' + path + }); +} + /** * Returns basic information about the bot in form of a `User` object. * @return {Promise} @@ -405,4 +415,32 @@ TelegramBot.prototype.getFile = function(fileId) { return this._request('getFile', {qs: query}); }; +/** + * Get link for file. + * Use this method to get link for file for subsequent use. + * Attention: link will be valid for 1 hour. + * + * This method is a sugar extension of the (getFile)[#getfilefileid] method, which returns just path to file on remote server (you will have to manually build full uri after that). + * + * @param {String} fileId File identifier to get info about + * @return {Promise} promise Promise which will have *fileURI* in resolve callback + * @see https://core.telegram.org/bots/api#getfile + */ +TelegramBot.prototype.getFileLink = function(fileId) { + + var bot = this; + + return new Promise(function(resolve) { + bot.getFile(fileId).then(function(resp) { + var fileURI = URL.format({ + protocol: 'https', + host: 'api.telegram.org', + pathname: '/file/bot' + bot.token + '/' + resp.file_path + }); + + resolve(fileURI); + }) + }); +}; + module.exports = TelegramBot; diff --git a/test/index.js b/test/index.js index cef7065..36858f5 100644 --- a/test/index.js +++ b/test/index.js @@ -394,6 +394,32 @@ describe('Telegram', function () { }); }); + describe('#getFileLink', function () { + var fileId; + + // To get a file we have to send any 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 get a file link', function (done) { + + var bot = new Telegram(TOKEN); + + bot.getFileLink(fileId).then(function (fileURI) { + fileURI.should.be.an.instanceOf(String); + fileURI.should.startWith('https'); + done(); // TODO: validate URL with some library or regexp + }); + }); + }); + }); // End Telegram