mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-01-18 07:10:46 +01:00
Adding sendAudio method
This commit is contained in:
parent
f5976fc0d7
commit
0b0bc3d550
3 changed files with 66 additions and 2 deletions
|
@ -153,9 +153,48 @@ class Bot(object):
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
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)
|
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):
|
def sendDocument(self):
|
||||||
url = '%s/sendDocument' % (self.base_url)
|
url = '%s/sendDocument' % (self.base_url)
|
||||||
|
|
||||||
|
@ -226,6 +265,17 @@ class Bot(object):
|
||||||
)
|
)
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
pass
|
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:
|
else:
|
||||||
try:
|
try:
|
||||||
return requests.post(
|
return requests.post(
|
||||||
|
|
BIN
tests/telegram.ogg
Normal file
BIN
tests/telegram.ogg
Normal file
Binary file not shown.
|
@ -51,9 +51,23 @@ class BotTest(unittest.TestCase):
|
||||||
chat_id=12173560)
|
chat_id=12173560)
|
||||||
self.assertEqual(12948, message.photo[2].get('file_size'))
|
self.assertEqual(12948, message.photo[2].get('file_size'))
|
||||||
|
|
||||||
def testReSendPhoto(self):
|
def testResendPhoto(self):
|
||||||
'''Test the telegram.Bot sendPhoto method'''
|
'''Test the telegram.Bot sendPhoto method'''
|
||||||
print 'Testing sendPhoto - Resend'
|
print 'Testing sendPhoto - Resend'
|
||||||
message = self._bot.sendPhoto(photo=str('AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI'),
|
message = self._bot.sendPhoto(photo=str('AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI'),
|
||||||
chat_id=12173560)
|
chat_id=12173560)
|
||||||
self.assertEqual(u'AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI', message.photo[2].get('file_id'))
|
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'))
|
||||||
|
|
Loading…
Reference in a new issue