diff --git a/AUTHORS.rst b/AUTHORS.rst index 48ae3efcc..afb7eed0d 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -66,6 +66,7 @@ The following wonderful people contributed directly or indirectly to this projec - `thodnev `_ - `Valentijn `_ - `voider1 `_ +- `Wagner Macedo `_ - `wjt `_ Please add yourself here alphabetically when you submit your first pull request. diff --git a/telegram/ext/commandhandler.py b/telegram/ext/commandhandler.py index 3fffa557f..82e8a40b6 100644 --- a/telegram/ext/commandhandler.py +++ b/telegram/ext/commandhandler.py @@ -140,6 +140,10 @@ class CommandHandler(Handler): 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): @@ -147,8 +151,7 @@ class CommandHandler(Handler): 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 diff --git a/tests/test_commandhandler.py b/tests/test_commandhandler.py index a8140cb6b..c8ff4a956 100644 --- a/tests/test_commandhandler.py +++ b/tests/test_commandhandler.py @@ -21,7 +21,7 @@ import pytest 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') @@ -246,3 +246,22 @@ class TestCommandHandler(object): 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