Skip to content

Commit 0b0bc3d

Browse files
committed
Adding sendAudio method
1 parent f5976fc commit 0b0bc3d

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

telegram/bot.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,48 @@ def sendPhoto(self,
153153

154154
return Message.newFromJsonDict(data)
155155

156-
def sendAudio(self):
156+
def sendAudio(self,
157+
chat_id,
158+
audio,
159+
reply_to_message_id=None,
160+
reply_markup=None):
161+
"""Use this method to send audio files, if you want Telegram clients to
162+
display the file as a playable voice message. For this to work, your
163+
audio must be in an .ogg file encoded with OPUS (other formats may be
164+
sent as telegram.Document).
165+
166+
Args:
167+
chat_id:
168+
Unique identifier for the message recipient — User or GroupChat id.
169+
audio:
170+
Audio file to send. You can either pass a file_id as String to
171+
resend an audio that is already on the Telegram servers, or upload
172+
a new audio file using multipart/form-data.
173+
reply_to_message_id:
174+
If the message is a reply, ID of the original message. [Optional]
175+
reply_markup:
176+
Additional interface options. A JSON-serialized object for a
177+
custom reply keyboard, instructions to hide keyboard or to force a
178+
reply from the user. [Optional]
179+
Returns:
180+
A telegram.Message instance representing the message posted.
181+
"""
182+
157183
url = '%s/sendAudio' % (self.base_url)
158184

185+
data = {'chat_id': chat_id,
186+
'audio': audio}
187+
188+
if reply_to_message_id:
189+
data['reply_to_message_id'] = reply_to_message_id
190+
if reply_markup:
191+
data['reply_markup'] = reply_markup
192+
193+
json_data = self._requestUrl(url, 'POST', data=data)
194+
data = self._parseAndCheckTelegram(json_data.content)
195+
196+
return Message.newFromJsonDict(data)
197+
159198
def sendDocument(self):
160199
url = '%s/sendDocument' % (self.base_url)
161200

@@ -226,6 +265,17 @@ def _requestUrl(self,
226265
)
227266
except requests.RequestException as e:
228267
pass
268+
if 'audio' in data and isinstance(data['audio'], file):
269+
try:
270+
audio = data.pop('audio')
271+
272+
return requests.post(
273+
url,
274+
data=data,
275+
files={'audio': audio}
276+
)
277+
except requests.RequestException as e:
278+
pass
229279
else:
230280
try:
231281
return requests.post(

tests/telegram.ogg

8.98 KB
Binary file not shown.

tests/test_bot.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,23 @@ def testSendPhoto(self):
5151
chat_id=12173560)
5252
self.assertEqual(12948, message.photo[2].get('file_size'))
5353

54-
def testReSendPhoto(self):
54+
def testResendPhoto(self):
5555
'''Test the telegram.Bot sendPhoto method'''
5656
print 'Testing sendPhoto - Resend'
5757
message = self._bot.sendPhoto(photo=str('AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI'),
5858
chat_id=12173560)
5959
self.assertEqual(u'AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI', message.photo[2].get('file_id'))
60+
61+
def testSendAudio(self):
62+
'''Test the telegram.Bot sendAudio method'''
63+
print 'Testing sendAudio - File'
64+
message = self._bot.sendAudio(audio=open('tests/telegram.ogg', 'rb'),
65+
chat_id=12173560)
66+
self.assertEqual(9199, message.audio.get('file_size'))
67+
68+
def testResendAudio(self):
69+
'''Test the telegram.Bot sendAudio method'''
70+
print 'Testing sendAudio - Resent'
71+
message = self._bot.sendAudio(audio=str('AwADAQADIQEAAvjAuQABSAXg_GhkhZcC'),
72+
chat_id=12173560)
73+
self.assertEqual(u'AwADAQADIQEAAvjAuQABSAXg_GhkhZcC', message.audio.get('file_id'))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy