Skip to content

Commit bc3669f

Browse files
Eldinnietsnoam
authored andcommitted
make commandhandler case insensitive
1 parent 7def2c5 commit bc3669f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

telegram/ext/commandhandler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ def __init__(self,
8686
_str = str # Python 3
8787

8888
if isinstance(command, _str):
89-
self.command = [command]
89+
self.command = [command.lower()]
9090
else:
91-
self.command = command
91+
self.command = [x.lower() for x in command]
9292
self.filters = filters
9393
self.allow_edited = allow_edited
9494
self.pass_args = pass_args
@@ -117,7 +117,7 @@ def check_update(self, update):
117117
else:
118118
res = self.filters(message)
119119

120-
return res and (message.text.startswith('/') and command[0] in self.command
120+
return res and (message.text.startswith('/') and command[0].lower() in self.command
121121
and command[1].lower() == message.bot.username.lower())
122122
else:
123123
return False

tests/test_updater.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,19 @@ def test_addRemoveTelegramCommandHandler(self):
271271
sleep(.1)
272272
self.assertTrue(None is self.received_message)
273273

274+
# case insensitivity
275+
self.reset()
276+
message = Message(0, user, None, None, text="/Test", bot=bot)
277+
queue.put(Update(update_id=0, message=message))
278+
sleep(.1)
279+
self.assertTrue(self.received_message, '/Test')
280+
handler = CommandHandler('Test', self.telegramHandlerTest)
281+
self.updater.dispatcher.add_handler(handler)
282+
message = Message(0, user, None, None, text="/test", bot=bot)
283+
queue.put(Update(update_id=0, message=message))
284+
sleep(.1)
285+
self.assertTrue(self.received_message, '/test')
286+
274287
# Remove handler
275288
d.remove_handler(handler)
276289
handler = CommandHandler('test', self.telegramHandlerEditedTest, allow_edited=False)

0 commit comments

Comments
 (0)