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/<botname> for each bot. Now, instead, the Botserver determines which bot to run by the section header of the bot in the flaskbotrc.
This commit is contained in:
parent
6249df0ad7
commit
6a8cb2965c
|
@ -17,12 +17,12 @@ class BotServerTestCase(TestCase):
|
||||||
available_bots: Optional[List[str]]=None,
|
available_bots: Optional[List[str]]=None,
|
||||||
bots_config: Optional[Dict[str, Dict[str, str]]]=None,
|
bots_config: Optional[Dict[str, Dict[str, str]]]=None,
|
||||||
bot_handlers: Optional[Dict[str, Any]]=None,
|
bot_handlers: Optional[Dict[str, Any]]=None,
|
||||||
payload_url: str="/bots/helloworld",
|
|
||||||
message: Optional[Dict[str, Any]]=dict(message={'key': "test message"}),
|
message: Optional[Dict[str, Any]]=dict(message={'key': "test message"}),
|
||||||
check_success: bool=False,
|
check_success: bool=False,
|
||||||
third_party_bot_conf: Optional[configparser.ConfigParser]=None,
|
third_party_bot_conf: Optional[configparser.ConfigParser]=None,
|
||||||
) -> None:
|
) -> None:
|
||||||
if available_bots is not None and bots_config is not 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)
|
bots_lib_modules = server.load_lib_modules(available_bots)
|
||||||
server.app.config["BOTS_LIB_MODULES"] = bots_lib_modules
|
server.app.config["BOTS_LIB_MODULES"] = bots_lib_modules
|
||||||
if bot_handlers is None:
|
if bot_handlers is None:
|
||||||
|
@ -31,7 +31,7 @@ class BotServerTestCase(TestCase):
|
||||||
server.app.config["BOT_HANDLERS"] = bot_handlers
|
server.app.config["BOT_HANDLERS"] = bot_handlers
|
||||||
server.app.config["MESSAGE_HANDLERS"] = message_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:
|
if check_success:
|
||||||
assert 200 <= response.status_code < 300
|
assert 200 <= response.status_code < 300
|
||||||
|
|
|
@ -31,6 +31,8 @@ class BotServerTests(BotServerTestCase):
|
||||||
}
|
}
|
||||||
self.assert_bot_server_response(available_bots=available_bots,
|
self.assert_bot_server_response(available_bots=available_bots,
|
||||||
bots_config=bots_config,
|
bots_config=bots_config,
|
||||||
|
message=dict(message={'content': "test message"},
|
||||||
|
bot_email='helloworld-bot@zulip.com'),
|
||||||
check_success=True)
|
check_success=True)
|
||||||
|
|
||||||
@mock.patch('zulip_bots.lib.ExternalBotHandler')
|
@mock.patch('zulip_bots.lib.ExternalBotHandler')
|
||||||
|
@ -49,12 +51,15 @@ class BotServerTests(BotServerTestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.assert_bot_server_response(available_bots=available_bots,
|
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,
|
bots_config=bots_config,
|
||||||
check_success=True)
|
check_success=True)
|
||||||
|
|
||||||
def test_bot_module_not_exists(self) -> None:
|
def test_bot_module_not_exists(self) -> None:
|
||||||
self.assert_bot_server_response(available_bots=[],
|
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)
|
check_success=False)
|
||||||
|
|
||||||
@mock.patch('logging.error')
|
@mock.patch('logging.error')
|
||||||
|
|
|
@ -98,25 +98,36 @@ def init_message_handlers(
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
bots_config = {} # type: Dict[str, Dict[str, str]]
|
||||||
|
|
||||||
|
|
||||||
@app.route('/bots/<bot>', methods=['POST'])
|
@app.route('/', methods=['POST'])
|
||||||
def handle_bot(bot: str) -> Union[str, BadRequest]:
|
def handle_bot() -> 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))
|
|
||||||
|
|
||||||
event = request.get_json(force=True)
|
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)
|
message_handler.handle_message(message=event["message"], bot_handler=bot_handler)
|
||||||
return json.dumps("")
|
return json.dumps("")
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
options = parse_args()
|
options = parse_args()
|
||||||
|
global bots_config
|
||||||
bots_config = read_config_file(options.config_file, options.bot_name)
|
bots_config = read_config_file(options.config_file, options.bot_name)
|
||||||
available_bots = list(bots_config.keys())
|
available_bots = list(bots_config.keys())
|
||||||
bots_lib_modules = load_lib_modules(available_bots)
|
bots_lib_modules = load_lib_modules(available_bots)
|
||||||
|
|
Loading…
Reference in a new issue