zulip-bot-server: Rename and move source file to api/zulip directory and
add support for running the server from any place.
MODIFY
setup.py
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)

RENAME
flask_bot_server.py
zulip/bot_server.py
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,11 +63,12 @@ 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 <path to flaskbotrc> --hostname <address> --port <port>
Example: zulip-bot-server --config-file ~/flaskbotrc
@@ -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()

zulip-bot-server: Rename and move source file to api/zulip directory and
add support for running the server from any place.