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
33 changes: 18 additions & 15 deletions telegram/ext/messagehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class MessageHandler(Handler):
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
allow_edited (Optional[bool]): If the handler should also accept edited messages.
Default is ``False``
pass_update_queue (optional[bool]): If the handler should be passed the
update queue as a keyword argument called ``update_queue``. It can
be used to insert updates. Default is ``False``
Expand All @@ -52,8 +50,12 @@ 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``.
allow_edited (Optional[bool]): If the handler should also accept edited messages.
Default is ``False`` - Deprecated. use edited updates instead.
channel_post_updates (Optional[bool]): Should channel posts updates be handled? Default is
``True``.
edited_updates (Optional[bool]): Should "edited" message updates be handled? Default is
``False``.

"""

Expand All @@ -66,9 +68,14 @@ def __init__(self,
pass_user_data=False,
pass_chat_data=False,
message_updates=True,
channel_post_updates=True):
if not message_updates and not channel_post_updates:
raise ValueError('Both message_updates & channel_post_updates are False')
channel_post_updates=True,
edited_updates=False):
if not message_updates and not channel_post_updates and not edited_updates:
raise ValueError(
'message_updates, channel_post_updates and edited_updates are all False')
if allow_edited:
warnings.warn('allow_edited is getting deprecated, please use edited_updates instead')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should put a line below such as this:

edited_updates = allow_edited

Copy link
Copy Markdown
Member

@jsmnbom jsmnbom May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't... if you rename an argument, then it is sooo much nicer to also rename the attribute in the class while you are at it. Then in the future when we wanna remove the deprecated stuff, we don't have to change a ton of stuff.
So well... do what @evgfilim1 is suggesting, just the other way around :D

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not renaming an argument, I'm adding behavior and warning the old behavior is going to be deprecated.
What you suggest would be valid if allow_edited would reflect the change but it doesn't.
If I do it this way, it will implement the new functionality and still work with the old way. If you want to remove the deprecated way in the future you need to remove three lines in the code instead of removing thee and modifying two.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are indeed right... I'm sorry, I had the arguments flipped around in my head

edited_updates = allow_edited

super(MessageHandler, self).__init__(
callback,
Expand All @@ -77,9 +84,9 @@ def __init__(self,
pass_user_data=pass_user_data,
pass_chat_data=pass_chat_data)
self.filters = filters
self.allow_edited = allow_edited
self.message_updates = message_updates
self.channel_post_updates = channel_post_updates
self.edited_updates = edited_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 @@ -88,17 +95,13 @@ def __init__(self,
'deprecated, please use bitwise operators (& and |) '
'instead. More info: https://git.io/vPTbc.')

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 _is_allowed_update(self, update):
return any([(self.message_updates and update.message),
(self.edited_updates and update.edited_message),
(self.channel_post_updates and update.channel_post)])

def check_update(self, update):
if (isinstance(update, Update)
and (self._is_allowed_message(update) or self._is_allowed_channel_post(update))):
if isinstance(update, Update) and self._is_allowed_update(update):

if not self.filters:
res = True
Expand Down
21 changes: 15 additions & 6 deletions tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def telegramHandlerTest(self, bot, update):
self.message_count += 1

def telegramHandlerEditedTest(self, bot, update):
self.received_message = update.edited_message.text
self.received_message = update.effective_message.text
self.message_count += 1

def telegramInlineHandlerTest(self, bot, update):
Expand Down Expand Up @@ -188,22 +188,31 @@ def test_editedMessageHandler(self):
self._setup_updater('Test', edited=True)
d = self.updater.dispatcher
from telegram.ext import Filters
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=True)
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, edited_updates=True)
d.add_handler(handler)
self.updater.start_polling(0.01)
sleep(.1)
self.assertEqual(self.received_message, 'Test')

# Remove handler
self.reset()
d.remove_handler(handler)
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=False)
handler = MessageHandler(
Filters.text,
self.telegramHandlerEditedTest,
edited_updates=False,
message_updates=False)
d.add_handler(handler)
self.reset()

self.updater.bot.send_messages = 1
sleep(.1)
self.assertTrue(None is self.received_message)

handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=True)
d.add_handler(handler)
self.reset()
self.updater.bot.send_messages = 1
sleep(.1)
self.assertEqual(self.received_message, 'Test')

def test_addTelegramMessageHandlerMultipleMessages(self):
self._setup_updater('Multiple', 100)
self.updater.dispatcher.add_handler(MessageHandler(Filters.all, self.telegramHandlerTest))
Expand Down
X Tutup