Skip to content

Commit 8e5cb58

Browse files
committed
bot.py: replace message decorator with an explicit method call
fixes python-telegram-bot#262
1 parent 2501036 commit 8e5cb58

File tree

1 file changed

+53
-91
lines changed

1 file changed

+53
-91
lines changed

telegram/bot.py

Lines changed: 53 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -115,33 +115,32 @@ def decorator(self, *args, **kwargs):
115115

116116
return decorator
117117

118-
def message(func):
119-
120-
@functools.wraps(func)
121-
def decorator(self, *args, **kwargs):
122-
url, data = func(self, *args, **kwargs)
123-
124-
if kwargs.get('reply_to_message_id'):
125-
data['reply_to_message_id'] = kwargs.get('reply_to_message_id')
118+
@staticmethod
119+
def _post_message(url, data, msg_cls=Message, expect_list=False, timeout=None, **kwargs):
120+
reply_to_message_id = kwargs.get('reply_to_message_id')
121+
if reply_to_message_id:
122+
data['reply_to_message_id'] = reply_to_message_id
126123

127-
if kwargs.get('disable_notification'):
128-
data['disable_notification'] = kwargs.get('disable_notification')
124+
disable_notification = kwargs.get('disable_notification')
125+
if disable_notification:
126+
data['disable_notification'] = disable_notification
129127

130-
if kwargs.get('reply_markup'):
131-
reply_markup = kwargs.get('reply_markup')
132-
if isinstance(reply_markup, ReplyMarkup):
133-
data['reply_markup'] = reply_markup.to_json()
134-
else:
135-
data['reply_markup'] = reply_markup
128+
reply_markup = kwargs.get('reply_markup')
129+
if reply_markup:
130+
if isinstance(reply_markup, ReplyMarkup):
131+
reply_markup = reply_markup.to_json()
132+
data['reply_markup'] = reply_markup
136133

137-
result = request.post(url, data, timeout=kwargs.get('timeout'))
134+
result = request.post(url, data, timeout=timeout)
138135

139-
if result is True:
140-
return result
136+
if isinstance(result, int):
137+
# int covers both boolean & integer results
138+
return result
141139

142-
return Message.de_json(result)
140+
if expect_list:
141+
return [msg_cls.de_json(x) for x in result]
143142

144-
return decorator
143+
return msg_cls.de_json(result)
145144

146145
@log
147146
def getMe(self, **kwargs):
@@ -166,7 +165,6 @@ def getMe(self, **kwargs):
166165
return self.bot
167166

168167
@log
169-
@message
170168
def sendMessage(self, chat_id, text, parse_mode=None, disable_web_page_preview=None, **kwargs):
171169
"""Use this method to send text messages.
172170
@@ -214,10 +212,9 @@ def sendMessage(self, chat_id, text, parse_mode=None, disable_web_page_preview=N
214212
if disable_web_page_preview:
215213
data['disable_web_page_preview'] = disable_web_page_preview
216214

217-
return url, data
215+
return self._post_message(url, data, **kwargs)
218216

219217
@log
220-
@message
221218
def forwardMessage(self, chat_id, from_chat_id, message_id, **kwargs):
222219
"""Use this method to forward messages of any kind.
223220
@@ -257,10 +254,9 @@ def forwardMessage(self, chat_id, from_chat_id, message_id, **kwargs):
257254
if message_id:
258255
data['message_id'] = message_id
259256

260-
return url, data
257+
return self._post_message(url, data, **kwargs)
261258

262259
@log
263-
@message
264260
def sendPhoto(self, chat_id, photo, caption=None, **kwargs):
265261
"""Use this method to send photos.
266262
@@ -304,10 +300,9 @@ def sendPhoto(self, chat_id, photo, caption=None, **kwargs):
304300
if caption:
305301
data['caption'] = caption
306302

307-
return url, data
303+
return self._post_message(url, data, **kwargs)
308304

309305
@log
310-
@message
311306
def sendAudio(self, chat_id, audio, duration=None, performer=None, title=None, **kwargs):
312307
"""Use this method to send audio files, if you want Telegram clients to
313308
display them in the music player. Your audio must be in an .mp3 format.
@@ -367,10 +362,9 @@ def sendAudio(self, chat_id, audio, duration=None, performer=None, title=None, *
367362
if title:
368363
data['title'] = title
369364

370-
return url, data
365+
return self._post_message(url, data, **kwargs)
371366

372367
@log
373-
@message
374368
def sendDocument(self, chat_id, document, filename=None, caption=None, **kwargs):
375369
"""Use this method to send general files.
376370
@@ -419,10 +413,9 @@ def sendDocument(self, chat_id, document, filename=None, caption=None, **kwargs)
419413
if caption:
420414
data['caption'] = caption
421415

422-
return url, data
416+
return self._post_message(url, data, **kwargs)
423417

424418
@log
425-
@message
426419
def sendSticker(self, chat_id, sticker, **kwargs):
427420
"""Use this method to send .webp stickers.
428421
@@ -460,10 +453,9 @@ def sendSticker(self, chat_id, sticker, **kwargs):
460453

461454
data = {'chat_id': chat_id, 'sticker': sticker}
462455

463-
return url, data
456+
return self._post_message(url, data, **kwargs)
464457

465458
@log
466-
@message
467459
def sendVideo(self, chat_id, video, duration=None, caption=None, **kwargs):
468460
"""Use this method to send video files, Telegram clients support mp4
469461
videos (other formats may be sent as telegram.Document).
@@ -512,10 +504,9 @@ def sendVideo(self, chat_id, video, duration=None, caption=None, **kwargs):
512504
if caption:
513505
data['caption'] = caption
514506

515-
return url, data
507+
return self._post_message(url, data, **kwargs)
516508

517509
@log
518-
@message
519510
def sendVoice(self, chat_id, voice, duration=None, **kwargs):
520511
"""Use this method to send audio files, if you want Telegram clients to
521512
display the file as a playable voice message. For this to work, your
@@ -563,10 +554,9 @@ def sendVoice(self, chat_id, voice, duration=None, **kwargs):
563554
if duration:
564555
data['duration'] = duration
565556

566-
return url, data
557+
return self._post_message(url, data, **kwargs)
567558

568559
@log
569-
@message
570560
def sendLocation(self, chat_id, latitude, longitude, **kwargs):
571561
"""Use this method to send point on the map.
572562
@@ -604,10 +594,9 @@ def sendLocation(self, chat_id, latitude, longitude, **kwargs):
604594

605595
data = {'chat_id': chat_id, 'latitude': latitude, 'longitude': longitude}
606596

607-
return url, data
597+
return self._post_message(url, data, **kwargs)
608598

609599
@log
610-
@message
611600
def sendVenue(
612601
self, chat_id,
613602
latitude,
@@ -666,10 +655,9 @@ def sendVenue(
666655
if foursquare_id:
667656
data['foursquare_id'] = foursquare_id
668657

669-
return url, data
658+
return self._post_message(url, data, **kwargs)
670659

671660
@log
672-
@message
673661
def sendContact(self, chat_id, phone_number, first_name, last_name=None, **kwargs):
674662
"""
675663
Use this method to send phone contacts.
@@ -714,10 +702,9 @@ def sendContact(self, chat_id, phone_number, first_name, last_name=None, **kwarg
714702
if last_name:
715703
data['last_name'] = last_name
716704

717-
return url, data
705+
return self._post_message(url, data, **kwargs)
718706

719707
@log
720-
@message
721708
def sendChatAction(self, chat_id, action, **kwargs):
722709
"""Use this method when you need to tell the user that something is
723710
happening on the bot's side. The status is set for 5 seconds or less
@@ -742,7 +729,7 @@ def sendChatAction(self, chat_id, action, **kwargs):
742729

743730
data = {'chat_id': chat_id, 'action': action}
744731

745-
return url, data
732+
return self._post_message(url, data, **kwargs)
746733

747734
@log
748735
def answerInlineQuery(self,
@@ -808,9 +795,7 @@ def answerInlineQuery(self,
808795
if switch_pm_parameter:
809796
data['switch_pm_parameter'] = switch_pm_parameter
810797

811-
result = request.post(url, data, timeout=kwargs.get('timeout'))
812-
813-
return result
798+
return self._post_message(url, data, **kwargs)
814799

815800
@log
816801
def getUserProfilePhotos(self, user_id, offset=None, limit=100, **kwargs):
@@ -848,9 +833,7 @@ def getUserProfilePhotos(self, user_id, offset=None, limit=100, **kwargs):
848833
if limit:
849834
data['limit'] = limit
850835

851-
result = request.post(url, data, timeout=kwargs.get('timeout'))
852-
853-
return UserProfilePhotos.de_json(result)
836+
return self._post_message(url, data, UserProfilePhotos, **kwargs)
854837

855838
@log
856839
def getFile(self, file_id, **kwargs):
@@ -879,12 +862,12 @@ def getFile(self, file_id, **kwargs):
879862

880863
data = {'file_id': file_id}
881864

882-
result = request.post(url, data, timeout=kwargs.get('timeout'))
865+
result = self._post_message(url, data, File, **kwargs)
883866

884-
if result.get('file_path'):
885-
result['file_path'] = '%s/%s' % (self.base_file_url, result['file_path'])
867+
if result.file_path:
868+
result.file_path = '%s/%s' % (self.base_file_url, result.file_path)
886869

887-
return File.de_json(result)
870+
return result
888871

889872
@log
890873
def kickChatMember(self, chat_id, user_id, **kwargs):
@@ -916,9 +899,7 @@ def kickChatMember(self, chat_id, user_id, **kwargs):
916899

917900
data = {'chat_id': chat_id, 'user_id': user_id}
918901

919-
result = request.post(url, data, timeout=kwargs.get('timeout'))
920-
921-
return result
902+
return self._post_message(url, data, **kwargs)
922903

923904
@log
924905
def unbanChatMember(self, chat_id, user_id, **kwargs):
@@ -950,9 +931,7 @@ def unbanChatMember(self, chat_id, user_id, **kwargs):
950931

951932
data = {'chat_id': chat_id, 'user_id': user_id}
952933

953-
result = request.post(url, data, timeout=kwargs.get('timeout'))
954-
955-
return result
934+
return self._post_message(url, data, **kwargs)
956935

957936
@log
958937
def answerCallbackQuery(self, callback_query_id, text=None, show_alert=False, **kwargs):
@@ -994,12 +973,9 @@ def answerCallbackQuery(self, callback_query_id, text=None, show_alert=False, **
994973
if show_alert:
995974
data['show_alert'] = show_alert
996975

997-
result = request.post(url, data, timeout=kwargs.get('timeout'))
998-
999-
return result
976+
return self._post_message(url, data, **kwargs)
1000977

1001978
@log
1002-
@message
1003979
def editMessageText(self,
1004980
text,
1005981
chat_id=None,
@@ -1065,10 +1041,9 @@ def editMessageText(self,
10651041
if disable_web_page_preview:
10661042
data['disable_web_page_preview'] = disable_web_page_preview
10671043

1068-
return url, data
1044+
return self._post_message(url, data, **kwargs)
10691045

10701046
@log
1071-
@message
10721047
def editMessageCaption(self,
10731048
chat_id=None,
10741049
message_id=None,
@@ -1118,10 +1093,9 @@ def editMessageCaption(self,
11181093
if inline_message_id:
11191094
data['inline_message_id'] = inline_message_id
11201095

1121-
return url, data
1096+
return self._post_message(url, data, **kwargs)
11221097

11231098
@log
1124-
@message
11251099
def editMessageReplyMarkup(
11261100
self, chat_id=None,
11271101
message_id=None, inline_message_id=None,
@@ -1166,7 +1140,7 @@ def editMessageReplyMarkup(
11661140
if inline_message_id:
11671141
data['inline_message_id'] = inline_message_id
11681142

1169-
return url, data
1143+
return self._post_message(url, data, **kwargs)
11701144

11711145
@log
11721146
def getUpdates(self, offset=None, limit=100, timeout=0, network_delay=5., **kwargs):
@@ -1207,14 +1181,14 @@ def getUpdates(self, offset=None, limit=100, timeout=0, network_delay=5., **kwar
12071181

12081182
urlopen_timeout = timeout + network_delay
12091183

1210-
result = request.post(url, data, timeout=urlopen_timeout)
1184+
result = self._post_message(url, data, Update, True, **kwargs)
12111185

12121186
if result:
1213-
self.logger.debug('Getting updates: %s', [u['update_id'] for u in result])
1187+
self.logger.debug('Got updates: %s', [u.update_id for u in result])
12141188
else:
12151189
self.logger.debug('No new updates found.')
12161190

1217-
return [Update.de_json(x) for x in result]
1191+
return result
12181192

12191193
@log
12201194
def setWebhook(self, webhook_url=None, certificate=None, **kwargs):
@@ -1250,9 +1224,7 @@ def setWebhook(self, webhook_url=None, certificate=None, **kwargs):
12501224
if certificate:
12511225
data['certificate'] = certificate
12521226

1253-
result = request.post(url, data, timeout=kwargs.get('timeout'))
1254-
1255-
return result
1227+
return self._post_message(url, data, **kwargs)
12561228

12571229
@log
12581230
def leaveChat(self, chat_id, **kwargs):
@@ -1280,9 +1252,7 @@ def leaveChat(self, chat_id, **kwargs):
12801252

12811253
data = {'chat_id': chat_id}
12821254

1283-
result = request.post(url, data, timeout=kwargs.get('timeout'))
1284-
1285-
return result
1255+
return self._post_message(url, data, **kwargs)
12861256

12871257
@log
12881258
def getChat(self, chat_id, **kwargs):
@@ -1312,9 +1282,7 @@ def getChat(self, chat_id, **kwargs):
13121282

13131283
data = {'chat_id': chat_id}
13141284

1315-
result = request.post(url, data, timeout=kwargs.get('timeout'))
1316-
1317-
return Chat.de_json(result)
1285+
return self._post_message(url, data, Chat, **kwargs)
13181286

13191287
@log
13201288
def getChatAdministrators(self, chat_id, **kwargs):
@@ -1347,9 +1315,7 @@ def getChatAdministrators(self, chat_id, **kwargs):
13471315

13481316
data = {'chat_id': chat_id}
13491317

1350-
result = request.post(url, data, timeout=kwargs.get('timeout'))
1351-
1352-
return [ChatMember.de_json(x) for x in result]
1318+
return self._post_message(url, data, ChatMember, True, **kwargs)
13531319

13541320
@log
13551321
def getChatMembersCount(self, chat_id, **kwargs):
@@ -1377,9 +1343,7 @@ def getChatMembersCount(self, chat_id, **kwargs):
13771343

13781344
data = {'chat_id': chat_id}
13791345

1380-
result = request.post(url, data, timeout=kwargs.get('timeout'))
1381-
1382-
return result
1346+
return self._post_message(url, data, **kwargs)
13831347

13841348
@log
13851349
def getChatMember(self, chat_id, user_id, **kwargs):
@@ -1410,9 +1374,7 @@ def getChatMember(self, chat_id, user_id, **kwargs):
14101374

14111375
data = {'chat_id': chat_id, 'user_id': user_id}
14121376

1413-
result = request.post(url, data, timeout=kwargs.get('timeout'))
1414-
1415-
return ChatMember.de_json(result)
1377+
return self._post_message(url, data, ChatMember, **kwargs)
14161378

14171379
@staticmethod
14181380
def de_json(data):

0 commit comments

Comments
 (0)