X Tutup
Skip to content

Commit e2a651a

Browse files
Eldinniejh0ker
authored andcommitted
Allow edited as seperate input (python-telegram-bot#608)
* Allow edited as seperate input In short made it possible to tune messagehandler more to your wishes. and choose exactly what updates to receive. messages, edited_message or channel_post or a combination. - Added the edited_updates argument to MessageHandler - Added DepricationWarning when using allow_edited - replaced _is_allowed_message and _is_allowed_channel_post with _is_allowed_update - Modified tests to reflect new way * oops Spelled deprecation wrong made an error in the _is_allowed_update. * Python 2 does not have assertWarns. * remove unneeded statements
1 parent 33512ff commit e2a651a

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

telegram/ext/messagehandler.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class MessageHandler(Handler):
3737
callback (function): A function that takes ``bot, update`` as
3838
positional arguments. It will be called when the ``check_update``
3939
has determined that an update should be processed by this handler.
40-
allow_edited (Optional[bool]): If the handler should also accept edited messages.
41-
Default is ``False``
4240
pass_update_queue (optional[bool]): If the handler should be passed the
4341
update queue as a keyword argument called ``update_queue``. It can
4442
be used to insert updates. Default is ``False``
@@ -52,8 +50,12 @@ class MessageHandler(Handler):
5250
For each update in the same chat, it will be the same ``dict``. Default is ``False``.
5351
message_updates (Optional[bool]): Should "normal" message updates be handled? Default is
5452
``True``.
53+
allow_edited (Optional[bool]): If the handler should also accept edited messages.
54+
Default is ``False`` - Deprecated. use edited updates instead.
5555
channel_post_updates (Optional[bool]): Should channel posts updates be handled? Default is
5656
``True``.
57+
edited_updates (Optional[bool]): Should "edited" message updates be handled? Default is
58+
``False``.
5759
5860
"""
5961

@@ -66,9 +68,14 @@ def __init__(self,
6668
pass_user_data=False,
6769
pass_chat_data=False,
6870
message_updates=True,
69-
channel_post_updates=True):
70-
if not message_updates and not channel_post_updates:
71-
raise ValueError('Both message_updates & channel_post_updates are False')
71+
channel_post_updates=True,
72+
edited_updates=False):
73+
if not message_updates and not channel_post_updates and not edited_updates:
74+
raise ValueError(
75+
'message_updates, channel_post_updates and edited_updates are all False')
76+
if allow_edited:
77+
warnings.warn('allow_edited is getting deprecated, please use edited_updates instead')
78+
edited_updates = allow_edited
7279

7380
super(MessageHandler, self).__init__(
7481
callback,
@@ -77,9 +84,9 @@ def __init__(self,
7784
pass_user_data=pass_user_data,
7885
pass_chat_data=pass_chat_data)
7986
self.filters = filters
80-
self.allow_edited = allow_edited
8187
self.message_updates = message_updates
8288
self.channel_post_updates = channel_post_updates
89+
self.edited_updates = edited_updates
8390

8491
# We put this up here instead of with the rest of checking code
8592
# in check_update since we don't wanna spam a ton
@@ -88,17 +95,13 @@ def __init__(self,
8895
'deprecated, please use bitwise operators (& and |) '
8996
'instead. More info: https://git.io/vPTbc.')
9097

91-
def _is_allowed_message(self, update):
92-
return (self.message_updates
93-
and (update.message or (update.edited_message and self.allow_edited)))
94-
95-
def _is_allowed_channel_post(self, update):
96-
return (self.channel_post_updates
97-
and (update.channel_post or (update.edited_channel_post and self.allow_edited)))
98+
def _is_allowed_update(self, update):
99+
return any([(self.message_updates and update.message),
100+
(self.edited_updates and update.edited_message),
101+
(self.channel_post_updates and update.channel_post)])
98102

99103
def check_update(self, update):
100-
if (isinstance(update, Update)
101-
and (self._is_allowed_message(update) or self._is_allowed_channel_post(update))):
104+
if isinstance(update, Update) and self._is_allowed_update(update):
102105

103106
if not self.filters:
104107
res = True

tests/test_updater.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def telegramHandlerTest(self, bot, update):
108108
self.message_count += 1
109109

110110
def telegramHandlerEditedTest(self, bot, update):
111-
self.received_message = update.edited_message.text
111+
self.received_message = update.effective_message.text
112112
self.message_count += 1
113113

114114
def telegramInlineHandlerTest(self, bot, update):
@@ -188,22 +188,31 @@ def test_editedMessageHandler(self):
188188
self._setup_updater('Test', edited=True)
189189
d = self.updater.dispatcher
190190
from telegram.ext import Filters
191-
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=True)
191+
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, edited_updates=True)
192192
d.add_handler(handler)
193193
self.updater.start_polling(0.01)
194194
sleep(.1)
195195
self.assertEqual(self.received_message, 'Test')
196196

197-
# Remove handler
197+
self.reset()
198198
d.remove_handler(handler)
199-
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=False)
199+
handler = MessageHandler(
200+
Filters.text,
201+
self.telegramHandlerEditedTest,
202+
edited_updates=False,
203+
message_updates=False)
200204
d.add_handler(handler)
201-
self.reset()
202-
203205
self.updater.bot.send_messages = 1
204206
sleep(.1)
205207
self.assertTrue(None is self.received_message)
206208

209+
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=True)
210+
d.add_handler(handler)
211+
self.reset()
212+
self.updater.bot.send_messages = 1
213+
sleep(.1)
214+
self.assertEqual(self.received_message, 'Test')
215+
207216
def test_addTelegramMessageHandlerMultipleMessages(self):
208217
self._setup_updater('Multiple', 100)
209218
self.updater.dispatcher.add_handler(MessageHandler(Filters.all, self.telegramHandlerTest))

0 commit comments

Comments
 (0)
X Tutup