@@ -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
0 commit comments