zulip_botserver: Add option to set third party configs for bots.

This commit is contained in:
dkvasov 2018-05-16 17:55:31 +03:00 committed by Tim Abbott
parent f76287412c
commit a8665aaac8
4 changed files with 106 additions and 79 deletions

View file

@ -88,8 +88,14 @@ class StateHandler(object):
return key in self.state_ return key in self.state_
class ExternalBotHandler(object): class ExternalBotHandler(object):
def __init__(self, client, root_dir, bot_details, bot_config_file): def __init__(
# type: (Client, str, Dict[str, Any], str) -> None self,
client: Client,
root_dir: str,
bot_details: Dict[str, Any],
bot_config_file: Optional[str]=None,
bot_config_parser: Optional[configparser.ConfigParser]=None,
) -> None:
# Only expose a subset of our Client's functionality # Only expose a subset of our Client's functionality
try: try:
user_profile = client.get_profile() user_profile = client.get_profile()
@ -114,6 +120,7 @@ class ExternalBotHandler(object):
self._root_dir = root_dir self._root_dir = root_dir
self.bot_details = bot_details self.bot_details = bot_details
self.bot_config_file = bot_config_file self.bot_config_file = bot_config_file
self._bot_config_parser = bot_config_parser
self._storage = StateHandler(client) self._storage = StateHandler(client)
try: try:
self.user_id = user_profile['user_id'] self.user_id = user_profile['user_id']
@ -156,9 +163,10 @@ class ExternalBotHandler(object):
else: else:
self._rate_limit.show_error_and_exit() self._rate_limit.show_error_and_exit()
def get_config_info(self, bot_name, optional=False): def get_config_info(self, bot_name: str, optional: Optional[bool]=False) -> Dict[str, Any]:
# type: (str, Optional[bool]) -> Dict[str, Any] if self._bot_config_parser is not None:
config_parser = self._bot_config_parser
else:
if self.bot_config_file is None: if self.bot_config_file is None:
if optional: if optional:
return dict() return dict()
@ -189,18 +197,17 @@ class ExternalBotHandler(object):
# filename, we'll let an IOError happen here. Callers # filename, we'll let an IOError happen here. Callers
# like `run.py` will do the command line parsing and checking # like `run.py` will do the command line parsing and checking
# for the existence of the file. # for the existence of the file.
config = configparser.ConfigParser() config_parser = configparser.ConfigParser()
with open(self.bot_config_file) as conf: with open(self.bot_config_file) as conf:
try: try:
config.readfp(conf) # type: ignore # readfp->read_file in python 3, so not in stubs config_parser.read(conf)
except configparser.Error as e: except configparser.Error as e:
display_config_file_errors(str(e), self.bot_config_file) display_config_file_errors(str(e), self.bot_config_file)
sys.exit(1) sys.exit(1)
return dict(config.items(bot_name)) return dict(config_parser.items(bot_name))
def open(self, filepath): def open(self, filepath: str) -> IO[str]:
# type: (str) -> IO[str]
filepath = os.path.normpath(filepath) filepath = os.path.normpath(filepath)
abs_filepath = os.path.join(self._root_dir, filepath) abs_filepath = os.path.join(self._root_dir, filepath)
if abs_filepath.startswith(self._root_dir): if abs_filepath.startswith(self._root_dir):

View file

@ -16,7 +16,7 @@ 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,
bots_lib_module: Optional[Dict[str, Any]]=None, bots_lib_module: Optional[Dict[str, Any]]=None,
bot_handlers: Optional[Union[Dict[str, Any], BadRequest]]=None, bot_handlers: Optional[Dict[str, Any]]=None,
payload_url: str="/bots/helloworld", 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,
@ -26,9 +26,10 @@ class BotServerTestCase(TestCase):
bots_lib_modules = server.load_lib_modules() bots_lib_modules = server.load_lib_modules()
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:
bot_handlers = server.load_bot_handlers(bots_config, bots_lib_modules) bot_handlers = server.load_bot_handlers(bots_config)
if not isinstance(bot_handlers, BadRequest): message_handlers = server.init_message_handlers(bots_lib_modules, bot_handlers)
server.app.config["BOT_HANDLERS"] = bot_handlers 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(payload_url, data=json.dumps(message))

View file

@ -24,6 +24,12 @@ def parse_args() -> argparse.Namespace:
required=True, required=True,
help='Config file for the zulip bot server (flaskbotrc)' help='Config file for the zulip bot server (flaskbotrc)'
) )
parser.add_argument(
'--bot-config-file',
action='store',
default=None,
help='Config file for third-party bots'
)
parser.add_argument( parser.add_argument(
'--bot-name', '-b', '--bot-name', '-b',
action='store', action='store',

View file

@ -9,18 +9,14 @@ from typing import Any, Dict, Union, List, Optional
from werkzeug.exceptions import BadRequest from werkzeug.exceptions import BadRequest
from zulip import Client from zulip import Client
from zulip_bots.lib import ExternalBotHandler, StateHandler from zulip_bots.lib import ExternalBotHandler
from zulip_botserver.input_parameters import parse_args from zulip_botserver.input_parameters import parse_args
available_bots = [] # type: List[str] available_bots = [] # type: List[str]
def read_config_file(config_file_path: str, bot_name: Optional[str]=None) -> Dict[str, Dict[str, str]]: def read_config_file(config_file_path: str, bot_name: Optional[str]=None) -> Dict[str, Dict[str, str]]:
config_file_path = os.path.abspath(os.path.expanduser(config_file_path)) parser = parse_config_file(config_file_path)
if not os.path.isfile(config_file_path):
raise IOError("Could not read config file {}: File not found.".format(config_file_path))
parser = configparser.ConfigParser()
parser.read(config_file_path)
bots_config = {} bots_config = {}
for section in parser.sections(): for section in parser.sections():
@ -33,12 +29,20 @@ def read_config_file(config_file_path: str, bot_name: Optional[str]=None) -> Dic
bots_config[bot_name] = section_info bots_config[bot_name] = section_info
logging.warning("First bot name in the config list was changed to '{}'. " logging.warning("First bot name in the config list was changed to '{}'. "
"Other bots will be ignored".format(bot_name)) "Other bots will be ignored".format(bot_name))
break return bots_config
else:
bots_config[section] = section_info bots_config[section] = section_info
return bots_config return bots_config
def parse_config_file(config_file_path: str) -> configparser.ConfigParser:
config_file_path = os.path.abspath(os.path.expanduser(config_file_path))
if not os.path.isfile(config_file_path):
raise IOError("Could not read config file {}: File not found.".format(config_file_path))
parser = configparser.ConfigParser()
parser.read(config_file_path)
return parser
def load_lib_modules() -> Dict[str, Any]: def load_lib_modules() -> Dict[str, Any]:
bots_lib_module = {} bots_lib_module = {}
for bot in available_bots: for bot in available_bots:
@ -56,36 +60,43 @@ def load_lib_modules() -> Dict[str, Any]:
def load_bot_handlers( def load_bot_handlers(
bots_config: Dict[str, Dict[str, str]], bots_config: Dict[str, Dict[str, str]],
bots_lib_module: Dict[str, Any], bot_config_file: Optional[str]=None
) -> Union[Dict[str, ExternalBotHandler], BadRequest]: ) -> Dict[str, ExternalBotHandler]:
bot_handlers = {} bot_handlers = {}
third_party_bot_conf = parse_config_file(bot_config_file) if bot_config_file is not None else None
for bot in available_bots: for bot in available_bots:
client = Client(email=bots_config[bot]["email"], client = Client(email=bots_config[bot]["email"],
api_key=bots_config[bot]["key"], api_key=bots_config[bot]["key"],
site=bots_config[bot]["site"]) site=bots_config[bot]["site"])
try:
bot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bots', bot) bot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bots', bot)
# TODO: Figure out how to pass in third party config info.
bot_handler = ExternalBotHandler( bot_handler = ExternalBotHandler(
client, client,
bot_dir, bot_dir,
bot_details={}, bot_details={},
bot_config_file=None bot_config_parser=third_party_bot_conf
) )
bot_handlers[bot] = bot_handler
lib_module = bots_lib_module[bot] bot_handlers[bot] = bot_handler
message_handler = lib_module.handler_class() return bot_handlers
def init_message_handlers(
bots_lib_modules: Dict[str, Any],
bot_handlers: Dict[str, ExternalBotHandler],
) -> Dict[str, Any]:
message_handlers = {}
for bot in available_bots:
bot_lib_module = bots_lib_modules[bot]
bot_handler = bot_handlers[bot]
message_handler = bot_lib_module.handler_class()
if hasattr(message_handler, 'validate_config'): if hasattr(message_handler, 'validate_config'):
config_data = bot_handlers[bot].get_config_info(bot) config_data = bot_handler.get_config_info(bot)
lib_module.handler_class.validate_config(config_data) bot_lib_module.handler_class.validate_config(config_data)
if hasattr(message_handler, 'initialize'): if hasattr(message_handler, 'initialize'):
message_handler.initialize(bot_handler=bot_handler) message_handler.initialize(bot_handler=bot_handler)
except SystemExit: message_handlers[bot] = message_handler
return BadRequest("Cannot fetch user profile for bot {}, make sure you have set up the flaskbotrc " return message_handlers
"file correctly.".format(bot))
return bot_handlers
app = Flask(__name__) app = Flask(__name__)
@ -94,15 +105,15 @@ app = Flask(__name__)
@app.route('/bots/<bot>', methods=['POST']) @app.route('/bots/<bot>', methods=['POST'])
def handle_bot(bot: str) -> Union[str, BadRequest]: def handle_bot(bot: str) -> Union[str, BadRequest]:
lib_module = app.config.get("BOTS_LIB_MODULES", {}).get(bot) 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: if lib_module is None:
return BadRequest("Can't find the configuration or Bot Handler code for bot {}. " return BadRequest("Can't find the configuration or Bot Handler code for bot {}. "
"Make sure that the `zulip_bots` package is installed, and " "Make sure that the `zulip_bots` package is installed, and "
"that your flaskbotrc is set up correctly".format(bot)) "that your flaskbotrc is set up correctly".format(bot))
message_handler = lib_module.handler_class()
event = request.get_json(force=True) event = request.get_json(force=True)
message_handler.handle_message(message=event["message"], message_handler.handle_message(message=event["message"], bot_handler=bot_handler)
bot_handler=app.config["BOT_HANDLERS"].get(bot))
return json.dumps("") return json.dumps("")
@ -112,9 +123,11 @@ def main() -> None:
global available_bots global available_bots
available_bots = list(bots_config.keys()) available_bots = list(bots_config.keys())
bots_lib_modules = load_lib_modules() bots_lib_modules = load_lib_modules()
bot_handlers = load_bot_handlers(bots_config, bots_lib_modules) bot_handlers = load_bot_handlers(bots_config, options.bot_config_file)
message_handlers = init_message_handlers(bots_lib_modules, bot_handlers)
app.config["BOTS_LIB_MODULES"] = bots_lib_modules app.config["BOTS_LIB_MODULES"] = bots_lib_modules
app.config["BOT_HANDLERS"] = bot_handlers app.config["BOT_HANDLERS"] = bot_handlers
app.config["MESSAGE_HANDLERS"] = message_handlers
app.run(host=options.hostname, port=int(options.port), debug=True) app.run(host=options.hostname, port=int(options.port), debug=True)
if __name__ == '__main__': if __name__ == '__main__':