diff --git a/telegram/bot.py b/telegram/bot.py index 82b6a7e79..46b8c7241 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -153,9 +153,48 @@ class Bot(object): return Message.newFromJsonDict(data) - def sendAudio(self): + def sendAudio(self, + chat_id, + audio, + reply_to_message_id=None, + reply_markup=None): + """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 telegram.Document). + + Args: + chat_id: + Unique identifier for the message recipient — User or GroupChat id. + audio: + Audio file to send. You can either pass a file_id as String to + resend an audio that is already on the Telegram servers, or upload + a new audio file using multipart/form-data. + reply_to_message_id: + If the message is a reply, ID of the original message. [Optional] + reply_markup: + Additional interface options. A JSON-serialized object for a + custom reply keyboard, instructions to hide keyboard or to force a + reply from the user. [Optional] + Returns: + A telegram.Message instance representing the message posted. + """ + url = '%s/sendAudio' % (self.base_url) + data = {'chat_id': chat_id, + 'audio': audio} + + if reply_to_message_id: + data['reply_to_message_id'] = reply_to_message_id + if reply_markup: + data['reply_markup'] = reply_markup + + json_data = self._requestUrl(url, 'POST', data=data) + data = self._parseAndCheckTelegram(json_data.content) + + return Message.newFromJsonDict(data) + def sendDocument(self): url = '%s/sendDocument' % (self.base_url) @@ -226,6 +265,17 @@ class Bot(object): ) except requests.RequestException as e: pass + if 'audio' in data and isinstance(data['audio'], file): + try: + audio = data.pop('audio') + + return requests.post( + url, + data=data, + files={'audio': audio} + ) + except requests.RequestException as e: + pass else: try: return requests.post( diff --git a/tests/telegram.ogg b/tests/telegram.ogg new file mode 100644 index 000000000..13f294472 Binary files /dev/null and b/tests/telegram.ogg differ diff --git a/tests/test_bot.py b/tests/test_bot.py index 9d93b9376..1d8501be1 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -51,9 +51,23 @@ class BotTest(unittest.TestCase): chat_id=12173560) self.assertEqual(12948, message.photo[2].get('file_size')) - def testReSendPhoto(self): + def testResendPhoto(self): '''Test the telegram.Bot sendPhoto method''' print 'Testing sendPhoto - Resend' message = self._bot.sendPhoto(photo=str('AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI'), chat_id=12173560) self.assertEqual(u'AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI', message.photo[2].get('file_id')) + + def testSendAudio(self): + '''Test the telegram.Bot sendAudio method''' + print 'Testing sendAudio - File' + message = self._bot.sendAudio(audio=open('tests/telegram.ogg', 'rb'), + chat_id=12173560) + self.assertEqual(9199, message.audio.get('file_size')) + + def testResendAudio(self): + '''Test the telegram.Bot sendAudio method''' + print 'Testing sendAudio - Resent' + message = self._bot.sendAudio(audio=str('AwADAQADIQEAAvjAuQABSAXg_GhkhZcC'), + chat_id=12173560) + self.assertEqual(u'AwADAQADIQEAAvjAuQABSAXg_GhkhZcC', message.audio.get('file_id'))