X Tutup
Skip to content

Commit ff39e24

Browse files
authored
Refactor Update.extract_ methods to Update.effective_ properties (python-telegram-bot#531)
* 🔨 Refactor `Update.extract_` methods to `Update.effective_` properties python-telegram-bot#507 * 🔨 handler.py: use effective_ properties
1 parent 5b14b13 commit ff39e24

File tree

4 files changed

+75
-50
lines changed

4 files changed

+75
-50
lines changed

telegram/ext/conversationhandler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ def __init__(self,
145145
raise ValueError("If 'per_chat=True', 'InlineQueryHandler' doesn't work")
146146

147147
def _get_key(self, update):
148-
chat, user = update.extract_chat_and_user()
148+
chat = update.effective_chat
149+
user = update.effective_user
150+
149151
key = list()
150152

151153
if self.per_chat:

telegram/ext/handler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ def collect_optional_args(self, dispatcher, update=None):
104104
if self.pass_job_queue:
105105
optional_args['job_queue'] = dispatcher.job_queue
106106
if self.pass_user_data or self.pass_chat_data:
107-
chat, user = update.extract_chat_and_user()
107+
chat = update.effective_chat
108+
user = update.effective_user
108109

109110
if self.pass_user_data:
110-
optional_args['user_data'] = dispatcher.user_data[user.id]
111+
optional_args['user_data'] = dispatcher.user_data[user.id if user else None]
111112

112113
if self.pass_chat_data:
113114
optional_args['chat_data'] = dispatcher.chat_data[chat.id if chat else None]

telegram/update.py

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def __init__(self,
7373
self.channel_post = channel_post
7474
self.edited_channel_post = edited_channel_post
7575

76+
self._effective_user = None
77+
self._effective_chat = None
78+
self._effective_message = None
79+
7680
@staticmethod
7781
def de_json(data, bot):
7882
"""
@@ -99,25 +103,23 @@ def de_json(data, bot):
99103

100104
return Update(**data)
101105

102-
def extract_chat_and_user(self):
106+
@property
107+
def effective_user(self):
103108
"""
104-
Helper method to get the sender's chat and user objects from an arbitrary update.
105-
Depending on the type of update, one of the available attributes ``message``,
106-
``edited_message`` or ``callback_query`` is used to determine the result.
107-
108-
Returns:
109-
tuple: of (chat, user), with None-values if no object could not be found.
109+
A property that contains the ``User`` that sent this update, no matter what kind of update
110+
this is. Will be ``None`` for channel posts.
110111
"""
112+
113+
if self._effective_user:
114+
return self._effective_user
115+
111116
user = None
112-
chat = None
113117

114118
if self.message:
115119
user = self.message.from_user
116-
chat = self.message.chat
117120

118121
elif self.edited_message:
119122
user = self.edited_message.from_user
120-
chat = self.edited_message.chat
121123

122124
elif self.inline_query:
123125
user = self.inline_query.from_user
@@ -127,51 +129,67 @@ def extract_chat_and_user(self):
127129

128130
elif self.callback_query:
129131
user = self.callback_query.from_user
130-
chat = self.callback_query.message.chat if self.callback_query.message else None
131132

132-
return chat, user
133+
self._effective_user = user
134+
return user
133135

134-
def extract_message_text(self):
136+
@property
137+
def effective_chat(self):
138+
"""
139+
A property that contains the ``Chat`` that this update was sent in, no matter what kind of
140+
update this is. Will be ``None`` for inline queries and chosen inline results.
135141
"""
136-
Helper method to get the message text from an arbitrary update.
137-
Depending on the type of update, one of the available attributes ``message``,
138-
``edited_message`` or ``callback_query`` is used to determine the result.
139142

140-
Returns:
141-
str: The extracted message text
143+
if self._effective_chat:
144+
return self._effective_chat
142145

143-
Raises:
144-
ValueError: If no message text was found in the update
146+
chat = None
145147

146-
"""
147148
if self.message:
148-
return self.message.text
149+
chat = self.message.chat
150+
149151
elif self.edited_message:
150-
return self.edited_message.text
151-
elif self.callback_query:
152-
return self.callback_query.message.text
153-
else:
154-
raise ValueError("Update contains no message text.")
152+
chat = self.edited_message.chat
155153

156-
def extract_entities(self):
157-
"""
158-
Helper method to get parsed entities from an arbitrary update.
159-
Depending on the type of update, one of the available attributes ``message``,
160-
``edited_message`` or ``callback_query`` is used to determine the result.
154+
elif self.callback_query and self.callback_query.message:
155+
chat = self.callback_query.message.chat
161156

162-
Returns:
163-
dict[:class:`telegram.MessageEntity`, ``str``]: A dictionary of entities mapped to the
164-
text that belongs to them, calculated based on UTF-16 codepoints.
157+
elif self.channel_post:
158+
chat = self.channel_post.chat
165159

166-
Raises:
167-
ValueError: If no entities were found in the update
160+
elif self.edited_channel_post:
161+
chat = self.edited_channel_post.chat
168162

163+
self._effective_chat = chat
164+
return chat
165+
166+
@property
167+
def effective_message(self):
168+
"""
169+
A property that contains the ``Message`` included in this update, no matter what kind
170+
of update this is. Will be ``None`` for inline queries, chosen inline results and callback
171+
queries from inline messages.
169172
"""
173+
174+
if self._effective_message:
175+
return self._effective_message
176+
177+
message = None
178+
170179
if self.message:
171-
return self.message.parse_entities()
180+
message = self.message
181+
172182
elif self.edited_message:
173-
return self.edited_message.parse_entities()
183+
message = self.edited_message
184+
174185
elif self.callback_query:
175-
return self.callback_query.message.parse_entities()
176-
else:
177-
raise ValueError("No message object found in self, therefore no entities available.")
186+
message = self.callback_query.message
187+
188+
elif self.channel_post:
189+
message = self.channel_post
190+
191+
elif self.edited_channel_post:
192+
message = self.edited_channel_post
193+
194+
self._effective_message = message
195+
return message

tests/test_update.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,20 @@ def test_update_to_dict(self):
7676
self.assertEqual(update['update_id'], self.update_id)
7777
self.assertTrue(isinstance(update['message'], telegram.Message))
7878

79-
def test_extract_chat_and_user(self):
79+
def test_effective_chat(self):
8080
update = telegram.Update.de_json(self.json_dict, self._bot)
81-
chat, user = update.extract_chat_and_user()
81+
chat = update.effective_chat
8282
self.assertEqual(update.message.chat, chat)
83+
84+
def test_effective_user(self):
85+
update = telegram.Update.de_json(self.json_dict, self._bot)
86+
user = update.effective_user
8387
self.assertEqual(update.message.from_user, user)
8488

85-
def test_extract_message_text(self):
89+
def test_effective_message(self):
8690
update = telegram.Update.de_json(self.json_dict, self._bot)
87-
text = update.extract_message_text()
88-
self.assertEqual(update.message.text, text)
91+
message = update.effective_message
92+
self.assertEqual(update.message.text, message.text)
8993

9094

9195
if __name__ == '__main__':

0 commit comments

Comments
 (0)
X Tutup