2018-05-15 15:23:10 -04:00
|
|
|
import configparser
|
2018-05-16 10:41:22 -04:00
|
|
|
import logging
|
2018-05-15 15:31:49 -04:00
|
|
|
import json
|
2017-07-25 10:04:38 -04:00
|
|
|
import os
|
2018-05-14 15:08:03 -04:00
|
|
|
|
2017-06-13 14:39:36 -04:00
|
|
|
from flask import Flask, request
|
|
|
|
from importlib import import_module
|
2018-05-16 10:41:22 -04:00
|
|
|
from typing import Any, Dict, Union, List, Optional
|
2017-05-21 03:48:09 -04:00
|
|
|
from werkzeug.exceptions import BadRequest
|
|
|
|
|
|
|
|
from zulip import Client
|
2018-05-16 10:55:31 -04:00
|
|
|
from zulip_bots.lib import ExternalBotHandler
|
2018-05-16 10:41:22 -04:00
|
|
|
from zulip_botserver.input_parameters import parse_args
|
2017-05-21 03:48:09 -04:00
|
|
|
|
|
|
|
available_bots = [] # type: List[str]
|
2018-05-15 10:55:58 -04:00
|
|
|
|
2017-05-21 03:48:09 -04:00
|
|
|
|
2018-05-16 10:41:22 -04:00
|
|
|
def read_config_file(config_file_path: str, bot_name: Optional[str]=None) -> Dict[str, Dict[str, str]]:
|
2018-05-16 10:55:31 -04:00
|
|
|
parser = parse_config_file(config_file_path)
|
2017-05-21 03:48:09 -04:00
|
|
|
|
2018-05-16 13:16:07 -04:00
|
|
|
bots_config = {} # type: Dict[str, Dict[str, str]]
|
2017-05-21 03:48:09 -04:00
|
|
|
for section in parser.sections():
|
2018-05-16 10:41:22 -04:00
|
|
|
section_info = {
|
2017-05-21 03:48:09 -04:00
|
|
|
"email": parser.get(section, 'email'),
|
|
|
|
"key": parser.get(section, 'key'),
|
|
|
|
"site": parser.get(section, 'site'),
|
|
|
|
}
|
2018-05-16 10:41:22 -04:00
|
|
|
if bot_name is not None:
|
2018-05-16 13:16:07 -04:00
|
|
|
logging.warning("Single bot mode is enabled")
|
|
|
|
if bots_config:
|
|
|
|
logging.warning("'{}' bot will be ignored".format(section))
|
|
|
|
else:
|
|
|
|
bots_config[bot_name] = section_info
|
|
|
|
logging.warning(
|
|
|
|
"First bot name in the config list was changed from '{}' to '{}'".format(section, bot_name)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
bots_config[section] = section_info
|
2018-05-15 15:27:55 -04:00
|
|
|
return bots_config
|
2017-05-21 03:48:09 -04:00
|
|
|
|
2018-05-15 10:55:58 -04:00
|
|
|
|
2018-05-16 10:55:31 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-05-15 10:55:58 -04:00
|
|
|
def load_lib_modules() -> Dict[str, Any]:
|
|
|
|
bots_lib_module = {}
|
2017-05-21 03:48:09 -04:00
|
|
|
for bot in available_bots:
|
2017-06-13 13:37:13 -04:00
|
|
|
try:
|
2017-07-16 05:59:21 -04:00
|
|
|
module_name = 'zulip_bots.bots.{bot}.{bot}'.format(bot=bot)
|
|
|
|
lib_module = import_module(module_name)
|
|
|
|
bots_lib_module[bot] = lib_module
|
2017-06-13 14:39:36 -04:00
|
|
|
except ImportError:
|
2018-05-15 15:23:40 -04:00
|
|
|
raise ImportError(
|
2018-05-15 10:55:58 -04:00
|
|
|
"\nImport Error: Bot \"{}\" doesn't exists. "
|
|
|
|
"Please make sure you have set up the flaskbotrc file correctly.\n".format(bot)
|
2018-05-15 15:23:40 -04:00
|
|
|
)
|
2018-05-15 10:55:58 -04:00
|
|
|
return bots_lib_module
|
|
|
|
|
2017-07-16 05:59:21 -04:00
|
|
|
|
2018-05-15 15:27:55 -04:00
|
|
|
def load_bot_handlers(
|
|
|
|
bots_config: Dict[str, Dict[str, str]],
|
2018-05-16 12:33:54 -04:00
|
|
|
third_party_bot_conf: Optional[configparser.ConfigParser]=None,
|
2018-05-16 10:55:31 -04:00
|
|
|
) -> Dict[str, ExternalBotHandler]:
|
2018-05-15 10:55:58 -04:00
|
|
|
bot_handlers = {}
|
2017-11-07 06:57:47 -05:00
|
|
|
for bot in available_bots:
|
|
|
|
client = Client(email=bots_config[bot]["email"],
|
|
|
|
api_key=bots_config[bot]["key"],
|
|
|
|
site=bots_config[bot]["site"])
|
2018-05-16 10:55:31 -04:00
|
|
|
bot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bots', bot)
|
|
|
|
bot_handler = ExternalBotHandler(
|
|
|
|
client,
|
|
|
|
bot_dir,
|
|
|
|
bot_details={},
|
|
|
|
bot_config_parser=third_party_bot_conf
|
|
|
|
)
|
|
|
|
|
|
|
|
bot_handlers[bot] = bot_handler
|
2018-05-15 10:55:58 -04:00
|
|
|
return bot_handlers
|
2017-11-07 06:57:47 -05:00
|
|
|
|
2017-05-21 03:48:09 -04:00
|
|
|
|
2018-05-16 10:55:31 -04:00
|
|
|
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'):
|
|
|
|
config_data = bot_handler.get_config_info(bot)
|
|
|
|
bot_lib_module.handler_class.validate_config(config_data)
|
|
|
|
|
|
|
|
if hasattr(message_handler, 'initialize'):
|
|
|
|
message_handler.initialize(bot_handler=bot_handler)
|
|
|
|
message_handlers[bot] = message_handler
|
|
|
|
return message_handlers
|
|
|
|
|
|
|
|
|
2017-05-21 03:48:09 -04:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2018-05-15 10:55:58 -04:00
|
|
|
|
2017-05-21 03:48:09 -04:00
|
|
|
@app.route('/bots/<bot>', methods=['POST'])
|
2018-05-14 15:45:54 -04:00
|
|
|
def handle_bot(bot: str) -> Union[str, BadRequest]:
|
2018-05-15 10:55:58 -04:00
|
|
|
lib_module = app.config.get("BOTS_LIB_MODULES", {}).get(bot)
|
2018-05-16 10:55:31 -04:00
|
|
|
bot_handler = app.config.get("BOT_HANDLERS", {}).get(bot)
|
|
|
|
message_handler = app.config.get("MESSAGE_HANDLERS", {}).get(bot)
|
2017-07-16 05:59:21 -04:00
|
|
|
if lib_module is None:
|
|
|
|
return BadRequest("Can't find the configuration or Bot Handler code for bot {}. "
|
2017-08-28 10:01:05 -04:00
|
|
|
"Make sure that the `zulip_bots` package is installed, and "
|
|
|
|
"that your flaskbotrc is set up correctly".format(bot))
|
2017-05-21 03:48:09 -04:00
|
|
|
|
2017-08-15 06:01:47 -04:00
|
|
|
event = request.get_json(force=True)
|
2018-05-16 10:55:31 -04:00
|
|
|
message_handler.handle_message(message=event["message"], bot_handler=bot_handler)
|
2017-07-16 05:59:21 -04:00
|
|
|
return json.dumps("")
|
2017-05-21 03:48:09 -04:00
|
|
|
|
2018-05-15 10:55:58 -04:00
|
|
|
|
2018-05-14 15:45:54 -04:00
|
|
|
def main() -> None:
|
2017-07-26 22:01:35 -04:00
|
|
|
options = parse_args()
|
2018-05-16 10:41:22 -04:00
|
|
|
bots_config = read_config_file(options.config_file, options.bot_name)
|
2017-06-13 14:39:36 -04:00
|
|
|
global available_bots
|
2017-05-21 03:48:09 -04:00
|
|
|
available_bots = list(bots_config.keys())
|
2018-05-15 10:55:58 -04:00
|
|
|
bots_lib_modules = load_lib_modules()
|
2018-05-16 12:33:54 -04:00
|
|
|
third_party_bot_conf = parse_config_file(options.bot_config_file) if options.bot_config_file is not None else None
|
|
|
|
bot_handlers = load_bot_handlers(bots_config, third_party_bot_conf)
|
2018-05-16 10:55:31 -04:00
|
|
|
message_handlers = init_message_handlers(bots_lib_modules, bot_handlers)
|
2018-05-15 10:55:58 -04:00
|
|
|
app.config["BOTS_LIB_MODULES"] = bots_lib_modules
|
|
|
|
app.config["BOT_HANDLERS"] = bot_handlers
|
2018-05-16 10:55:31 -04:00
|
|
|
app.config["MESSAGE_HANDLERS"] = message_handlers
|
2017-07-16 05:59:21 -04:00
|
|
|
app.run(host=options.hostname, port=int(options.port), debug=True)
|
2017-06-13 14:39:36 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|