2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-28 12:57:38 +00:00

Merge pull request #5 from Waterloo/master

Add sendDocument method
This commit is contained in:
Yago 2015-07-07 11:29:36 +02:00
commit f30393401e
2 changed files with 62 additions and 0 deletions

View File

@ -295,6 +295,26 @@ TelegramBot.prototype.sendAudio = function (chatId, audio, options) {
return this._request('sendAudio', opts);
};
/**
* Send Document
* @param {Number|String} chatId Unique identifier for the message recipient
* @param {String|stream.Stream} A file path or a Stream. Can
* also be a `file_id` previously uploaded.
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#sendDocument
*/
TelegramBot.prototype.sendDocument = function (chatId, doc, options) {
var opts = {
qs: options || {}
};
opts.qs.chat_id = chatId;
var content = this._formatSendData('document', doc);
opts.formData = content[0];
opts.qs.document = content[1];
return this._request('sendDocument', opts);
};
/**
* Send chat action.
* `typing` for text messages,

View File

@ -203,3 +203,45 @@ describe('Telegram', function () {
});
describe('#sendDocument', function () {
var documentId;
it('should send a document from file', function (done) {
var bot = new Telegram(TOKEN);
var document = __dirname+'/bot.gif';
bot.sendDocument(USERID, document).then(function (resp) {
resp.should.be.an.instanceOf(Object);
documentId = resp.document.file_id;
done();
});
});
it('should send a document from id', function (done) {
var bot = new Telegram(TOKEN);
// Send the same photo as before
var document = documentId;
bot.sendPhoto(USERID, document).then(function (resp) {
resp.should.be.an.instanceOf(Object);
done();
});
});
it('should send a document from fs.readStream', function (done) {
var bot = new Telegram(TOKEN);
var document = fs.createReadStream(__dirname+'/bot.gif');
bot.sendDocument(USERID, document).then(function (resp) {
resp.should.be.an.instanceOf(Object);
done();
});
});
it('should send a document from request Stream', function (done) {
var bot = new Telegram(TOKEN);
var document = request('https://telegram.org/img/t_logo.png');
bot.sendDocument(USERID, document).then(function (resp) {
resp.should.be.an.instanceOf(Object);
done();
});
});
});