Skip to content

Commit b67ea7a

Browse files
PaulSonOfLarstsnoam
authored andcommitted
CommandHandler - ignore strings in entities and "/" followed by whitespace (python-telegram-bot#1020)
1 parent f6332d4 commit b67ea7a

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The following wonderful people contributed directly or indirectly to this projec
5454
- `Oleg Sushchenko <https://github.com/feuillemorte>`_
5555
- `overquota <https://github.com/overquota>`_
5656
- `Patrick Hofmann <https://github.com/PH89>`_
57+
- `Paul Larsen <https://github.com/PaulSonOfLars>`_
5758
- `Pieter Schutz <https://github.com/eldinnie>`_
5859
- `Rahiel Kasim <https://github.com/rahiel>`_
5960
- `Sascha <https://github.com/saschalalala>`_

telegram/ext/commandhandler.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,23 @@ def check_update(self, update):
134134
message = update.message or update.edited_message
135135

136136
if message.text and message.text.startswith('/') and len(message.text) > 1:
137-
command = message.text[1:].split(None, 1)[0].split('@')
138-
command.append(
139-
message.bot.username) # in case the command was send without a username
140-
141-
if self.filters is None:
142-
res = True
143-
elif isinstance(self.filters, list):
144-
res = any(func(message) for func in self.filters)
145-
else:
146-
res = self.filters(message)
147-
148-
return res and (command[0].lower() in self.command
149-
and command[1].lower() == message.bot.username.lower())
150-
else:
151-
return False
152-
153-
else:
154-
return False
137+
first_word = message.text_html.split(None, 1)[0]
138+
if len(first_word) > 1 and first_word.startswith('/'):
139+
command = first_word[1:].split('@')
140+
command.append(
141+
message.bot.username) # in case the command was sent without a username
142+
143+
if self.filters is None:
144+
res = True
145+
elif isinstance(self.filters, list):
146+
res = any(func(message) for func in self.filters)
147+
else:
148+
res = self.filters(message)
149+
150+
return res and (command[0].lower() in self.command
151+
and command[1].lower() == message.bot.username.lower())
152+
153+
return False
155154

156155
def handle_update(self, update, dispatcher):
157156
"""Send the update to the :attr:`callback`.

tests/test_commandhandler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ def test_single_slash(self, dp, message):
190190
message.text = '/'
191191
assert not handler.check_update(Update(0, message))
192192

193+
message.text = '/ test'
194+
assert not handler.check_update(Update(0, message))
195+
193196
def test_pass_user_or_chat_data(self, dp, message):
194197
handler = CommandHandler('test', self.callback_data_1, pass_user_data=True)
195198
dp.add_handler(handler)

0 commit comments

Comments
 (0)