1818# along with this program. If not, see [http://www.gnu.org/licenses/].
1919"""This module contains the CommandHandler and PrefixHandler classes."""
2020import re
21+ import warnings
2122
2223from future .utils import string_types
2324
25+ from telegram .ext import Filters
26+ from telegram .utils .deprecate import TelegramDeprecationWarning
27+
2428from telegram import Update , MessageEntity
2529from .handler import Handler
2630
@@ -33,6 +37,9 @@ class CommandHandler(Handler):
3337 :class:`CallbackContext` named :attr:`CallbackContext.args`. It will contain a list of strings,
3438 which is the text following the command split on single or consecutive whitespace characters.
3539
40+ By default the handler listens to messages as well as edited messages. To change this behavior
41+ use ``~Filters.update_type.edited_message``.
42+
3643 Attributes:
3744 command (:obj:`str` | List[:obj:`str`]): The command or list of commands this handler
3845 should listen for. Limitations are the same as described here
@@ -80,6 +87,8 @@ class CommandHandler(Handler):
8087 operators (& for and, | for or, ~ for not).
8188 allow_edited (:obj:`bool`, optional): Determines whether the handler should also accept
8289 edited messages. Default is ``False``.
90+ DEPRECATED: Edited is allowed by default. To change this behavior use
91+ ``~Filters.update_type.edited_message``.
8392 pass_args (:obj:`bool`, optional): Determines whether the handler should be passed the
8493 arguments passed to the command as a keyword argument called ``args``. It will contain
8594 a list of strings, which is the text following the command split on single or
@@ -110,7 +119,7 @@ def __init__(self,
110119 command ,
111120 callback ,
112121 filters = None ,
113- allow_edited = False ,
122+ allow_edited = None ,
114123 pass_args = False ,
115124 pass_update_queue = False ,
116125 pass_job_queue = False ,
@@ -131,8 +140,17 @@ def __init__(self,
131140 if not re .match (r'^[\da-z_]{1,32}$' , comm ):
132141 raise ValueError ('Command is not a valid bot command' )
133142
134- self .filters = filters
135- self .allow_edited = allow_edited
143+ if filters :
144+ self .filters = Filters .update_type .messages & filters
145+ else :
146+ self .filters = Filters .update_type .messages
147+
148+ if allow_edited is not None :
149+ warnings .warn ('allow_edited is deprecated. See https://git.io/vp113 for more info' ,
150+ TelegramDeprecationWarning ,
151+ stacklevel = 2 )
152+ if not allow_edited :
153+ self .filters &= ~ Filters .update_type .edited_message
136154 self .pass_args = pass_args
137155
138156 def check_update (self , update ):
@@ -145,8 +163,7 @@ def check_update(self, update):
145163 :obj:`bool`
146164
147165 """
148- if (isinstance (update , Update ) and
149- (update .message or update .edited_message and self .allow_edited )):
166+ if (isinstance (update , Update ) and update .effective_message ):
150167 message = update .effective_message
151168
152169 if (message .entities and message .entities [0 ].type == MessageEntity .BOT_COMMAND and
@@ -160,7 +177,7 @@ def check_update(self, update):
160177 command [1 ].lower () == message .bot .username .lower ()):
161178 return None
162179
163- if self .filters is None or self . filters (update ):
180+ if self .filters (update ):
164181 return args
165182
166183 def collect_optional_args (self , dispatcher , update = None , check_result = None ):
@@ -198,15 +215,16 @@ class PrefixHandler(CommandHandler):
198215 PrefixHandler(['!', '#'], ['test', 'help`], callback) will respond to '!test',
199216 '#test', '!help' and '#help'.
200217
218+ By default the handler listens to messages as well as edited messages. To change this behavior
219+ use ``~Filters.update_type.edited_message``.
220+
201221 Attributes:
202222 prefix (:obj:`str` | List[:obj:`str`]): The prefix(es) that will precede :attr:`command`.
203223 command (:obj:`str` | List[:obj:`str`]): The command or list of commands this handler
204224 should listen for.
205225 callback (:obj:`callable`): The callback function for this handler.
206226 filters (:class:`telegram.ext.BaseFilter`): Optional. Only allow updates with these
207227 Filters.
208- allow_edited (:obj:`bool`): Determines Whether the handler should also accept
209- edited messages.
210228 pass_args (:obj:`bool`): Determines whether the handler should be passed
211229 ``args``.
212230 pass_update_queue (:obj:`bool`): Determines whether ``update_queue`` will be
@@ -243,8 +261,6 @@ class PrefixHandler(CommandHandler):
243261 :class:`telegram.ext.filters.BaseFilter`. Standard filters can be found in
244262 :class:`telegram.ext.filters.Filters`. Filters can be combined using bitwise
245263 operators (& for and, | for or, ~ for not).
246- allow_edited (:obj:`bool`, optional): Determines whether the handler should also accept
247- edited messages. Default is ``False``.
248264 pass_args (:obj:`bool`, optional): Determines whether the handler should be passed the
249265 arguments passed to the command as a keyword argument called ``args``. It will contain
250266 a list of strings, which is the text following the command split on single or
@@ -274,15 +290,14 @@ def __init__(self,
274290 command ,
275291 callback ,
276292 filters = None ,
277- allow_edited = False ,
278293 pass_args = False ,
279294 pass_update_queue = False ,
280295 pass_job_queue = False ,
281296 pass_user_data = False ,
282297 pass_chat_data = False ):
283298
284299 super (PrefixHandler , self ).__init__ (
285- 'nocommand' , callback , filters = filters , allow_edited = allow_edited , pass_args = pass_args ,
300+ 'nocommand' , callback , filters = filters , allow_edited = None , pass_args = pass_args ,
286301 pass_update_queue = pass_update_queue ,
287302 pass_job_queue = pass_job_queue ,
288303 pass_user_data = pass_user_data ,
@@ -308,12 +323,11 @@ def check_update(self, update):
308323 :obj:`bool`
309324
310325 """
311- if (isinstance (update , Update ) and
312- (update .message or update .edited_message and self .allow_edited )):
326+ if (isinstance (update , Update ) and update .effective_message ):
313327 message = update .effective_message
314328
315329 text_list = message .text .split ()
316330 if text_list [0 ].lower () not in self .command :
317331 return None
318- if self .filters is None or self . filters (update ):
332+ if self .filters (update ):
319333 return text_list [1 :]
0 commit comments