|
12 | 12 | from mock import MagicMock, patch
|
13 | 13 | from zulip_bots.lib import StateHandler
|
14 | 14 | from zulip_bots.lib import ExternalBotHandler
|
| 15 | +from zulip_bots.lib import get_bot_details, setup_default_commands, updated_default_commands |
15 | 16 | from zulip_bots.provision import provision_bot
|
16 | 17 | from zulip_bots.run import import_module_from_source
|
17 | 18 |
|
@@ -66,18 +67,32 @@ def main():
|
66 | 67 | message = { 'content': args. message, 'sender_email': '[email protected]'}
|
67 | 68 | message_handler = lib_module.handler_class()
|
68 | 69 |
|
| 70 | + bot_details = get_bot_details(message_handler, args.bot) |
| 71 | + |
| 72 | + default_commands = setup_default_commands(bot_details, message_handler) |
| 73 | + updated_defaults = updated_default_commands(default_commands, bot_details) |
| 74 | + |
69 | 75 | with patch('zulip.Client') as mock_client:
|
70 | 76 | mock_bot_handler = ExternalBotHandler(mock_client, bot_dir)
|
71 | 77 | mock_bot_handler.send_reply = MagicMock()
|
72 | 78 | mock_bot_handler.send_message = MagicMock()
|
73 | 79 | mock_bot_handler.update_message = MagicMock()
|
74 | 80 | if hasattr(message_handler, 'initialize') and callable(message_handler.initialize):
|
75 | 81 | message_handler.initialize(mock_bot_handler)
|
76 |
| - message_handler.handle_message( |
77 |
| - message=message, |
78 |
| - bot_handler=mock_bot_handler, |
79 |
| - state_handler=StateHandler() |
80 |
| - ) |
| 82 | + |
| 83 | + # Check if command handled in library first; if not, then delegate to bot |
| 84 | + handled = False |
| 85 | + for command in updated_defaults: |
| 86 | + if command == message['content']: |
| 87 | + mock_bot_handler.send_reply(message, |
| 88 | + updated_defaults[command]['action']()) |
| 89 | + handled = True |
| 90 | + if not handled: |
| 91 | + message_handler.handle_message( |
| 92 | + message=message, |
| 93 | + bot_handler=mock_bot_handler, |
| 94 | + state_handler=StateHandler() |
| 95 | + ) |
81 | 96 | print("On sending {} bot the message \"{}\"".format(bot_name, args.message))
|
82 | 97 | # send_reply and send_message have slightly arguments; the
|
83 | 98 | # following takes that into account.
|
|
0 commit comments