X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion telegram/ext/conversationhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ def __init__(self,
raise ValueError("If 'per_chat=True', 'InlineQueryHandler' doesn't work")

def _get_key(self, update):
chat, user = update.extract_chat_and_user()
chat = update.effective_chat
user = update.effective_user

key = list()

if self.per_chat:
Expand Down
5 changes: 3 additions & 2 deletions telegram/ext/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ def collect_optional_args(self, dispatcher, update=None):
if self.pass_job_queue:
optional_args['job_queue'] = dispatcher.job_queue
if self.pass_user_data or self.pass_chat_data:
chat, user = update.extract_chat_and_user()
chat = update.effective_chat
user = update.effective_user

if self.pass_user_data:
optional_args['user_data'] = dispatcher.user_data[user.id]
optional_args['user_data'] = dispatcher.user_data[user.id if user else None]

if self.pass_chat_data:
optional_args['chat_data'] = dispatcher.chat_data[chat.id if chat else None]
Expand Down
13 changes: 6 additions & 7 deletions telegram/ext/messagehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MessageHandler(Handler):
For each update in the same chat, it will be the same ``dict``. Default is ``False``.
message_updates (Optional[bool]): Should "normal" message updates be handled? Default is
``True``.
channel_posts_updates (Optional[bool]): Should channel posts updates be handled? Default is
channel_post_updates (Optional[bool]): Should channel posts updates be handled? Default is
``True``.

"""
Expand All @@ -67,8 +67,8 @@ def __init__(self,
pass_user_data=False,
pass_chat_data=False,
message_updates=True,
channel_posts_updates=True):
if not message_updates and not channel_posts_updates:
channel_post_updates=True):
if not message_updates and not channel_post_updates:
raise ValueError('Both message_updates & channel_post_updates are False')

super(MessageHandler, self).__init__(
Expand All @@ -80,7 +80,7 @@ def __init__(self,
self.filters = filters
self.allow_edited = allow_edited
self.message_updates = message_updates
self.channel_posts_updates = channel_posts_updates
self.channel_post_updates = channel_post_updates

# We put this up here instead of with the rest of checking code
# in check_update since we don't wanna spam a ton
Expand All @@ -94,7 +94,7 @@ def _is_allowed_message(self, update):
and (update.message or (update.edited_message and self.allow_edited)))

def _is_allowed_channel_post(self, update):
return (self.channel_posts_updates
return (self.channel_post_updates
and (update.channel_post or (update.edited_channel_post and self.allow_edited)))

def check_update(self, update):
Expand All @@ -105,8 +105,7 @@ def check_update(self, update):
res = True

else:
message = (update.message or update.edited_message or update.channel_post
or update.edited_channel_post)
message = update.effective_message
if isinstance(self.filters, list):
res = any(func(message) for func in self.filters)
else:
Expand Down
24 changes: 20 additions & 4 deletions telegram/ext/regexhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def __init__(self,
pass_update_queue=False,
pass_job_queue=False,
pass_user_data=False,
pass_chat_data=False):
pass_chat_data=False,
allow_edited=False,
message_updates=True,
channel_post_updates=False):
super(RegexHandler, self).__init__(
callback,
pass_update_queue=pass_update_queue,
Expand All @@ -85,17 +88,30 @@ def __init__(self,
self.pattern = pattern
self.pass_groups = pass_groups
self.pass_groupdict = pass_groupdict
self.allow_edited = allow_edited
self.message_updates = message_updates
self.channel_post_updates = channel_post_updates

def _is_allowed_message(self, update):
return (self.message_updates
and (update.message or (update.edited_message and self.allow_edited)))

def _is_allowed_channel_post(self, update):
return (self.channel_post_updates
and (update.channel_post or (update.edited_channel_post and self.allow_edited)))

def check_update(self, update):
if isinstance(update, Update) and update.message and update.message.text:
match = re.match(self.pattern, update.message.text)
if (isinstance(update, Update)
and (self._is_allowed_message(update) or self._is_allowed_channel_post(update))
and update.effective_message.text):
match = re.match(self.pattern, update.effective_message.text)
return bool(match)
else:
return False

def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher, update)
match = re.match(self.pattern, update.message.text)
match = re.match(self.pattern, update.effective_message.text)

if self.pass_groups:
optional_args['groups'] = match.groups()
Expand Down
102 changes: 60 additions & 42 deletions telegram/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def __init__(self,
self.channel_post = channel_post
self.edited_channel_post = edited_channel_post

self._effective_user = None
self._effective_chat = None
self._effective_message = None

@staticmethod
def de_json(data, bot):
"""
Expand All @@ -99,25 +103,23 @@ def de_json(data, bot):

return Update(**data)

def extract_chat_and_user(self):
@property
def effective_user(self):
"""
Helper method to get the sender's chat and user objects from an arbitrary update.
Depending on the type of update, one of the available attributes ``message``,
``edited_message`` or ``callback_query`` is used to determine the result.

Returns:
tuple: of (chat, user), with None-values if no object could not be found.
A property that contains the ``User`` that sent this update, no matter what kind of update
this is. Will be ``None`` for channel posts.
"""

if self._effective_user:
return self._effective_user

user = None
chat = None

if self.message:
user = self.message.from_user
chat = self.message.chat

elif self.edited_message:
user = self.edited_message.from_user
chat = self.edited_message.chat

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

elif self.callback_query:
user = self.callback_query.from_user
chat = self.callback_query.message.chat if self.callback_query.message else None

return chat, user
self._effective_user = user
return user

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

Returns:
str: The extracted message text
if self._effective_chat:
return self._effective_chat

Raises:
ValueError: If no message text was found in the update
chat = None

"""
if self.message:
return self.message.text
chat = self.message.chat

elif self.edited_message:
return self.edited_message.text
elif self.callback_query:
return self.callback_query.message.text
else:
raise ValueError("Update contains no message text.")
chat = self.edited_message.chat

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

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

Raises:
ValueError: If no entities were found in the update
elif self.edited_channel_post:
chat = self.edited_channel_post.chat

self._effective_chat = chat
return chat

@property
def effective_message(self):
"""
A property that contains the ``Message`` included in this update, no matter what kind
of update this is. Will be ``None`` for inline queries, chosen inline results and callback
queries from inline messages.
"""

if self._effective_message:
return self._effective_message

message = None

if self.message:
return self.message.parse_entities()
message = self.message

elif self.edited_message:
return self.edited_message.parse_entities()
message = self.edited_message

elif self.callback_query:
return self.callback_query.message.parse_entities()
else:
raise ValueError("No message object found in self, therefore no entities available.")
message = self.callback_query.message

elif self.channel_post:
message = self.channel_post

elif self.edited_channel_post:
message = self.edited_channel_post

self._effective_message = message
return message
14 changes: 9 additions & 5 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,20 @@ def test_update_to_dict(self):
self.assertEqual(update['update_id'], self.update_id)
self.assertTrue(isinstance(update['message'], telegram.Message))

def test_extract_chat_and_user(self):
def test_effective_chat(self):
update = telegram.Update.de_json(self.json_dict, self._bot)
chat, user = update.extract_chat_and_user()
chat = update.effective_chat
self.assertEqual(update.message.chat, chat)

def test_effective_user(self):
update = telegram.Update.de_json(self.json_dict, self._bot)
user = update.effective_user
self.assertEqual(update.message.from_user, user)

def test_extract_message_text(self):
def test_effective_message(self):
update = telegram.Update.de_json(self.json_dict, self._bot)
text = update.extract_message_text()
self.assertEqual(update.message.text, text)
message = update.effective_message
self.assertEqual(update.message.text, message.text)


if __name__ == '__main__':
Expand Down
X Tutup