Skip to content

Commit 5220aef

Browse files
committed
Commandhandler rework
1 parent bb54b19 commit 5220aef

File tree

5 files changed

+79
-28
lines changed

5 files changed

+79
-28
lines changed

telegram/ext/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
from .updater import Updater
2626
from .callbackqueryhandler import CallbackQueryHandler
2727
from .choseninlineresulthandler import ChosenInlineResultHandler
28-
from .commandhandler import CommandHandler, PrefixHandler
2928
from .inlinequeryhandler import InlineQueryHandler
3029
from .filters import BaseFilter, Filters
3130
from .messagehandler import MessageHandler
31+
from .commandhandler import CommandHandler, PrefixHandler
3232
from .regexhandler import RegexHandler
3333
from .stringcommandhandler import StringCommandHandler
3434
from .stringregexhandler import StringRegexHandler

telegram/ext/commandhandler.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
"""This module contains the CommandHandler and PrefixHandler classes."""
2020
import re
21+
import warnings
2122

2223
from future.utils import string_types
2324

25+
from telegram.ext import Filters
26+
from telegram.utils.deprecate import TelegramDeprecationWarning
27+
2428
from telegram import Update, MessageEntity
2529
from .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:]

telegram/ext/messagehandler.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,4 @@ def check_update(self, update):
166166
167167
"""
168168
if isinstance(update, Update) and update.effective_message:
169-
if not self.filters:
170-
return True
171-
else:
172-
return self.filters(update)
169+
return self.filters(update)

tests/test_commandhandler.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from queue import Queue
2121

2222
import pytest
23+
from telegram.utils.deprecate import TelegramDeprecationWarning
2324

2425
from telegram import (Message, Update, Chat, Bot, User, CallbackQuery, InlineQuery,
2526
ChosenInlineResult, ShippingQuery, PreCheckoutQuery, MessageEntity)
@@ -153,18 +154,39 @@ def test_command_list(self, message):
153154
check = handler.check_update(Update(0, message))
154155
assert check is None or check is False
155156

156-
def test_edited(self, message):
157+
def test_deprecation_warning(self):
158+
with pytest.warns(TelegramDeprecationWarning, match='See https://git.io/vp113'):
159+
CommandHandler('test', self.callback_basic, allow_edited=True)
160+
161+
def test_no_edited(self, message):
162+
handler = CommandHandler('test', self.callback_basic)
163+
message.text = '/test'
164+
check = handler.check_update(Update(0, message))
165+
assert check is not None and check is not False
166+
167+
check = handler.check_update(Update(0, edited_message=message))
168+
assert check is not None and check is not False
169+
157170
handler = CommandHandler('test', self.callback_basic,
158-
allow_edited=False)
171+
filters=~Filters.update_type.edited_message)
172+
check = handler.check_update(Update(0, message))
173+
assert check is not None and check is not False
174+
175+
check = handler.check_update(Update(0, edited_message=message))
176+
assert check is None or check is False
159177

178+
def test_edited_deprecated(self, message):
179+
handler = CommandHandler('test', self.callback_basic,
180+
allow_edited=False)
160181
message.text = '/test'
161182
check = handler.check_update(Update(0, message))
162183
assert check is not None and check is not False
163184

164185
check = handler.check_update(Update(0, edited_message=message))
165186
assert check is None or check is False
166187

167-
handler.allow_edited = True
188+
handler = CommandHandler('test', self.callback_basic,
189+
allow_edited=True)
168190
check = handler.check_update(Update(0, message))
169191
assert check is not None and check is not False
170192

@@ -436,16 +458,34 @@ def test_multi_prefix_single_command(self, prefixmessage):
436458
else:
437459
assert check is None or check is False
438460

439-
def test_edited(self, prefixmessage):
461+
def test_no_edited(self, prefixmessage):
440462
handler = PrefixHandler(['!', '#', 'mytrig-'], ['help', 'test'], self.callback_basic)
463+
check = handler.check_update(Update(0, prefixmessage))
464+
assert check is not None and check is not False
465+
466+
check = handler.check_update(Update(0, edited_message=prefixmessage))
467+
assert check is not None and check is not False
468+
469+
handler = PrefixHandler(['!', '#', 'mytrig-'], ['help', 'test'], self.callback_basic,
470+
filters=~Filters.update_type.edited_message)
471+
check = handler.check_update(Update(0, prefixmessage))
472+
assert check is not None and check is not False
473+
474+
check = handler.check_update(Update(0, edited_message=prefixmessage))
475+
assert check is None or check is False
476+
477+
def test_edited_deprecated(self, prefixmessage):
478+
handler = PrefixHandler(['!', '#', 'mytrig-'], ['help', 'test'], self.callback_basic,
479+
allow_edited=False)
441480

442481
check = handler.check_update(Update(0, prefixmessage))
443482
assert check is not None and check is not False
444483

445484
check = handler.check_update(Update(0, edited_message=prefixmessage))
446485
assert check is None or check is False
447486

448-
handler.allow_edited = True
487+
handler = PrefixHandler(['!', '#', 'mytrig-'], ['help', 'test'], self.callback_basic,
488+
allow_edited=True)
449489
check = handler.check_update(Update(0, prefixmessage))
450490
assert check is not None and check is not False
451491

tests/test_messagehandler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ def test_none_allowed_deprecated(self):
142142
channel_post_updates=False, edited_updates=False)
143143

144144
def test_with_filter(self, message):
145-
handler = MessageHandler(Filters.command, self.callback_basic)
145+
handler = MessageHandler(Filters.group, self.callback_basic)
146146

147-
message.text = '/test'
147+
message.chat.type = 'group'
148148
assert handler.check_update(Update(0, message))
149149

150-
message.text = 'test'
150+
message.chat.type = 'private'
151151
assert not handler.check_update(Update(0, message))
152152

153153
def test_specific_filters(self, message):

0 commit comments

Comments
 (0)