mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-29 13:27:44 +00:00
Merge branch 'getFileLink' of https://github.com/universome/node-telegram-bot-api into universome-getFileLink
This commit is contained in:
commit
ea61696275
17
README.md
17
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
|
||||
|
||||
<!-- End 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;
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user