X Tutup
Skip to content

Commit 2b930c2

Browse files
committed
Sane default for sending files timeout
1 parent 2a1d40b commit 2b930c2

File tree

2 files changed

+92
-48
lines changed

2 files changed

+92
-48
lines changed

telegram/bot.py

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,33 @@ def decorator(self, *args, **kwargs):
129129

130130
return decorator
131131

132-
def message(func):
132+
def _message_wrapper(self, url, data, *args, **kwargs):
133+
if kwargs.get('reply_to_message_id'):
134+
data['reply_to_message_id'] = kwargs.get('reply_to_message_id')
133135

134-
@functools.wraps(func)
135-
def decorator(self, *args, **kwargs):
136-
url, data = func(self, *args, **kwargs)
136+
if kwargs.get('disable_notification'):
137+
data['disable_notification'] = kwargs.get('disable_notification')
137138

138-
if kwargs.get('reply_to_message_id'):
139-
data['reply_to_message_id'] = kwargs.get('reply_to_message_id')
139+
if kwargs.get('reply_markup'):
140+
reply_markup = kwargs.get('reply_markup')
141+
if isinstance(reply_markup, ReplyMarkup):
142+
data['reply_markup'] = reply_markup.to_json()
143+
else:
144+
data['reply_markup'] = reply_markup
140145

141-
if kwargs.get('disable_notification'):
142-
data['disable_notification'] = kwargs.get('disable_notification')
146+
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
143147

144-
if kwargs.get('reply_markup'):
145-
reply_markup = kwargs.get('reply_markup')
146-
if isinstance(reply_markup, ReplyMarkup):
147-
data['reply_markup'] = reply_markup.to_json()
148-
else:
149-
data['reply_markup'] = reply_markup
148+
if result is True:
149+
return result
150150

151-
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
151+
return Message.de_json(result, self)
152152

153-
if result is True:
154-
return result
153+
def message(func):
155154

156-
return Message.de_json(result, self)
155+
@functools.wraps(func)
156+
def decorator(self, *args, **kwargs):
157+
url, data = func(self, *args, **kwargs)
158+
return Bot._message_wrapper(self, url, data, *args, **kwargs)
157159

158160
return decorator
159161

@@ -283,15 +285,14 @@ def forwardMessage(self,
283285
return url, data
284286

285287
@log
286-
@message
287288
def sendPhoto(self,
288289
chat_id,
289290
photo,
290291
caption=None,
291292
disable_notification=False,
292293
reply_to_message_id=None,
293294
reply_markup=None,
294-
timeout=None,
295+
timeout=20.,
295296
**kwargs):
296297
"""Use this method to send photos.
297298
@@ -308,9 +309,7 @@ def sendPhoto(self,
308309
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
309310
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
310311
to remove reply keyboard or to force a reply from the user.
311-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
312-
from the server (instead of the one specified during creation of the connection
313-
pool).
312+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
314313
**kwargs (dict): Arbitrary keyword arguments.
315314
316315
Returns:
@@ -327,10 +326,19 @@ def sendPhoto(self,
327326
if caption:
328327
data['caption'] = caption
329328

330-
return url, data
329+
return self._message_wrapper(
330+
url,
331+
data,
332+
chat_id=chat_id,
333+
photo=photo,
334+
caption=caption,
335+
disable_notification=disable_notification,
336+
reply_to_message_id=reply_to_message_id,
337+
reply_markup=reply_markup,
338+
timeout=timeout,
339+
**kwargs)
331340

332341
@log
333-
@message
334342
def sendAudio(self,
335343
chat_id,
336344
audio,
@@ -341,7 +349,7 @@ def sendAudio(self,
341349
disable_notification=False,
342350
reply_to_message_id=None,
343351
reply_markup=None,
344-
timeout=None,
352+
timeout=20.,
345353
**kwargs):
346354
"""Use this method to send audio files, if you want Telegram clients to
347355
display them in the music player. Your audio must be in an .mp3 format.
@@ -370,9 +378,7 @@ def sendAudio(self,
370378
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
371379
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
372380
to remove reply keyboard or to force a reply from the user.
373-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
374-
from the server (instead of the one specified during creation of the connection
375-
pool).
381+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
376382
**kwargs (dict): Arbitrary keyword arguments.
377383
378384
Returns:
@@ -395,10 +401,22 @@ def sendAudio(self,
395401
if caption:
396402
data['caption'] = caption
397403

398-
return url, data
404+
return self._message_wrapper(
405+
url,
406+
data,
407+
chat_id=chat_id,
408+
audio=audio,
409+
duration=duration,
410+
performer=performer,
411+
title=title,
412+
caption=caption,
413+
disable_notification=disable_notification,
414+
reply_to_message_id=reply_to_message_id,
415+
reply_markup=reply_markup,
416+
timeout=20.,
417+
**kwargs)
399418

400419
@log
401-
@message
402420
def sendDocument(self,
403421
chat_id,
404422
document,
@@ -407,7 +425,7 @@ def sendDocument(self,
407425
disable_notification=False,
408426
reply_to_message_id=None,
409427
reply_markup=None,
410-
timeout=None,
428+
timeout=20.,
411429
**kwargs):
412430
"""Use this method to send general files.
413431
@@ -426,9 +444,7 @@ def sendDocument(self,
426444
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
427445
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
428446
to remove reply keyboard or to force a reply from the user.
429-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
430-
from the server (instead of the one specified during creation of the connection
431-
pool).
447+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
432448
**kwargs (dict): Arbitrary keyword arguments.
433449
434450
Returns:
@@ -447,6 +463,18 @@ def sendDocument(self,
447463
if caption:
448464
data['caption'] = caption
449465

466+
self._message_wrapper(
467+
url,
468+
data,
469+
chat_id=chat_id,
470+
document=document,
471+
filename=filename,
472+
caption=caption,
473+
disable_notification=disable_notification,
474+
reply_to_message_id=reply_to_message_id,
475+
reply_markup=reply_markup,
476+
timeout=timeout,
477+
**kwargs)
450478
return url, data
451479

452480
@log
@@ -492,7 +520,6 @@ def sendSticker(self,
492520
return url, data
493521

494522
@log
495-
@message
496523
def sendVideo(self,
497524
chat_id,
498525
video,
@@ -501,7 +528,7 @@ def sendVideo(self,
501528
disable_notification=False,
502529
reply_to_message_id=None,
503530
reply_markup=None,
504-
timeout=None,
531+
timeout=20.,
505532
**kwargs):
506533
"""Use this method to send video files, Telegram clients support mp4
507534
videos (other formats may be sent as telegram.Document).
@@ -521,9 +548,7 @@ def sendVideo(self,
521548
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
522549
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
523550
to remove reply keyboard or to force a reply from the user.
524-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
525-
from the server (instead of the one specified during creation of the connection
526-
pool).
551+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
527552
528553
Returns:
529554
:class:`telegram.Message`: On success, instance representing the message posted.
@@ -541,10 +566,20 @@ def sendVideo(self,
541566
if caption:
542567
data['caption'] = caption
543568

544-
return url, data
569+
return self._message_wrapper(
570+
url,
571+
data,
572+
chat_id=chat_id,
573+
video=video,
574+
duration=duration,
575+
caption=caption,
576+
disable_notification=disable_notification,
577+
reply_to_message_id=reply_to_message_id,
578+
reply_markup=reply_markup,
579+
timeout=timeout,
580+
**kwargs)
545581

546582
@log
547-
@message
548583
def sendVoice(self,
549584
chat_id,
550585
voice,
@@ -553,7 +588,7 @@ def sendVoice(self,
553588
disable_notification=False,
554589
reply_to_message_id=None,
555590
reply_markup=None,
556-
timeout=None,
591+
timeout=20.,
557592
**kwargs):
558593
"""Use this method to send audio files, if you want Telegram clients to display the file as
559594
a playable voice message. For this to work, your audio must be in an .ogg file encoded with
@@ -575,9 +610,7 @@ def sendVoice(self,
575610
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
576611
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
577612
to remove reply keyboard or to force a reply from the user.
578-
timeout (Optional[int|float]): If this value is specified, use it as the read timeout
579-
from the server (instead of the one specified during creation of the connection
580-
pool).
613+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
581614
**kwargs (dict): Arbitrary keyword arguments.
582615
583616
Returns:
@@ -596,7 +629,18 @@ def sendVoice(self,
596629
if caption:
597630
data['caption'] = caption
598631

599-
return url, data
632+
return self._message_wrapper(
633+
url,
634+
data,
635+
chat_id=chat_id,
636+
voice=voice,
637+
duration=duration,
638+
caption=caption,
639+
disable_notification=disable_notification,
640+
reply_to_message_id=reply_to_message_id,
641+
reply_markup=reply_markup,
642+
timeout=timeout,
643+
**kwargs)
600644

601645
@log
602646
@message

telegram/utils/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(self,
8181
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
8282
],
8383
timeout=urllib3.Timeout(
84-
connect=self._connect_timeout, read=read_timeout),)
84+
connect=self._connect_timeout, read=read_timeout, total=None))
8585

8686
# Set a proxy according to the following order:
8787
# * proxy defined in proxy_url (+ urllib3_proxy_kwargs)

0 commit comments

Comments
 (0)
X Tutup