2017-05-21 03:48:09 -04:00
|
|
|
from __future__ import absolute_import
|
2017-06-13 13:37:13 -04:00
|
|
|
from __future__ import print_function
|
2017-06-13 14:39:36 -04:00
|
|
|
|
2017-07-25 10:04:38 -04:00
|
|
|
import os
|
2017-05-21 03:48:09 -04:00
|
|
|
import sys
|
|
|
|
import json
|
2017-07-26 22:01:35 -04:00
|
|
|
import argparse
|
2017-06-13 14:39:36 -04:00
|
|
|
from flask import Flask, request
|
|
|
|
from importlib import import_module
|
|
|
|
from typing import Any, Dict, Mapping, Union, List, Tuple
|
2017-05-21 03:48:09 -04:00
|
|
|
from werkzeug.exceptions import BadRequest
|
|
|
|
from six.moves.configparser import SafeConfigParser
|
|
|
|
|
|
|
|
from zulip import Client
|
2017-07-18 00:01:54 -04:00
|
|
|
from zulip_bots.lib import ExternalBotHandler, StateHandler
|
2017-05-21 03:48:09 -04:00
|
|
|
|
|
|
|
bots_config = {} # type: Dict[str, Mapping[str, str]]
|
|
|
|
available_bots = [] # type: List[str]
|
|
|
|
bots_lib_module = {} # type: Dict[str, Any]
|
|
|
|
|
|
|
|
def read_config_file(config_file_path):
|
|
|
|
# type: (str) -> None
|
|
|
|
parser = SafeConfigParser()
|
|
|
|
parser.read(config_file_path)
|
|
|
|
|
|
|
|
for section in parser.sections():
|
|
|
|
bots_config[section] = {
|
|
|
|
"email": parser.get(section, 'email'),
|
|
|
|
"key": parser.get(section, 'key'),
|
|
|
|
"site": parser.get(section, 'site'),
|
|
|
|
}
|
|
|
|
|
|
|
|
def load_lib_modules():
|
|
|
|
# type: () -> None
|
|
|
|
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:
|
|
|
|
print("\n Import Error: Bot \"{}\" doesn't exists. Please make sure you have set up the flaskbotrc "
|
2017-06-13 13:37:13 -04:00
|
|
|
"file correctly.\n".format(bot))
|
2017-07-16 05:59:21 -04:00
|
|
|
|
|
|
|
def get_bot_lib_module(bot):
|
|
|
|
# type: (str) -> Any
|
|
|
|
if bot in bots_lib_module.keys():
|
|
|
|
return bots_lib_module[bot]
|
|
|
|
return None
|
2017-05-21 03:48:09 -04:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
@app.route('/bots/<bot>', methods=['POST'])
|
|
|
|
def handle_bot(bot):
|
|
|
|
# type: (str) -> Union[str, BadRequest]
|
2017-07-16 05:59:21 -04:00
|
|
|
lib_module = get_bot_lib_module(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!".format(bot))
|
|
|
|
|
2017-05-21 03:48:09 -04:00
|
|
|
client = Client(email=bots_config[bot]["email"],
|
|
|
|
api_key=bots_config[bot]["key"],
|
|
|
|
site=bots_config[bot]["site"])
|
2017-06-13 13:37:13 -04:00
|
|
|
try:
|
2017-07-25 10:04:38 -04:00
|
|
|
bot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'bots', bot)
|
|
|
|
restricted_client = ExternalBotHandler(client, bot_dir)
|
2017-06-13 13:37:13 -04:00
|
|
|
except SystemExit:
|
|
|
|
return BadRequest("Cannot fetch user profile for bot {}, make sure you have set up the flaskbotrc "
|
|
|
|
"file correctly.".format(bot))
|
2017-07-16 05:59:21 -04:00
|
|
|
|
|
|
|
message_handler = lib_module.handler_class()
|
2017-05-21 03:48:09 -04:00
|
|
|
|
|
|
|
# TODO: Handle stateful bots properly.
|
|
|
|
state_handler = StateHandler()
|
|
|
|
|
2017-08-15 06:01:47 -04:00
|
|
|
event = request.get_json(force=True)
|
2017-05-21 03:48:09 -04:00
|
|
|
message_handler.handle_message(message=event["message"],
|
2017-06-13 14:39:36 -04:00
|
|
|
bot_handler=restricted_client,
|
2017-05-21 03:48:09 -04:00
|
|
|
state_handler=state_handler)
|
2017-07-16 05:59:21 -04:00
|
|
|
return json.dumps("")
|
2017-05-21 03:48:09 -04:00
|
|
|
|
2017-06-13 14:39:36 -04:00
|
|
|
def parse_args():
|
2017-07-26 22:01:35 -04:00
|
|
|
# type: () -> Tuple[Any]
|
2017-06-13 13:39:19 -04:00
|
|
|
usage = '''
|
2017-06-13 14:39:36 -04:00
|
|
|
zulip-bot-server --config-file <path to flaskbotrc> --hostname <address> --port <port>
|
|
|
|
Example: zulip-bot-server --config-file ~/flaskbotrc
|
|
|
|
(This program loads the bot configurations from the
|
|
|
|
config file (flaskbotrc here) and loads the bot modules.
|
|
|
|
It then starts the server and fetches the requests to the
|
|
|
|
above loaded modules and returns the success/failure result)
|
|
|
|
Please make sure you have a current flaskbotrc file with the
|
|
|
|
configurations of the required bots.
|
|
|
|
Hostname and Port are optional arguments. Default hostname is
|
|
|
|
127.0.0.1 and default port is 5002.
|
|
|
|
See lib/readme.md for more context.
|
|
|
|
'''
|
2017-06-13 13:39:19 -04:00
|
|
|
|
2017-07-26 22:01:35 -04:00
|
|
|
parser = argparse.ArgumentParser(usage=usage)
|
|
|
|
parser.add_argument('--config-file',
|
|
|
|
action='store',
|
|
|
|
help='(config file for the zulip bot server (flaskbotrc))')
|
|
|
|
parser.add_argument('--hostname',
|
|
|
|
action='store',
|
|
|
|
default="127.0.0.1",
|
|
|
|
help='(address on which you want to run the server)')
|
|
|
|
parser.add_argument('--port',
|
|
|
|
action='store',
|
|
|
|
default=5002,
|
|
|
|
help='(port on which you want to run the server)')
|
|
|
|
options = parser.parse_args()
|
2017-06-13 13:39:19 -04:00
|
|
|
if not options.config_file: # if flaskbotrc is not given
|
|
|
|
parser.error('Flaskbotrc not given')
|
2017-07-26 22:01:35 -04:00
|
|
|
return options
|
2017-06-13 14:39:36 -04:00
|
|
|
|
2017-06-13 13:39:19 -04:00
|
|
|
|
2017-06-13 14:39:36 -04:00
|
|
|
def main():
|
|
|
|
# type: () -> None
|
2017-07-26 22:01:35 -04:00
|
|
|
options = parse_args()
|
2017-06-13 13:39:19 -04:00
|
|
|
read_config_file(options.config_file)
|
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())
|
|
|
|
load_lib_modules()
|
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()
|