Skip to content

Commit 3413669

Browse files
committed
Added optional connection (aka sending) timeouts to methods that may upload big chunks of data: send_audio, send_voice, send_document, send_sticker and send_video.
1 parent 747f142 commit 3413669

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

telebot/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, rep
334334
disable_notification))
335335

336336
def send_audio(self, chat_id, audio, duration=None, performer=None, title=None, reply_to_message_id=None,
337-
reply_markup=None, disable_notification=None):
337+
reply_markup=None, disable_notification=None, timeout=None):
338338
"""
339339
Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format.
340340
:param chat_id:Unique identifier for the message recipient
@@ -348,10 +348,10 @@ def send_audio(self, chat_id, audio, duration=None, performer=None, title=None,
348348
"""
349349
return types.Message.de_json(
350350
apihelper.send_audio(self.token, chat_id, audio, duration, performer, title, reply_to_message_id,
351-
reply_markup, disable_notification))
351+
reply_markup, disable_notification, timeout))
352352

353353
def send_voice(self, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None,
354-
disable_notification=None):
354+
disable_notification=None, timeout=None):
355355
"""
356356
Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message.
357357
:param chat_id:Unique identifier for the message recipient.
@@ -363,9 +363,9 @@ def send_voice(self, chat_id, voice, duration=None, reply_to_message_id=None, re
363363
"""
364364
return types.Message.de_json(
365365
apihelper.send_voice(self.token, chat_id, voice, duration, reply_to_message_id, reply_markup,
366-
disable_notification))
366+
disable_notification, timeout))
367367

368-
def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None):
368+
def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None, timeout=None):
369369
"""
370370
Use this method to send general files.
371371
:param chat_id:
@@ -376,9 +376,9 @@ def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=No
376376
"""
377377
return types.Message.de_json(
378378
apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup,
379-
disable_notification))
379+
disable_notification, timeout))
380380

381-
def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None):
381+
def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None, timeout=None):
382382
"""
383383
Use this method to send .webp stickers.
384384
:param chat_id:
@@ -389,10 +389,10 @@ def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=Non
389389
"""
390390
return types.Message.de_json(
391391
apihelper.send_data(self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup,
392-
disable_notification))
392+
disable_notification, timeout))
393393

394394
def send_video(self, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None,
395-
disable_notification=None):
395+
disable_notification=None, timeout=None):
396396
"""
397397
Use this method to send video files, Telegram clients support mp4 videos.
398398
:param chat_id: Integer : Unique identifier for the message recipient — User or GroupChat id
@@ -405,7 +405,7 @@ def send_video(self, chat_id, data, duration=None, caption=None, reply_to_messag
405405
"""
406406
return types.Message.de_json(
407407
apihelper.send_video(self.token, chat_id, data, duration, caption, reply_to_message_id, reply_markup,
408-
disable_notification))
408+
disable_notification, timeout))
409409

410410
def send_location(self, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None,
411411
disable_notification=None):

telebot/apihelper.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ def _make_request(token, method_name, method='get', params=None, files=None, bas
2727
request_url = base_url.format(token, method_name)
2828
logger.debug("Request: method={0} url={1} params={2} files={3}".format(method, request_url, params, files))
2929
read_timeout = READ_TIMEOUT
30+
connect_timeout = CONNECT_TIMEOUT
3031
if params:
3132
if 'timeout' in params: read_timeout = params['timeout'] + 10
32-
result = requests.request(method, request_url, params=params, files=files, timeout=(CONNECT_TIMEOUT, read_timeout))
33+
if 'connect-timeout' in params: connect_timeout = params['connect-timeout'] + 10
34+
result = requests.request(method, request_url, params=params, files=files, timeout=(connect_timeout, read_timeout))
3335
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
3436
return _check_result(method_name, result)['result']
3537

@@ -225,7 +227,7 @@ def send_chat_action(token, chat_id, action):
225227

226228

227229
def send_video(token, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None,
228-
disable_notification=None):
230+
disable_notification=None, timeout=None):
229231
method_url = r'sendVideo'
230232
payload = {'chat_id': chat_id}
231233
files = None
@@ -243,11 +245,13 @@ def send_video(token, chat_id, data, duration=None, caption=None, reply_to_messa
243245
payload['reply_markup'] = _convert_markup(reply_markup)
244246
if disable_notification:
245247
payload['disable_notification'] = disable_notification
248+
if timeout:
249+
payload['connect-timeout'] = timeout
246250
return _make_request(token, method_url, params=payload, files=files, method='post')
247251

248252

249253
def send_voice(token, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None,
250-
disable_notification=None):
254+
disable_notification=None, timeout=None):
251255
method_url = r'sendVoice'
252256
payload = {'chat_id': chat_id}
253257
files = None
@@ -263,11 +267,13 @@ def send_voice(token, chat_id, voice, duration=None, reply_to_message_id=None, r
263267
payload['reply_markup'] = _convert_markup(reply_markup)
264268
if disable_notification:
265269
payload['disable_notification'] = disable_notification
270+
if timeout:
271+
payload['connect-timeout'] = timeout
266272
return _make_request(token, method_url, params=payload, files=files, method='post')
267273

268274

269275
def send_audio(token, chat_id, audio, duration=None, performer=None, title=None, reply_to_message_id=None,
270-
reply_markup=None, disable_notification=None):
276+
reply_markup=None, disable_notification=None, timeout=None):
271277
method_url = r'sendAudio'
272278
payload = {'chat_id': chat_id}
273279
files = None
@@ -287,10 +293,12 @@ def send_audio(token, chat_id, audio, duration=None, performer=None, title=None,
287293
payload['reply_markup'] = _convert_markup(reply_markup)
288294
if disable_notification:
289295
payload['disable_notification'] = disable_notification
296+
if timeout:
297+
payload['connect-timeout'] = timeout
290298
return _make_request(token, method_url, params=payload, files=files, method='post')
291299

292300

293-
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None, disable_notification=None):
301+
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None, disable_notification=None, timeout=None):
294302
method_url = get_method_by_type(data_type)
295303
payload = {'chat_id': chat_id}
296304
files = None
@@ -304,6 +312,8 @@ def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_m
304312
payload['reply_markup'] = _convert_markup(reply_markup)
305313
if disable_notification:
306314
payload['disable_notification'] = disable_notification
315+
if timeout:
316+
payload['connect-timeout'] = timeout
307317
return _make_request(token, method_url, params=payload, files=files, method='post')
308318

309319

0 commit comments

Comments
 (0)