X Tutup
Skip to content

Commit e78d11a

Browse files
jossalgonjh0ker
authored andcommitted
Add filters to commandHandler (python-telegram-bot#536)
* Add filters to commandHandler * Add commandHandler tests with filters * Add myself to authors
1 parent cc73469 commit e78d11a

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The following wonderful people contributed directly or indirectly to this projec
2121
- `Jacob Bom <https://github.com/bomjacob>`_
2222
- `JASON0916 <https://github.com/JASON0916>`_
2323
- `jh0ker <https://github.com/jh0ker>`_
24+
- `jossalgon <https://github.com/jossalgon>`_
2425
- `JRoot3D <https://github.com/JRoot3D>`_
2526
- `jlmadurga <https://github.com/jlmadurga>`_
2627
- `Kjwon15 <https://github.com/kjwon15>`_

telegram/ext/commandhandler.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
""" This module contains the CommandHandler class """
20+
import warnings
2021

2122
from .handler import Handler
2223
from telegram import Update
@@ -34,6 +35,10 @@ class CommandHandler(Handler):
3435
callback (function): A function that takes ``bot, update`` as
3536
positional arguments. It will be called when the ``check_update``
3637
has determined that an update should be processed by this handler.
38+
filters (telegram.ext.BaseFilter): A filter inheriting from
39+
:class:`telegram.ext.filters.BaseFilter`. Standard filters can be found in
40+
:class:`telegram.ext.filters.Filters`. Filters can be combined using bitwise
41+
operators (& for and, | for or).
3742
allow_edited (Optional[bool]): If the handler should also accept edited messages.
3843
Default is ``False``
3944
pass_args (optional[bool]): If the handler should be passed the
@@ -62,6 +67,7 @@ class CommandHandler(Handler):
6267
def __init__(self,
6368
command,
6469
callback,
70+
filters=None,
6571
allow_edited=False,
6672
pass_args=False,
6773
pass_update_queue=False,
@@ -75,9 +81,17 @@ def __init__(self,
7581
pass_user_data=pass_user_data,
7682
pass_chat_data=pass_chat_data)
7783
self.command = command
84+
self.filters = filters
7885
self.allow_edited = allow_edited
7986
self.pass_args = pass_args
8087

88+
# We put this up here instead of with the rest of checking code
89+
# in check_update since we don't wanna spam a ton
90+
if isinstance(self.filters, list):
91+
warnings.warn('Using a list of filters in MessageHandler is getting '
92+
'deprecated, please use bitwise operators (& and |) '
93+
'instead. More info: https://git.io/vPTbc.')
94+
8195
def check_update(self, update):
8296
if (isinstance(update, Update)
8397
and (update.message or update.edited_message and self.allow_edited)):
@@ -87,8 +101,16 @@ def check_update(self, update):
87101
command = message.text[1:].split(' ')[0].split('@')
88102
command.append(
89103
update.message.bot.username) # in case the command was send without a username
90-
return (message.text.startswith('/') and command[0] == self.command
91-
and command[1] == update.message.bot.username)
104+
105+
if self.filters is None:
106+
res = True
107+
elif isinstance(self.filters, list):
108+
res = any(func(message) for func in self.filters)
109+
else:
110+
res = self.filters(message)
111+
112+
return res and (message.text.startswith('/') and command[0] == self.command
113+
and command[1] == update.message.bot.username)
92114
else:
93115
return False
94116

tests/test_updater.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,34 @@ def test_addRemoveTelegramCommandHandler(self):
268268
sleep(.1)
269269
self.assertTrue(None is self.received_message)
270270

271+
def test_filterPassTelegramCommandHandler(self):
272+
self._setup_updater('', messages=0)
273+
d = self.updater.dispatcher
274+
handler = CommandHandler('test', self.telegramHandlerTest, lambda msg: True)
275+
self.updater.dispatcher.add_handler(handler)
276+
user = User(first_name="singelton", id=404)
277+
bot = self.updater.bot
278+
queue = self.updater.start_polling(0.01)
279+
280+
message = Message(0, user, None, None, text="/test", bot=bot)
281+
queue.put(Update(update_id=0, message=message))
282+
sleep(.1)
283+
self.assertEqual(self.received_message, '/test')
284+
285+
def test_filterNotPassTelegramCommandHandler(self):
286+
self._setup_updater('', messages=0)
287+
d = self.updater.dispatcher
288+
handler = CommandHandler('test', self.telegramHandlerTest, lambda msg: False)
289+
self.updater.dispatcher.add_handler(handler)
290+
user = User(first_name="singelton", id=404)
291+
bot = self.updater.bot
292+
queue = self.updater.start_polling(0.01)
293+
294+
message = Message(0, user, None, None, text="/test", bot=bot)
295+
queue.put(Update(update_id=0, message=message))
296+
sleep(.1)
297+
self.assertTrue(None is self.received_message)
298+
271299
def test_addRemoveStringRegexHandler(self):
272300
self._setup_updater('', messages=0)
273301
d = self.updater.dispatcher

0 commit comments

Comments
 (0)
X Tutup