2020"""This module contains the MessageHandler class."""
2121import warnings
2222
23+ from telegram .utils .deprecate import TelegramDeprecationWarning
24+
2325from telegram import Update
26+ from telegram .ext import Filters
2427from .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 :
0 commit comments