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 @@ -65,6 +65,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `thodnev <https://github.com/thodnev>`_
- `Valentijn <https://github.com/Faalentijn>`_
- `voider1 <https://github.com/voider1>`_
- `Wagner Macedo <https://github.com/wagnerluis1982>`_
- `wjt <https://github.com/wjt>`_

Please add yourself here alphabetically when you submit your first pull request.
7 changes: 5 additions & 2 deletions telegram/ext/commandhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,18 @@ def check_update(self, update):
command.append(
message.bot.username) # in case the command was sent without a username

if not (command[0].lower() in self.command
and command[1].lower() == message.bot.username.lower()):
return False

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 (command[0].lower() in self.command
and command[1].lower() == message.bot.username.lower())
return res

return False

Expand Down
21 changes: 20 additions & 1 deletion tests/test_commandhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from telegram import (Message, Update, Chat, Bot, User, CallbackQuery, InlineQuery,
ChosenInlineResult, ShippingQuery, PreCheckoutQuery)
from telegram.ext import CommandHandler, Filters
from telegram.ext import CommandHandler, Filters, BaseFilter

message = Message(1, User(1, '', False), None, Chat(1, ''), text='test')

Expand Down Expand Up @@ -246,3 +246,22 @@ def test_pass_job_or_update_queue(self, dp, message):
def test_other_update_types(self, false_update):
handler = CommandHandler('test', self.callback_basic)
assert not handler.check_update(false_update)

def test_filters_for_wrong_command(self, message):
"""Filters should not be executed if the command does not match the handler"""

class TestFilter(BaseFilter):
def __init__(self):
self.tested = False

def filter(self, message):
self.tested = True

test_filter = TestFilter()

handler = CommandHandler('foo', self.callback_basic, filters=test_filter)
message.text = '/bar'

handler.check_update(Update(0, message=message))

assert not test_filter.tested