Skip to content

Commit 780ea32

Browse files
committed
Remove message decorator to fix default timeouts.
1 parent d4b5bd4 commit 780ea32

File tree

2 files changed

+57
-68
lines changed

2 files changed

+57
-68
lines changed

telegram/bot.py

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,6 @@ def decorator(self, *args, **kwargs):
6565
return decorator
6666

6767

68-
def message(func):
69-
@functools.wraps(func)
70-
def decorator(self, *args, **kwargs):
71-
url, data = func(self, *args, **kwargs)
72-
if kwargs.get('reply_to_message_id'):
73-
data['reply_to_message_id'] = kwargs.get('reply_to_message_id')
74-
75-
if kwargs.get('disable_notification'):
76-
data['disable_notification'] = kwargs.get('disable_notification')
77-
78-
if kwargs.get('reply_markup'):
79-
reply_markup = kwargs.get('reply_markup')
80-
if isinstance(reply_markup, ReplyMarkup):
81-
data['reply_markup'] = reply_markup.to_json()
82-
else:
83-
data['reply_markup'] = reply_markup
84-
85-
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
86-
87-
if result is True:
88-
return result
89-
90-
return Message.de_json(result, self)
91-
92-
return decorator
93-
94-
9568
class Bot(TelegramObject):
9669
"""This object represents a Telegram Bot.
9770
@@ -119,6 +92,27 @@ def __init__(self, token, base_url=None, base_file_url=None, request=None):
11992
self._request = request or Request()
12093
self.logger = logging.getLogger(__name__)
12194

95+
def message(self, url, data, **kwargs):
96+
if kwargs.get('reply_to_message_id'):
97+
data['reply_to_message_id'] = kwargs.get('reply_to_message_id')
98+
99+
if kwargs.get('disable_notification'):
100+
data['disable_notification'] = kwargs.get('disable_notification')
101+
102+
if kwargs.get('reply_markup'):
103+
reply_markup = kwargs.get('reply_markup')
104+
if isinstance(reply_markup, ReplyMarkup):
105+
data['reply_markup'] = reply_markup.to_json()
106+
else:
107+
data['reply_markup'] = reply_markup
108+
109+
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
110+
111+
if result is True:
112+
return result
113+
114+
return Message.de_json(result, self)
115+
122116
@property
123117
def request(self):
124118
return self._request
@@ -195,7 +189,6 @@ def get_me(self, timeout=None, **kwargs):
195189
return self.bot
196190

197191
@log
198-
@message
199192
def send_message(self,
200193
chat_id,
201194
text,
@@ -246,7 +239,7 @@ def send_message(self,
246239
if disable_web_page_preview:
247240
data['disable_web_page_preview'] = disable_web_page_preview
248241

249-
return url, data
242+
return self.message(url, data, timeout=timeout, **kwargs)
250243

251244
@log
252245
def delete_message(self, chat_id, message_id, timeout=None, **kwargs):
@@ -285,7 +278,6 @@ def delete_message(self, chat_id, message_id, timeout=None, **kwargs):
285278
return result
286279

287280
@log
288-
@message
289281
def forward_message(self,
290282
chat_id,
291283
from_chat_id,
@@ -327,10 +319,9 @@ def forward_message(self,
327319
if message_id:
328320
data['message_id'] = message_id
329321

330-
return url, data
322+
return self.message(url, data, timeout=timeout, **kwargs)
331323

332324
@log
333-
@message
334325
def send_photo(self,
335326
chat_id,
336327
photo,
@@ -389,10 +380,9 @@ def send_photo(self,
389380
if parse_mode:
390381
data['parse_mode'] = parse_mode
391382

392-
return url, data
383+
return self.message(url, data, timeout=timeout, **kwargs)
393384

394385
@log
395-
@message
396386
def send_audio(self,
397387
chat_id,
398388
audio,
@@ -468,10 +458,9 @@ def send_audio(self,
468458
if parse_mode:
469459
data['parse_mode'] = parse_mode
470460

471-
return url, data
461+
return self.message(url, data, timeout=timeout, **kwargs)
472462

473463
@log
474-
@message
475464
def send_document(self,
476465
chat_id,
477466
document,
@@ -535,10 +524,9 @@ def send_document(self,
535524
if parse_mode:
536525
data['parse_mode'] = parse_mode
537526

538-
return url, data
527+
return self.message(url, data, timeout=timeout, **kwargs)
539528

540529
@log
541-
@message
542530
def send_sticker(self,
543531
chat_id,
544532
sticker,
@@ -585,10 +573,9 @@ def send_sticker(self,
585573

586574
data = {'chat_id': chat_id, 'sticker': sticker}
587575

588-
return url, data
576+
return self.message(url, data, timeout=timeout, **kwargs)
589577

590578
@log
591-
@message
592579
def send_video(self,
593580
chat_id,
594581
video,
@@ -666,10 +653,9 @@ def send_video(self,
666653
if height:
667654
data['height'] = height
668655

669-
return url, data
656+
return self.message(url, data, timeout=timeout, **kwargs)
670657

671658
@log
672-
@message
673659
def send_voice(self,
674660
chat_id,
675661
voice,
@@ -734,10 +720,9 @@ def send_voice(self,
734720
if parse_mode:
735721
data['parse_mode'] = parse_mode
736722

737-
return url, data
723+
return self.message(url, data, timeout=timeout, **kwargs)
738724

739725
@log
740-
@message
741726
def send_video_note(self,
742727
chat_id,
743728
video_note,
@@ -793,7 +778,7 @@ def send_video_note(self,
793778
if length is not None:
794779
data['length'] = length
795780

796-
return url, data
781+
return self.message(url, data, timeout=timeout, **kwargs)
797782

798783
@log
799784
def send_media_group(self,
@@ -841,7 +826,6 @@ def send_media_group(self,
841826
return [Message.de_json(res, self) for res in result]
842827

843828
@log
844-
@message
845829
def send_location(self,
846830
chat_id,
847831
latitude=None,
@@ -904,10 +888,9 @@ def send_location(self,
904888
if live_period:
905889
data['live_period'] = live_period
906890

907-
return url, data
891+
return self.message(url, data, timeout=timeout, **kwargs)
908892

909893
@log
910-
@message
911894
def edit_message_live_location(self,
912895
chat_id=None,
913896
message_id=None,
@@ -916,6 +899,7 @@ def edit_message_live_location(self,
916899
longitude=None,
917900
location=None,
918901
reply_markup=None,
902+
timeout=None,
919903
**kwargs):
920904
"""Use this method to edit live location messages sent by the bot or via the bot
921905
(for inline bots). A location can be edited until its :attr:`live_period` expires or
@@ -967,15 +951,15 @@ def edit_message_live_location(self,
967951
if inline_message_id:
968952
data['inline_message_id'] = inline_message_id
969953

970-
return url, data
954+
return self.message(url, data, timeout=timeout, **kwargs)
971955

972956
@log
973-
@message
974957
def stop_message_live_location(self,
975958
chat_id=None,
976959
message_id=None,
977960
inline_message_id=None,
978961
reply_markup=None,
962+
timeout=None,
979963
**kwargs):
980964
"""Use this method to stop updating a live location message sent by the bot or via the bot
981965
(for inline bots) before live_period expires.
@@ -1009,10 +993,9 @@ def stop_message_live_location(self,
1009993
if inline_message_id:
1010994
data['inline_message_id'] = inline_message_id
1011995

1012-
return url, data
996+
return self.message(url, data, timeout=timeout, **kwargs)
1013997

1014998
@log
1015-
@message
1016999
def send_venue(self,
10171000
chat_id,
10181001
latitude=None,
@@ -1084,10 +1067,9 @@ def send_venue(self,
10841067
if foursquare_id:
10851068
data['foursquare_id'] = foursquare_id
10861069

1087-
return url, data
1070+
return self.message(url, data, timeout=timeout, **kwargs)
10881071

10891072
@log
1090-
@message
10911073
def send_contact(self,
10921074
chat_id,
10931075
phone_number=None,
@@ -1147,10 +1129,9 @@ def send_contact(self,
11471129
if last_name:
11481130
data['last_name'] = last_name
11491131

1150-
return url, data
1132+
return self.message(url, data, timeout=timeout, **kwargs)
11511133

11521134
@log
1153-
@message
11541135
def send_game(self,
11551136
chat_id,
11561137
game_short_name,
@@ -1189,7 +1170,7 @@ def send_game(self,
11891170

11901171
data = {'chat_id': chat_id, 'game_short_name': game_short_name}
11911172

1192-
return url, data
1173+
return self.message(url, data, timeout=timeout, **kwargs)
11931174

11941175
@log
11951176
def send_chat_action(self, chat_id, action, timeout=None, **kwargs):
@@ -1523,7 +1504,6 @@ def answer_callback_query(self,
15231504
return result
15241505

15251506
@log
1526-
@message
15271507
def edit_message_text(self,
15281508
text,
15291509
chat_id=None,
@@ -1582,10 +1562,9 @@ def edit_message_text(self,
15821562
if disable_web_page_preview:
15831563
data['disable_web_page_preview'] = disable_web_page_preview
15841564

1585-
return url, data
1565+
return self.message(url, data, timeout=timeout, **kwargs)
15861566

15871567
@log
1588-
@message
15891568
def edit_message_caption(self,
15901569
chat_id=None,
15911570
message_id=None,
@@ -1646,10 +1625,9 @@ def edit_message_caption(self,
16461625
if inline_message_id:
16471626
data['inline_message_id'] = inline_message_id
16481627

1649-
return url, data
1628+
return self.message(url, data, timeout=timeout, **kwargs)
16501629

16511630
@log
1652-
@message
16531631
def edit_message_reply_markup(self,
16541632
chat_id=None,
16551633
message_id=None,
@@ -1700,7 +1678,7 @@ def edit_message_reply_markup(self,
17001678
if inline_message_id:
17011679
data['inline_message_id'] = inline_message_id
17021680

1703-
return url, data
1681+
return self.message(url, data, timeout=timeout, **kwargs)
17041682

17051683
@log
17061684
def get_updates(self,
@@ -2117,7 +2095,6 @@ def get_webhook_info(self, timeout=None, **kwargs):
21172095
return WebhookInfo.de_json(result, self)
21182096

21192097
@log
2120-
@message
21212098
def set_game_score(self,
21222099
user_id,
21232100
score,
@@ -2176,7 +2153,7 @@ def set_game_score(self,
21762153
if disable_edit_message is not None:
21772154
data['disable_edit_message'] = disable_edit_message
21782155

2179-
return url, data
2156+
return self.message(url, data, timeout=timeout, **kwargs)
21802157

21812158
@log
21822159
def get_game_high_scores(self,
@@ -2227,7 +2204,6 @@ def get_game_high_scores(self,
22272204
return [GameHighScore.de_json(hs, self) for hs in result]
22282205

22292206
@log
2230-
@message
22312207
def send_invoice(self,
22322208
chat_id,
22332209
title,
@@ -2351,7 +2327,7 @@ def send_invoice(self,
23512327
if send_email_to_provider is not None:
23522328
data['send_email_to_provider'] = send_email_to_provider
23532329

2354-
return url, data
2330+
return self.message(url, data, timeout=timeout, **kwargs)
23552331

23562332
@log
23572333
def answer_shipping_query(self,

tests/test_bot.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def test_error_pin_unpin_message(self, bot, message):
619619
# set_sticker_position_in_set and delete_sticker_from_set are tested in the
620620
# test_sticker module.
621621

622-
def test_timeout_propagation(self, monkeypatch, bot, chat_id):
622+
def test_timeout_propagation_explicit(self, monkeypatch, bot, chat_id):
623623
class OkException(Exception):
624624
pass
625625

@@ -633,3 +633,16 @@ def post(*args, **kwargs):
633633

634634
with pytest.raises(OkException):
635635
bot.send_photo(chat_id, open('tests/data/telegram.jpg', 'rb'), timeout=timeout)
636+
637+
def test_timeout_propagation_implicit(self, monkeypatch, bot, chat_id):
638+
class OkException(Exception):
639+
pass
640+
641+
def post(*args, **kwargs):
642+
if kwargs.get('timeout') == 20:
643+
raise OkException
644+
645+
monkeypatch.setattr('telegram.utils.request.Request.post', post)
646+
647+
with pytest.raises(OkException):
648+
bot.send_photo(chat_id, open('tests/data/telegram.jpg', 'rb'))

0 commit comments

Comments
 (0)