diff --git a/src/telegram.js b/src/telegram.js index fc06d3d..dd0516c 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -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, diff --git a/test/index.js b/test/index.js index 29946bb..cd4adb2 100644 --- a/test/index.js +++ b/test/index.js @@ -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(); + }); + }); + });