Skip to content

Commit bb54b19

Browse files
committed
Messagehandler rework
- remove allow_edited (deprecated for a while) - set deprecated defaults to None - Raise deprecation warning when they're used - add sensible defaults for filters. - rework tests
1 parent a9c166d commit bb54b19

File tree

3 files changed

+76
-47
lines changed

3 files changed

+76
-47
lines changed

telegram/ext/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
from .choseninlineresulthandler import ChosenInlineResultHandler
2828
from .commandhandler import CommandHandler, PrefixHandler
2929
from .inlinequeryhandler import InlineQueryHandler
30-
from .messagehandler import MessageHandler
3130
from .filters import BaseFilter, Filters
31+
from .messagehandler import MessageHandler
3232
from .regexhandler import RegexHandler
3333
from .stringcommandhandler import StringCommandHandler
3434
from .stringregexhandler import StringRegexHandler

telegram/ext/messagehandler.py

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
"""This module contains the MessageHandler class."""
2121
import warnings
2222

23+
from telegram.utils.deprecate import TelegramDeprecationWarning
24+
2325
from telegram import Update
26+
from telegram.ext import Filters
2427
from .handler import Handler
2528

2629

@@ -40,13 +43,11 @@ class MessageHandler(Handler):
4043
pass_chat_data (:obj:`bool`): Determines whether ``chat_data`` will be passed to
4144
the callback function.
4245
message_updates (:obj:`bool`): Should "normal" message updates be handled?
43-
Default is ``True``.
46+
Default is ``None``.
4447
channel_post_updates (:obj:`bool`): Should channel posts updates be handled?
45-
Default is ``True``.
48+
Default is ``None``.
4649
edited_updates (:obj:`bool`): Should "edited" message updates be handled?
47-
Default is ``False``.
48-
allow_edited (:obj:`bool`): If the handler should also accept edited messages.
49-
Default is ``False`` - Deprecated. use edited_updates instead.
50+
Default is ``None``.
5051
5152
Note:
5253
:attr:`pass_user_data` and :attr:`pass_chat_data` determine whether a ``dict`` you
@@ -61,7 +62,9 @@ class MessageHandler(Handler):
6162
filters (:class:`telegram.ext.BaseFilter`, optional): A filter inheriting from
6263
:class:`telegram.ext.filters.BaseFilter`. Standard filters can be found in
6364
:class:`telegram.ext.filters.Filters`. Filters can be combined using bitwise
64-
operators (& for and, | for or, ~ for not).
65+
operators (& for and, | for or, ~ for not). Default is
66+
``Filters.update_type``. If you don't want or need any of those
67+
pass ``~Filters.update_type.*``
6568
callback (:obj:`callable`): The callback function for this handler. Will be called when
6669
:attr:`check_update` has determined that an update should be processed by this handler.
6770
Callback signature for context based API:
@@ -87,13 +90,14 @@ class MessageHandler(Handler):
8790
``chat_data`` will be passed to the callback function. Default is ``False``.
8891
DEPRECATED: Please switch to context based callbacks.
8992
message_updates (:obj:`bool`, optional): Should "normal" message updates be handled?
90-
Default is ``True``.
93+
Default is ``None``.
94+
DEPRECATED: Please switch to filters for update filtering.
9195
channel_post_updates (:obj:`bool`, optional): Should channel posts updates be handled?
92-
Default is ``True``.
96+
Default is ``None``.
97+
DEPRECATED: Please switch to filters for update filtering.
9398
edited_updates (:obj:`bool`, optional): Should "edited" message updates be handled? Default
94-
is ``False``.
95-
allow_edited (:obj:`bool`, optional): If the handler should also accept edited messages.
96-
Default is ``False`` - Deprecated. use edited_updates instead.
99+
is ``None``.
100+
DEPRECATED: Please switch to filters for update filtering.
97101
98102
Raises:
99103
ValueError
@@ -103,36 +107,53 @@ class MessageHandler(Handler):
103107
def __init__(self,
104108
filters,
105109
callback,
106-
allow_edited=False,
107110
pass_update_queue=False,
108111
pass_job_queue=False,
109112
pass_user_data=False,
110113
pass_chat_data=False,
111-
message_updates=True,
112-
channel_post_updates=True,
113-
edited_updates=False):
114-
if not message_updates and not channel_post_updates and not edited_updates:
115-
raise ValueError(
116-
'message_updates, channel_post_updates and edited_updates are all False')
117-
if allow_edited:
118-
warnings.warn('allow_edited is getting deprecated, please use edited_updates instead')
119-
edited_updates = allow_edited
114+
message_updates=None,
115+
channel_post_updates=None,
116+
edited_updates=None):
120117

121118
super(MessageHandler, self).__init__(
122119
callback,
123120
pass_update_queue=pass_update_queue,
124121
pass_job_queue=pass_job_queue,
125122
pass_user_data=pass_user_data,
126123
pass_chat_data=pass_chat_data)
124+
if message_updates is False and channel_post_updates is False and edited_updates is False:
125+
raise ValueError(
126+
'message_updates, channel_post_updates and edited_updates are all False')
127127
self.filters = filters
128-
self.message_updates = message_updates
129-
self.channel_post_updates = channel_post_updates
130-
self.edited_updates = edited_updates
131-
132-
def _is_allowed_update(self, update):
133-
return any([self.message_updates and update.message,
134-
self.edited_updates and (update.edited_message or update.edited_channel_post),
135-
self.channel_post_updates and update.channel_post])
128+
if self.filters is not None:
129+
self.filters &= Filters.update_type
130+
else:
131+
self.filters = Filters.update_type
132+
if message_updates is not None:
133+
warnings.warn('message_updates is deprecated. See https://git.io/vp113 for more info',
134+
TelegramDeprecationWarning,
135+
stacklevel=2)
136+
if message_updates is False:
137+
self.filters &= ~Filters.update_type.message
138+
139+
if channel_post_updates is not None:
140+
warnings.warn('channel_post_updates is deprecated. See https://git.io/vp113 '
141+
'for more info',
142+
TelegramDeprecationWarning,
143+
stacklevel=2)
144+
if channel_post_updates is False:
145+
self.filters &= ~Filters.update_type.channel_post
146+
147+
if edited_updates is not None:
148+
warnings.warn('edited_updates is deprecated. See https://git.io/vp113 for more info',
149+
TelegramDeprecationWarning,
150+
stacklevel=2)
151+
if edited_updates:
152+
self.filters |= (Filters.update_type.edited_message |
153+
Filters.update_type.edited_channel_post)
154+
if edited_updates is False:
155+
self.filters &= ~(Filters.update_type.edited_message |
156+
Filters.update_type.edited_channel_post)
136157

137158
def check_update(self, update):
138159
"""Determines whether an update should be passed to this handlers :attr:`callback`.
@@ -144,7 +165,7 @@ def check_update(self, update):
144165
:obj:`bool`
145166
146167
"""
147-
if isinstance(update, Update) and self._is_allowed_update(update):
168+
if isinstance(update, Update) and update.effective_message:
148169
if not self.filters:
149170
return True
150171
else:

tests/test_messagehandler.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from queue import Queue
2020

2121
import pytest
22+
from telegram.utils.deprecate import TelegramDeprecationWarning
2223

2324
from telegram import (Message, Update, Chat, Bot, User, CallbackQuery, InlineQuery,
2425
ChosenInlineResult, ShippingQuery, PreCheckoutQuery)
@@ -100,7 +101,15 @@ def test_basic(self, dp, message):
100101
dp.process_update(Update(0, message))
101102
assert self.test_flag
102103

103-
def test_edited(self, message):
104+
def test_deprecation_warning(self):
105+
with pytest.warns(TelegramDeprecationWarning, match='See https://git.io/vp113'):
106+
MessageHandler(None, self.callback_basic, edited_updates=True)
107+
with pytest.warns(TelegramDeprecationWarning, match='See https://git.io/vp113'):
108+
MessageHandler(None, self.callback_basic, message_updates=False)
109+
with pytest.warns(TelegramDeprecationWarning, match='See https://git.io/vp113'):
110+
MessageHandler(None, self.callback_basic, channel_post_updates=True)
111+
112+
def test_edited_deprecated(self, message):
104113
handler = MessageHandler(None, self.callback_basic, edited_updates=True,
105114
message_updates=False, channel_post_updates=False)
106115

@@ -109,17 +118,16 @@ def test_edited(self, message):
109118
assert not handler.check_update(Update(0, channel_post=message))
110119
assert handler.check_update(Update(0, edited_channel_post=message))
111120

112-
def test_channel_post(self, message):
121+
def test_channel_post_deprecated(self, message):
113122
handler = MessageHandler(None, self.callback_basic,
114123
edited_updates=False, message_updates=False,
115124
channel_post_updates=True)
116-
117125
assert not handler.check_update(Update(0, edited_message=message))
118126
assert not handler.check_update(Update(0, message=message))
119127
assert handler.check_update(Update(0, channel_post=message))
120128
assert not handler.check_update(Update(0, edited_channel_post=message))
121129

122-
def test_multiple_flags(self, message):
130+
def test_multiple_flags_deprecated(self, message):
123131
handler = MessageHandler(None, self.callback_basic, edited_updates=True,
124132
message_updates=True, channel_post_updates=True)
125133

@@ -128,18 +136,7 @@ def test_multiple_flags(self, message):
128136
assert handler.check_update(Update(0, channel_post=message))
129137
assert handler.check_update(Update(0, edited_channel_post=message))
130138

131-
def test_allow_edited(self, message):
132-
with pytest.warns(UserWarning):
133-
handler = MessageHandler(None, self.callback_basic,
134-
message_updates=True, allow_edited=True,
135-
channel_post_updates=False)
136-
137-
assert handler.check_update(Update(0, edited_message=message))
138-
assert handler.check_update(Update(0, message=message))
139-
assert not handler.check_update(Update(0, channel_post=message))
140-
assert handler.check_update(Update(0, edited_channel_post=message))
141-
142-
def test_none_allowed(self):
139+
def test_none_allowed_deprecated(self):
143140
with pytest.raises(ValueError, match='are all False'):
144141
MessageHandler(None, self.callback_basic, message_updates=False,
145142
channel_post_updates=False, edited_updates=False)
@@ -153,6 +150,17 @@ def test_with_filter(self, message):
153150
message.text = 'test'
154151
assert not handler.check_update(Update(0, message))
155152

153+
def test_specific_filters(self, message):
154+
f = (~Filters.update_type.messages &
155+
~Filters.update_type.channel_post &
156+
Filters.update_type.edited_channel_post)
157+
handler = MessageHandler(f, self.callback_basic)
158+
159+
assert not handler.check_update(Update(0, edited_message=message))
160+
assert not handler.check_update(Update(0, message=message))
161+
assert not handler.check_update(Update(0, channel_post=message))
162+
assert handler.check_update(Update(0, edited_channel_post=message))
163+
156164
def test_pass_user_or_chat_data(self, dp, message):
157165
handler = MessageHandler(None, self.callback_data_1,
158166
pass_user_data=True)

0 commit comments

Comments
 (0)