Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Jacob Bom <https://github.com/bomjacob>`_
- `JASON0916 <https://github.com/JASON0916>`_
- `jh0ker <https://github.com/jh0ker>`_
- `jossalgon <https://github.com/jossalgon>`_
- `JRoot3D <https://github.com/JRoot3D>`_
- `jlmadurga <https://github.com/jlmadurga>`_
- `Kjwon15 <https://github.com/kjwon15>`_
Expand Down
26 changes: 24 additions & 2 deletions telegram/ext/commandhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
""" This module contains the CommandHandler class """
import warnings

from .handler import Handler
from telegram import Update
Expand All @@ -34,6 +35,10 @@ class CommandHandler(Handler):
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
filters (telegram.ext.BaseFilter): A filter inheriting from
:class:`telegram.ext.filters.BaseFilter`. Standard filters can be found in
:class:`telegram.ext.filters.Filters`. Filters can be combined using bitwise
operators (& for and, | for or).
allow_edited (Optional[bool]): If the handler should also accept edited messages.
Default is ``False``
pass_args (optional[bool]): If the handler should be passed the
Expand Down Expand Up @@ -62,6 +67,7 @@ class CommandHandler(Handler):
def __init__(self,
command,
callback,
filters=None,
allow_edited=False,
pass_args=False,
pass_update_queue=False,
Expand All @@ -75,9 +81,17 @@ def __init__(self,
pass_user_data=pass_user_data,
pass_chat_data=pass_chat_data)
self.command = command
self.filters = filters
self.allow_edited = allow_edited
self.pass_args = pass_args

# We put this up here instead of with the rest of checking code
# in check_update since we don't wanna spam a ton
if isinstance(self.filters, list):
warnings.warn('Using a list of filters in MessageHandler is getting '
'deprecated, please use bitwise operators (& and |) '
'instead. More info: https://git.io/vPTbc.')

def check_update(self, update):
if (isinstance(update, Update)
and (update.message or update.edited_message and self.allow_edited)):
Expand All @@ -87,8 +101,16 @@ def check_update(self, update):
command = message.text[1:].split(' ')[0].split('@')
command.append(
update.message.bot.username) # in case the command was send without a username
return (message.text.startswith('/') and command[0] == self.command
and command[1] == update.message.bot.username)

if self.filters is None:
res = True
elif isinstance(self.filters, list):
res = any(func(message) for func in self.filters)
else:
res = self.filters(message)

return res and (message.text.startswith('/') and command[0] == self.command
and command[1] == update.message.bot.username)
else:
return False

Expand Down
28 changes: 28 additions & 0 deletions tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,34 @@ def test_addRemoveTelegramCommandHandler(self):
sleep(.1)
self.assertTrue(None is self.received_message)

def test_filterPassTelegramCommandHandler(self):
self._setup_updater('', messages=0)
d = self.updater.dispatcher
handler = CommandHandler('test', self.telegramHandlerTest, lambda msg: True)
self.updater.dispatcher.add_handler(handler)
user = User(first_name="singelton", id=404)
bot = self.updater.bot
queue = self.updater.start_polling(0.01)

message = Message(0, user, None, None, text="/test", bot=bot)
queue.put(Update(update_id=0, message=message))
sleep(.1)
self.assertEqual(self.received_message, '/test')

def test_filterNotPassTelegramCommandHandler(self):
self._setup_updater('', messages=0)
d = self.updater.dispatcher
handler = CommandHandler('test', self.telegramHandlerTest, lambda msg: False)
self.updater.dispatcher.add_handler(handler)
user = User(first_name="singelton", id=404)
bot = self.updater.bot
queue = self.updater.start_polling(0.01)

message = Message(0, user, None, None, text="/test", bot=bot)
queue.put(Update(update_id=0, message=message))
sleep(.1)
self.assertTrue(None is self.received_message)

def test_addRemoveStringRegexHandler(self):
self._setup_updater('', messages=0)
d = self.updater.dispatcher
Expand Down