zulip-bot-server: Rename and move source file to api/zulip directory and

add support for running the server from any place.
This commit is contained in:
vaibhav 2017-06-14 00:09:36 +05:30 committed by showell
parent 3a3636105c
commit 303daf074a
2 changed files with 45 additions and 31 deletions

View file

@ -41,7 +41,6 @@ package_info = dict(
'Topic :: Communications :: Chat', 'Topic :: Communications :: Chat',
], ],
url='https://www.zulip.org/', url='https://www.zulip.org/',
packages=['zulip'],
data_files=[('share/zulip/examples', data_files=[('share/zulip/examples',
["examples/zuliprc", ["examples/zuliprc",
"examples/create-user", "examples/create-user",
@ -60,6 +59,7 @@ package_info = dict(
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'zulip-send=zulip.send:main', 'zulip-send=zulip.send:main',
'zulip-bot-server=zulip.bot_server:main',
], ],
}, },
) # type: Dict[str, Any] ) # type: Dict[str, Any]
@ -69,12 +69,15 @@ setuptools_info = dict(
'simplejson', 'simplejson',
'six', 'six',
'typing>=3.5.2.2', 'typing>=3.5.2.2',
'flask>=0.12.2'
], ],
) )
try: try:
from setuptools import setup from setuptools import setup, find_packages
package_info.update(setuptools_info) package_info.update(setuptools_info)
package_info['packages'] = find_packages()
except ImportError: except ImportError:
from distutils.core import setup from distutils.core import setup
from distutils.version import LooseVersion from distutils.version import LooseVersion
@ -91,5 +94,12 @@ except ImportError:
print("requests >=0.12.1 is not installed", file=sys.stderr) print("requests >=0.12.1 is not installed", file=sys.stderr)
sys.exit(1) 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) setup(**package_info)

View file

@ -1,22 +1,16 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import print_function from __future__ import print_function
from flask import Flask, request, jsonify
import os
import sys import sys
import json 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 werkzeug.exceptions import BadRequest
from six.moves.configparser import SafeConfigParser 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 zulip import Client
from bots_api.run import get_lib_module
from bots_api.bot_lib import ExternalBotHandler, StateHandler from bots_api.bot_lib import ExternalBotHandler, StateHandler
bots_config = {} # type: Dict[str, Mapping[str, str]] bots_config = {} # type: Dict[str, Mapping[str, str]]
@ -38,11 +32,11 @@ def read_config_file(config_file_path):
def load_lib_modules(): def load_lib_modules():
# type: () -> None # type: () -> None
for bot in available_bots: for bot in available_bots:
path = "bots/" + str(bot) + "/" + str(bot) + ".py"
try: try:
bots_lib_module[bot] = get_lib_module(path) module_name = 'bots.{bot}.{bot}'.format(bot=bot)
except Exception: bots_lib_module[bot] = import_module(module_name)
print("\n ERROR: Bot \"{}\" doesn't exists. Please make sure you have set up the flaskbotrc " except ImportError:
print("\n Import Error: Bot \"{}\" doesn't exists. Please make sure you have set up the flaskbotrc "
"file correctly.\n".format(bot)) "file correctly.\n".format(bot))
sys.exit(1) sys.exit(1)
@ -69,24 +63,25 @@ def handle_bot(bot):
event = json.loads(request.data) event = json.loads(request.data)
message_handler.handle_message(message=event["message"], message_handler.handle_message(message=event["message"],
client=restricted_client, bot_handler=restricted_client,
state_handler=state_handler) state_handler=state_handler)
return "Success!" return "Success!"
if __name__ == "__main__": def parse_args():
# type: () -> Tuple[Any, Any]
usage = ''' usage = '''
zulip-bot-server --config-file <path to flaskbotrc> --hostname <address> --port <port> zulip-bot-server --config-file <path to flaskbotrc> --hostname <address> --port <port>
Example: zulip-bot-server --config-file ~/flaskbotrc Example: zulip-bot-server --config-file ~/flaskbotrc
(This program loads the bot configurations from the (This program loads the bot configurations from the
config file (flaskbotrc here) and loads the bot modules. config file (flaskbotrc here) and loads the bot modules.
It then starts the server and fetches the requests to the It then starts the server and fetches the requests to the
above loaded modules and returns the success/failure result) above loaded modules and returns the success/failure result)
Please make sure you have a current flaskbotrc file with the Please make sure you have a current flaskbotrc file with the
configurations of the required bots. configurations of the required bots.
Hostname and Port are optional arguments. Default hostname is Hostname and Port are optional arguments. Default hostname is
127.0.0.1 and default port is 5002. 127.0.0.1 and default port is 5002.
See lib/readme.md for more context. See lib/readme.md for more context.
''' '''
parser = optparse.OptionParser(usage=usage) parser = optparse.OptionParser(usage=usage)
parser.add_option('--config-file', parser.add_option('--config-file',
@ -103,9 +98,18 @@ if __name__ == "__main__":
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if not options.config_file: # if flaskbotrc is not given if not options.config_file: # if flaskbotrc is not given
parser.error('Flaskbotrc not given') parser.error('Flaskbotrc not given')
return (options, args)
def main():
# type: () -> None
(options, args) = parse_args()
read_config_file(options.config_file) read_config_file(options.config_file)
global available_bots
available_bots = list(bots_config.keys()) available_bots = list(bots_config.keys())
load_lib_modules() load_lib_modules()
app.run(host=options.hostname, port=options.port, debug=True) app.run(host=options.hostname, port=options.port, debug=True)
if __name__ == '__main__':
main()