mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-29 21:37:58 +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**
|
* **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 -->
|
<!-- End src/telegram.js -->
|
||||||
|
@ -82,11 +82,7 @@ TelegramBot.prototype._request = function (path, options) {
|
|||||||
throw new Error('Telegram Bot Token not provided!');
|
throw new Error('Telegram Bot Token not provided!');
|
||||||
}
|
}
|
||||||
options = options || {};
|
options = options || {};
|
||||||
options.url = URL.format({
|
options.url = this._buildURL(path);
|
||||||
protocol: 'https',
|
|
||||||
host: 'api.telegram.org',
|
|
||||||
pathname: '/bot'+this.token+'/'+path
|
|
||||||
});
|
|
||||||
debug('HTTP request: %j', options);
|
debug('HTTP request: %j', options);
|
||||||
return requestPromise(options)
|
return requestPromise(options)
|
||||||
.then(function (resp) {
|
.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.
|
* Returns basic information about the bot in form of a `User` object.
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
@ -405,4 +415,32 @@ TelegramBot.prototype.getFile = function(fileId) {
|
|||||||
return this._request('getFile', {qs: query});
|
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;
|
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
|
}); // End Telegram
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user