From 6a8cb2965c325ff1a6ed46af7061adf1c04f26a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Tue, 22 May 2018 13:46:40 +0200 Subject: [PATCH] zulip_botserver: Handle all requests from the root / endpoint. Previously, the Botserver determined which bot to run by dispatching on a unique URL endpoint /bots/ for each bot. Now, instead, the Botserver determines which bot to run by the section header of the bot in the flaskbotrc. --- zulip_botserver/tests/server_test_lib.py | 4 +-- zulip_botserver/tests/test_server.py | 7 ++++- zulip_botserver/zulip_botserver/server.py | 31 +++++++++++++++-------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/zulip_botserver/tests/server_test_lib.py b/zulip_botserver/tests/server_test_lib.py index 8d84a05..bbe2eb4 100644 --- a/zulip_botserver/tests/server_test_lib.py +++ b/zulip_botserver/tests/server_test_lib.py @@ -17,12 +17,12 @@ class BotServerTestCase(TestCase): available_bots: Optional[List[str]]=None, bots_config: Optional[Dict[str, Dict[str, str]]]=None, bot_handlers: Optional[Dict[str, Any]]=None, - payload_url: str="/bots/helloworld", message: Optional[Dict[str, Any]]=dict(message={'key': "test message"}), check_success: bool=False, third_party_bot_conf: Optional[configparser.ConfigParser]=None, ) -> None: if available_bots is not None and bots_config is not None: + server.bots_config = bots_config bots_lib_modules = server.load_lib_modules(available_bots) server.app.config["BOTS_LIB_MODULES"] = bots_lib_modules if bot_handlers is None: @@ -31,7 +31,7 @@ class BotServerTestCase(TestCase): server.app.config["BOT_HANDLERS"] = bot_handlers server.app.config["MESSAGE_HANDLERS"] = message_handlers - response = self.app.post(payload_url, data=json.dumps(message)) + response = self.app.post(data=json.dumps(message)) if check_success: assert 200 <= response.status_code < 300 diff --git a/zulip_botserver/tests/test_server.py b/zulip_botserver/tests/test_server.py index 0513747..7074d06 100644 --- a/zulip_botserver/tests/test_server.py +++ b/zulip_botserver/tests/test_server.py @@ -31,6 +31,8 @@ class BotServerTests(BotServerTestCase): } self.assert_bot_server_response(available_bots=available_bots, bots_config=bots_config, + message=dict(message={'content': "test message"}, + bot_email='helloworld-bot@zulip.com'), check_success=True) @mock.patch('zulip_bots.lib.ExternalBotHandler') @@ -49,12 +51,15 @@ class BotServerTests(BotServerTestCase): } } self.assert_bot_server_response(available_bots=available_bots, + message=dict(message={'content': "test message"}, + bot_email='helloworld-bot@zulip.com'), bots_config=bots_config, check_success=True) def test_bot_module_not_exists(self) -> None: self.assert_bot_server_response(available_bots=[], - payload_url="/bots/not_supported_bot", + message=dict(message={'content': "test message"}, + bot_email='nonexistent-bot@zulip.com'), check_success=False) @mock.patch('logging.error') diff --git a/zulip_botserver/zulip_botserver/server.py b/zulip_botserver/zulip_botserver/server.py index 14d9f8b..d192393 100644 --- a/zulip_botserver/zulip_botserver/server.py +++ b/zulip_botserver/zulip_botserver/server.py @@ -98,25 +98,36 @@ def init_message_handlers( app = Flask(__name__) +bots_config = {} # type: Dict[str, Dict[str, str]] -@app.route('/bots/', methods=['POST']) -def handle_bot(bot: str) -> Union[str, BadRequest]: - lib_module = app.config.get("BOTS_LIB_MODULES", {}).get(bot) - bot_handler = app.config.get("BOT_HANDLERS", {}).get(bot) - message_handler = app.config.get("MESSAGE_HANDLERS", {}).get(bot) - if lib_module is None: - return BadRequest("Can't find the configuration or Bot Handler code for bot {}. " - "Make sure that the `zulip_bots` package is installed, and " - "that your flaskbotrc is set up correctly".format(bot)) - +@app.route('/', methods=['POST']) +def handle_bot() -> Union[str, BadRequest]: event = request.get_json(force=True) + bot = None + for bot_name, config in bots_config.items(): + if config['email'] == event['bot_email']: + bot = bot_name + if bot is None: + return BadRequest("Cannot find a bot with email {} in the bot server " + "configuration file. Do the emails in your flaskbotrc " + "match the bot emails on the server?".format(event['bot_email'])) + else: + lib_module = app.config.get("BOTS_LIB_MODULES", {}).get(bot) + bot_handler = app.config.get("BOT_HANDLERS", {}).get(bot) + message_handler = app.config.get("MESSAGE_HANDLERS", {}).get(bot) + if lib_module is None: + return BadRequest("Can't find the configuration or Bot Handler code for bot {}. " + "Make sure that the `zulip_bots` package is installed, and " + "that your flaskbotrc is set up correctly".format(bot)) + message_handler.handle_message(message=event["message"], bot_handler=bot_handler) return json.dumps("") def main() -> None: options = parse_args() + global bots_config bots_config = read_config_file(options.config_file, options.bot_name) available_bots = list(bots_config.keys()) bots_lib_modules = load_lib_modules(available_bots)