From b34caa84c582346393c634b1a085e929b74d8a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patricio=20L=C3=B3pez?= Date: Tue, 15 Sep 2015 17:55:02 -0300 Subject: [PATCH] added sendVoice function --- README.md | 18 +++++++++++++++++- src/telegram.js | 22 +++++++++++++++++++++- test/index.js | 11 +++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index daa592b..2b5dd71 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ bot.on('text', function (msg) { There are some other examples on [examples](https://github.com/yagop/node-telegram-bot-api/tree/master/examples). ### Events -Every time TelegramBot receives a message, it emits a `message`. Depending on which [message](https://core.telegram.org/bots/api#message) was received, emits an event from this ones: `text`, `audio`, `document`, `photo`, `sticker`, `video`, `contact`, `location`, `new_chat_participant`, `left_chat_participant`, `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`. Its much better to listen a specific event rather than a `message` in order to stay safe from the content. +Every time TelegramBot receives a message, it emits a `message`. Depending on which [message](https://core.telegram.org/bots/api#message) was received, emits an event from this ones: `text`, `audio`, `document`, `photo`, `sticker`, `video`, `voice`, `contact`, `location`, `new_chat_participant`, `left_chat_participant`, `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`. Its much better to listen a specific event rather than a `message` in order to stay safe from the content. * * * @@ -194,6 +194,22 @@ See: https://core.telegram.org/bots/api#sendvideo * **Promise** +## sendVoice(chatId, A, [options]) + +Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). + +See: https://core.telegram.org/bots/api#sendvoice + +### Params: + +* **Number|String** *chatId* Unique identifier for the message recipient +* **String|stream.Stream** *A* file path or a Stream. Can also be a `file_id` previously uploaded. +* **Object** *[options]* Additional Telegram query options + +### Return: + +* **Promise** + ## sendChatAction(chatId, action) Send chat action. diff --git a/src/telegram.js b/src/telegram.js index 9f20d1a..a54a447 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -34,7 +34,7 @@ var TelegramBot = function (token, options) { options = options || {}; this.token = token; this.messageTypes = [ - 'text', 'audio', 'document', 'photo', 'sticker', 'video', 'contact', + 'text', 'audio', 'document', 'photo', 'sticker', 'video', 'voice', 'contact', 'location', 'new_chat_participant', 'left_chat_participant', 'new_chat_title', 'new_chat_photo', 'delete_chat_photo', 'group_chat_created' ]; // Telegram message events @@ -301,6 +301,26 @@ TelegramBot.prototype.sendVideo = function (chatId, video, options) { return this._request('sendVideo', opts); }; +/** + * Send voice + * @param {Number|String} chatId Unique identifier for the message recipient + * @param {String|stream.Stream} voice 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#sendvoice + */ +TelegramBot.prototype.sendVoice = function (chatId, voice, options) { + var opts = { + qs: options || {} + }; + opts.qs.chat_id = chatId; + var content = this._formatSendData('voice', voice); + opts.formData = content[0]; + opts.qs.voice = content[1]; + return this._request('sendVoice', opts); +}; + /** * Send chat action. diff --git a/test/index.js b/test/index.js index cca988d..4a8b6b7 100644 --- a/test/index.js +++ b/test/index.js @@ -330,6 +330,17 @@ describe('Telegram', function () { }); }); + describe('#sendVoice', function () { + it('should send an OGG audio as voice', function (done) { + var bot = new Telegram(TOKEN); + var voice = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'); + bot.sendVoice(USERID, voice).then(function (resp) { + resp.should.be.an.instanceOf(Object); + done(); + }); + }); + }); + describe('#getUserProfilePhotos', function () { it('should get user profile photos', function (done) { var bot = new Telegram(TOKEN);