X Tutup
Skip to content

Commit 33512ff

Browse files
evgfilim1jh0ker
authored andcommitted
Improved CommandHandler (python-telegram-bot#613)
* Improved CommandHandler Now you can pass list of commands instead of one command * Added tests for list of commands * Return backward compatibility Renamed `commands` to `command` in CommandHandler * Added test for a command not in the list * Fixed py2 unicode command support in `CommandHandler`
1 parent c2c5452 commit 33512ff

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

telegram/ext/commandhandler.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class CommandHandler(Handler):
3030
name and/or some additional text.
3131
3232
Args:
33-
command (str): The name of the command this handler should listen for.
33+
command (str|list): The name of the command or list of command this handler should
34+
listen for.
3435
callback (function): A function that takes ``bot, update`` as
3536
positional arguments. It will be called when the ``check_update``
3637
has determined that an update should be processed by this handler.
@@ -79,7 +80,15 @@ def __init__(self,
7980
pass_job_queue=pass_job_queue,
8081
pass_user_data=pass_user_data,
8182
pass_chat_data=pass_chat_data)
82-
self.command = command
83+
try:
84+
_str = basestring # Python 2
85+
except NameError:
86+
_str = str # Python 3
87+
88+
if isinstance(command, _str):
89+
self.command = [command]
90+
else:
91+
self.command = command
8392
self.filters = filters
8493
self.allow_edited = allow_edited
8594
self.pass_args = pass_args
@@ -108,7 +117,7 @@ def check_update(self, update):
108117
else:
109118
res = self.filters(message)
110119

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

tests/test_updater.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,35 @@ def test_filterNotPassTelegramCommandHandler(self):
300300
sleep(.1)
301301
self.assertTrue(None is self.received_message)
302302

303+
def test_CommandHandler_commandList(self):
304+
self._setup_updater('', messages=0)
305+
handler = CommandHandler(['foo', 'bar', 'spameggs'], self.telegramHandlerTest)
306+
self.updater.dispatcher.add_handler(handler)
307+
bot = self.updater.bot
308+
user = User(0, 'TestUser')
309+
queue = self.updater.start_polling(0.01)
310+
311+
message = Message(0, user, 0, None, text='/foo', bot=bot)
312+
queue.put(Update(0, message=message))
313+
sleep(.1)
314+
self.assertEqual(self.received_message, '/foo')
315+
316+
message.text = '/bar'
317+
queue.put(Update(1, message=message))
318+
sleep(.1)
319+
self.assertEqual(self.received_message, '/bar')
320+
321+
message.text = '/spameggs'
322+
queue.put(Update(2, message=message))
323+
sleep(.1)
324+
self.assertEqual(self.received_message, '/spameggs')
325+
326+
self.reset()
327+
message.text = '/not_in_list'
328+
queue.put(Update(3, message=message))
329+
sleep(.1)
330+
self.assertTrue(self.received_message is None)
331+
303332
def test_addRemoveStringRegexHandler(self):
304333
self._setup_updater('', messages=0)
305334
d = self.updater.dispatcher

0 commit comments

Comments
 (0)
X Tutup