mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-16 12:25:45 +01:00
Fix commandhandler (#515)
* stripping token of whitespaces before starting bot * Line feed * CommandHandler checks if message is for this bot * CommandHandler checks if message is for this bot - Added tests * Fixed tests in test_conversationHandler to work with new commandhandler structure * type in conversationhandler test
This commit is contained in:
parent
264b9bd08c
commit
78094b796d
3 changed files with 36 additions and 27 deletions
|
@ -83,8 +83,14 @@ class CommandHandler(Handler):
|
|||
and (update.message or update.edited_message and self.allow_edited)):
|
||||
message = update.message or update.edited_message
|
||||
|
||||
return (message.text and message.text.startswith('/')
|
||||
and message.text[1:].split(' ')[0].split('@')[0] == self.command)
|
||||
if message.text:
|
||||
command = message.text[1:].split(' ')[0].split('@')
|
||||
command.append(
|
||||
update.message.bot.username) # in case the command was send without a username
|
||||
return (message.text.startswith('/') and command[0] == self.command
|
||||
and command[1] == update.message.bot.username)
|
||||
else:
|
||||
return False
|
||||
|
||||
else:
|
||||
return False
|
||||
|
|
|
@ -80,8 +80,8 @@ class ConversationHandlerTest(BaseTest, unittest.TestCase):
|
|||
self.fallbacks = [CommandHandler('eat', self.start)]
|
||||
|
||||
def _setup_updater(self, *args, **kwargs):
|
||||
bot = MockBot(*args, **kwargs)
|
||||
self.updater = Updater(workers=2, bot=bot)
|
||||
self.bot = MockBot(*args, **kwargs)
|
||||
self.updater = Updater(workers=2, bot=self.bot)
|
||||
|
||||
def tearDown(self):
|
||||
if self.updater is not None:
|
||||
|
@ -137,32 +137,32 @@ class ConversationHandlerTest(BaseTest, unittest.TestCase):
|
|||
queue = self.updater.start_polling(0.01)
|
||||
|
||||
# User one, starts the state machine.
|
||||
message = Message(0, user, None, None, text="/start")
|
||||
message = Message(0, user, None, None, text="/start", bot=self.bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
self.assertTrue(self.current_state[user.id] == self.THIRSTY)
|
||||
|
||||
# The user is thirsty and wants to brew coffee.
|
||||
message = Message(0, user, None, None, text="/brew")
|
||||
message = Message(0, user, None, None, text="/brew", bot=self.bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
self.assertTrue(self.current_state[user.id] == self.BREWING)
|
||||
|
||||
# Lets see if an invalid command makes sure, no state is changed.
|
||||
message = Message(0, user, None, None, text="/nothing")
|
||||
message = Message(0, user, None, None, text="/nothing", bot=self.bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
self.assertTrue(self.current_state[user.id] == self.BREWING)
|
||||
|
||||
# Lets see if the state machine still works by pouring coffee.
|
||||
message = Message(0, user, None, None, text="/pourCoffee")
|
||||
message = Message(0, user, None, None, text="/pourCoffee", bot=self.bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
self.assertTrue(self.current_state[user.id] == self.DRINKING)
|
||||
|
||||
# Let's now verify that for another user, who did not start yet,
|
||||
# the state has not been changed.
|
||||
message = Message(0, second_user, None, None, text="/brew")
|
||||
message = Message(0, second_user, None, None, text="/brew", bot=self.bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
self.assertRaises(KeyError, self._get_state, user_id=second_user.id)
|
||||
|
@ -197,13 +197,13 @@ class ConversationHandlerTest(BaseTest, unittest.TestCase):
|
|||
|
||||
# User starts the state machine with an async function that immediately ends the
|
||||
# conversation. Async results are resolved when the users state is queried next time.
|
||||
message = Message(0, user, None, None, text="/start")
|
||||
message = Message(0, user, None, None, text="/start", bot=self.bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
# Assert that the Promise has been accepted as the new state
|
||||
self.assertEquals(len(handler.conversations), 1)
|
||||
|
||||
message = Message(0, user, None, None, text="resolve promise pls")
|
||||
message = Message(0, user, None, None, text="resolve promise pls", bot=self.bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
# Assert that the Promise has been resolved and the conversation ended.
|
||||
|
|
|
@ -231,31 +231,33 @@ class UpdaterTest(BaseTest, unittest.TestCase):
|
|||
self.assertTrue(None is self.received_message)
|
||||
|
||||
def test_addRemoveTelegramCommandHandler(self):
|
||||
self._setup_updater('/test')
|
||||
self._setup_updater('', messages=0)
|
||||
d = self.updater.dispatcher
|
||||
handler = CommandHandler('test', self.telegramHandlerTest)
|
||||
self.updater.dispatcher.add_handler(handler)
|
||||
self.updater.start_polling(0.01)
|
||||
user = User(first_name="singelton", id=404)
|
||||
bot = self.updater.bot
|
||||
queue = self.updater.start_polling(0.01)
|
||||
|
||||
# regular use
|
||||
message = Message(0, user, None, None, text="/test", bot=bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
self.assertEqual(self.received_message, '/test')
|
||||
|
||||
# Remove handler
|
||||
d.remove_handler(handler)
|
||||
self.reset()
|
||||
# assigned use
|
||||
message = Message(0, user, None, None, text="/test@MockBot", bot=bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
self.assertEqual(self.received_message, '/test@MockBot')
|
||||
|
||||
self.updater.bot.send_messages = 1
|
||||
# directed at other bot
|
||||
self.reset()
|
||||
message = Message(0, user, None, None, text="/test@OtherBot", bot=bot)
|
||||
queue.put(Update(update_id=0, message=message))
|
||||
sleep(.1)
|
||||
self.assertTrue(None is self.received_message)
|
||||
|
||||
def test_editedCommandHandler(self):
|
||||
self._setup_updater('/test', edited=True)
|
||||
d = self.updater.dispatcher
|
||||
handler = CommandHandler('test', self.telegramHandlerEditedTest, allow_edited=True)
|
||||
d.addHandler(handler)
|
||||
self.updater.start_polling(0.01)
|
||||
sleep(.1)
|
||||
self.assertEqual(self.received_message, '/test')
|
||||
|
||||
# Remove handler
|
||||
d.removeHandler(handler)
|
||||
handler = CommandHandler('test', self.telegramHandlerEditedTest, allow_edited=False)
|
||||
|
@ -788,9 +790,10 @@ class MockBot(object):
|
|||
self.bootstrap_attempts = 0
|
||||
self.bootstrap_err = bootstrap_err
|
||||
self.edited = edited
|
||||
self.username = "MockBot"
|
||||
|
||||
def mockUpdate(self, text):
|
||||
message = Message(0, User(0, 'Testuser'), None, Chat(0, Chat.GROUP))
|
||||
message = Message(0, User(0, 'Testuser'), None, Chat(0, Chat.GROUP), bot=self)
|
||||
message.text = text
|
||||
update = Update(0)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue