@@ -153,9 +153,48 @@ def sendPhoto(self,
153
153
154
154
return Message .newFromJsonDict (data )
155
155
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
+
157
183
url = '%s/sendAudio' % (self .base_url )
158
184
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
+
159
198
def sendDocument (self ):
160
199
url = '%s/sendDocument' % (self .base_url )
161
200
@@ -226,6 +265,17 @@ def _requestUrl(self,
226
265
)
227
266
except requests .RequestException as e :
228
267
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
229
279
else :
230
280
try :
231
281
return requests .post (
0 commit comments