diff --git a/setup.py b/setup.py index f982d7a..0014f1b 100755 --- a/setup.py +++ b/setup.py @@ -41,7 +41,6 @@ package_info = dict( 'Topic :: Communications :: Chat', ], url='https://www.zulip.org/', - packages=['zulip'], data_files=[('share/zulip/examples', ["examples/zuliprc", "examples/create-user", @@ -60,6 +59,7 @@ package_info = dict( entry_points={ 'console_scripts': [ 'zulip-send=zulip.send:main', + 'zulip-bot-server=zulip.bot_server:main', ], }, ) # type: Dict[str, Any] @@ -69,12 +69,15 @@ setuptools_info = dict( 'simplejson', 'six', 'typing>=3.5.2.2', + 'flask>=0.12.2' ], ) try: - from setuptools import setup + from setuptools import setup, find_packages package_info.update(setuptools_info) + package_info['packages'] = find_packages() + except ImportError: from distutils.core import setup from distutils.version import LooseVersion @@ -91,5 +94,12 @@ except ImportError: print("requests >=0.12.1 is not installed", file=sys.stderr) sys.exit(1) + package_list = ['zulip', 'bots_api', 'bots'] + bots_dirs = os.listdir('bots') + for bot in bots_dirs: + if os.path.isdir(os.path.join('bots', bot)): + package_list.append('bots.' + bot) + package_info['packages'] = package_list + setup(**package_info) diff --git a/flask_bot_server.py b/zulip/bot_server.py similarity index 66% rename from flask_bot_server.py rename to zulip/bot_server.py index 463ca4c..de41659 100644 --- a/flask_bot_server.py +++ b/zulip/bot_server.py @@ -1,22 +1,16 @@ from __future__ import absolute_import from __future__ import print_function -from flask import Flask, request, jsonify -import os + import sys import json -from typing import Any, Dict, Mapping, Union, List +import optparse +from flask import Flask, request +from importlib import import_module +from typing import Any, Dict, Mapping, Union, List, Tuple from werkzeug.exceptions import BadRequest from six.moves.configparser import SafeConfigParser -import optparse - -our_dir = os.path.dirname(os.path.abspath(__file__)) - -# For dev setups, we can find the API in the repo itself. -if os.path.exists(os.path.join(our_dir, '../api/zulip')): - sys.path.insert(0, '../api') from zulip import Client -from bots_api.run import get_lib_module from bots_api.bot_lib import ExternalBotHandler, StateHandler bots_config = {} # type: Dict[str, Mapping[str, str]] @@ -38,11 +32,11 @@ def read_config_file(config_file_path): def load_lib_modules(): # type: () -> None for bot in available_bots: - path = "bots/" + str(bot) + "/" + str(bot) + ".py" try: - bots_lib_module[bot] = get_lib_module(path) - except Exception: - print("\n ERROR: Bot \"{}\" doesn't exists. Please make sure you have set up the flaskbotrc " + module_name = 'bots.{bot}.{bot}'.format(bot=bot) + bots_lib_module[bot] = import_module(module_name) + except ImportError: + print("\n Import Error: Bot \"{}\" doesn't exists. Please make sure you have set up the flaskbotrc " "file correctly.\n".format(bot)) sys.exit(1) @@ -69,24 +63,25 @@ def handle_bot(bot): event = json.loads(request.data) message_handler.handle_message(message=event["message"], - client=restricted_client, + bot_handler=restricted_client, state_handler=state_handler) return "Success!" -if __name__ == "__main__": +def parse_args(): + # type: () -> Tuple[Any, Any] usage = ''' - zulip-bot-server --config-file --hostname
--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. - ''' + zulip-bot-server --config-file --hostname
--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. + ''' parser = optparse.OptionParser(usage=usage) parser.add_option('--config-file', @@ -103,9 +98,18 @@ if __name__ == "__main__": (options, args) = parser.parse_args() if not options.config_file: # if flaskbotrc is not given parser.error('Flaskbotrc not given') + return (options, args) + +def main(): + # type: () -> None + (options, args) = parse_args() read_config_file(options.config_file) + global available_bots available_bots = list(bots_config.keys()) load_lib_modules() app.run(host=options.hostname, port=options.port, debug=True) + +if __name__ == '__main__': + main()