black: Reformat without skipping string normalization.
This commit is contained in:
parent
fba21bb00d
commit
6f3f9bf7e4
178 changed files with 5242 additions and 5242 deletions
|
@ -10,45 +10,45 @@ with open("README.md") as fh:
|
|||
|
||||
# We should be installable with either setuptools or distutils.
|
||||
package_info = dict(
|
||||
name='zulip_botserver',
|
||||
name="zulip_botserver",
|
||||
version=ZULIP_BOTSERVER_VERSION,
|
||||
description='Zulip\'s Flask server for running bots',
|
||||
description="Zulip's Flask server for running bots",
|
||||
long_description=long_description,
|
||||
long_description_content_type="text/markdown",
|
||||
author='Zulip Open Source Project',
|
||||
author_email='zulip-devel@googlegroups.com',
|
||||
author="Zulip Open Source Project",
|
||||
author_email="zulip-devel@googlegroups.com",
|
||||
classifiers=[
|
||||
'Development Status :: 4 - Beta',
|
||||
'Environment :: Web Environment',
|
||||
'Intended Audience :: Developers',
|
||||
'License :: OSI Approved :: Apache Software License',
|
||||
'Topic :: Communications :: Chat',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Programming Language :: Python :: 3.7',
|
||||
'Programming Language :: Python :: 3.8',
|
||||
'Programming Language :: Python :: 3.9',
|
||||
"Development Status :: 4 - Beta",
|
||||
"Environment :: Web Environment",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: Apache Software License",
|
||||
"Topic :: Communications :: Chat",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.6",
|
||||
"Programming Language :: Python :: 3.7",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
],
|
||||
python_requires='>=3.6',
|
||||
url='https://www.zulip.org/',
|
||||
python_requires=">=3.6",
|
||||
url="https://www.zulip.org/",
|
||||
project_urls={
|
||||
"Source": "https://github.com/zulip/python-zulip-api/",
|
||||
"Documentation": "https://zulip.com/api",
|
||||
},
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'zulip-botserver=zulip_botserver.server:main',
|
||||
"console_scripts": [
|
||||
"zulip-botserver=zulip_botserver.server:main",
|
||||
],
|
||||
},
|
||||
test_suite='tests',
|
||||
package_data={'zulip_botserver': ['py.typed']},
|
||||
test_suite="tests",
|
||||
package_data={"zulip_botserver": ["py.typed"]},
|
||||
) # type: Dict[str, Any]
|
||||
|
||||
setuptools_info = dict(
|
||||
install_requires=[
|
||||
'zulip',
|
||||
'zulip_bots',
|
||||
'flask>=0.12.2',
|
||||
"zulip",
|
||||
"zulip_bots",
|
||||
"flask>=0.12.2",
|
||||
],
|
||||
)
|
||||
|
||||
|
@ -56,7 +56,7 @@ try:
|
|||
from setuptools import find_packages, setup
|
||||
|
||||
package_info.update(setuptools_info)
|
||||
package_info['packages'] = find_packages(exclude=['tests'])
|
||||
package_info["packages"] = find_packages(exclude=["tests"])
|
||||
|
||||
except ImportError:
|
||||
from distutils.core import setup
|
||||
|
@ -79,11 +79,11 @@ except ImportError:
|
|||
print("{name} is not installed.".format(name=module_name), file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
check_dependency_manually('zulip')
|
||||
check_dependency_manually('zulip_bots')
|
||||
check_dependency_manually('flask', '0.12.2')
|
||||
check_dependency_manually("zulip")
|
||||
check_dependency_manually("zulip_bots")
|
||||
check_dependency_manually("flask", "0.12.2")
|
||||
|
||||
package_info['packages'] = ['zulip_botserver']
|
||||
package_info["packages"] = ["zulip_botserver"]
|
||||
|
||||
|
||||
setup(**package_info)
|
||||
|
|
|
@ -13,7 +13,7 @@ class BotServerTestCase(TestCase):
|
|||
server.app.testing = True
|
||||
self.app = server.app.test_client()
|
||||
|
||||
@mock.patch('zulip_bots.lib.ExternalBotHandler')
|
||||
@mock.patch("zulip_bots.lib.ExternalBotHandler")
|
||||
def assert_bot_server_response(
|
||||
self,
|
||||
mock_ExternalBotHandler: mock.Mock,
|
||||
|
|
|
@ -19,58 +19,58 @@ from .server_test_lib import BotServerTestCase
|
|||
class BotServerTests(BotServerTestCase):
|
||||
class MockMessageHandler:
|
||||
def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None:
|
||||
assert message == {'key': "test message"}
|
||||
assert message == {"key": "test message"}
|
||||
|
||||
class MockLibModule:
|
||||
def handler_class(self) -> Any:
|
||||
return BotServerTests.MockMessageHandler()
|
||||
|
||||
def test_successful_request(self) -> None:
|
||||
available_bots = ['helloworld']
|
||||
available_bots = ["helloworld"]
|
||||
bots_config = {
|
||||
'helloworld': {
|
||||
'email': 'helloworld-bot@zulip.com',
|
||||
'key': '123456789qwertyuiop',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"helloworld": {
|
||||
"email": "helloworld-bot@zulip.com",
|
||||
"key": "123456789qwertyuiop",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
}
|
||||
}
|
||||
self.assert_bot_server_response(
|
||||
available_bots=available_bots,
|
||||
bots_config=bots_config,
|
||||
event=dict(
|
||||
message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='abcd1234',
|
||||
message={"content": "@**test** test message"},
|
||||
bot_email="helloworld-bot@zulip.com",
|
||||
trigger="mention",
|
||||
token="abcd1234",
|
||||
),
|
||||
expected_response="beep boop",
|
||||
check_success=True,
|
||||
)
|
||||
|
||||
def test_successful_request_from_two_bots(self) -> None:
|
||||
available_bots = ['helloworld', 'help']
|
||||
available_bots = ["helloworld", "help"]
|
||||
bots_config = {
|
||||
'helloworld': {
|
||||
'email': 'helloworld-bot@zulip.com',
|
||||
'key': '123456789qwertyuiop',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"helloworld": {
|
||||
"email": "helloworld-bot@zulip.com",
|
||||
"key": "123456789qwertyuiop",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
},
|
||||
'help': {
|
||||
'email': 'help-bot@zulip.com',
|
||||
'key': '123456789qwertyuiop',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"help": {
|
||||
"email": "help-bot@zulip.com",
|
||||
"key": "123456789qwertyuiop",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
},
|
||||
}
|
||||
self.assert_bot_server_response(
|
||||
available_bots=available_bots,
|
||||
event=dict(
|
||||
message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='abcd1234',
|
||||
message={"content": "@**test** test message"},
|
||||
bot_email="helloworld-bot@zulip.com",
|
||||
trigger="mention",
|
||||
token="abcd1234",
|
||||
),
|
||||
expected_response="beep boop",
|
||||
bots_config=bots_config,
|
||||
|
@ -79,54 +79,54 @@ class BotServerTests(BotServerTestCase):
|
|||
|
||||
def test_request_for_unkown_bot(self) -> None:
|
||||
bots_config = {
|
||||
'helloworld': {
|
||||
'email': 'helloworld-bot@zulip.com',
|
||||
'key': '123456789qwertyuiop',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"helloworld": {
|
||||
"email": "helloworld-bot@zulip.com",
|
||||
"key": "123456789qwertyuiop",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
},
|
||||
}
|
||||
self.assert_bot_server_response(
|
||||
available_bots=['helloworld'],
|
||||
event=dict(message={'content': "test message"}, bot_email='unknown-bot@zulip.com'),
|
||||
available_bots=["helloworld"],
|
||||
event=dict(message={"content": "test message"}, bot_email="unknown-bot@zulip.com"),
|
||||
bots_config=bots_config,
|
||||
check_success=False,
|
||||
)
|
||||
|
||||
def test_wrong_bot_token(self) -> None:
|
||||
available_bots = ['helloworld']
|
||||
available_bots = ["helloworld"]
|
||||
bots_config = {
|
||||
'helloworld': {
|
||||
'email': 'helloworld-bot@zulip.com',
|
||||
'key': '123456789qwertyuiop',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"helloworld": {
|
||||
"email": "helloworld-bot@zulip.com",
|
||||
"key": "123456789qwertyuiop",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
}
|
||||
}
|
||||
self.assert_bot_server_response(
|
||||
available_bots=available_bots,
|
||||
bots_config=bots_config,
|
||||
event=dict(
|
||||
message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='wrongtoken',
|
||||
message={"content": "@**test** test message"},
|
||||
bot_email="helloworld-bot@zulip.com",
|
||||
trigger="mention",
|
||||
token="wrongtoken",
|
||||
),
|
||||
check_success=False,
|
||||
)
|
||||
|
||||
@mock.patch('logging.error')
|
||||
@mock.patch('zulip_bots.lib.StateHandler')
|
||||
@mock.patch("logging.error")
|
||||
@mock.patch("zulip_bots.lib.StateHandler")
|
||||
def test_wrong_bot_credentials(
|
||||
self, mock_StateHandler: mock.Mock, mock_LoggingError: mock.Mock
|
||||
) -> None:
|
||||
available_bots = ['nonexistent-bot']
|
||||
available_bots = ["nonexistent-bot"]
|
||||
bots_config = {
|
||||
'nonexistent-bot': {
|
||||
'email': 'helloworld-bot@zulip.com',
|
||||
'key': '123456789qwertyuiop',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"nonexistent-bot": {
|
||||
"email": "helloworld-bot@zulip.com",
|
||||
"key": "123456789qwertyuiop",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
}
|
||||
}
|
||||
# This works, but mypy still complains:
|
||||
|
@ -135,26 +135,26 @@ class BotServerTests(BotServerTestCase):
|
|||
with self.assertRaisesRegexp(
|
||||
SystemExit,
|
||||
'Error: Bot "nonexistent-bot" doesn\'t exist. Please make '
|
||||
'sure you have set up the botserverrc file correctly.',
|
||||
"sure you have set up the botserverrc file correctly.",
|
||||
):
|
||||
self.assert_bot_server_response(
|
||||
available_bots=available_bots,
|
||||
event=dict(
|
||||
message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='abcd1234',
|
||||
message={"content": "@**test** test message"},
|
||||
bot_email="helloworld-bot@zulip.com",
|
||||
trigger="mention",
|
||||
token="abcd1234",
|
||||
),
|
||||
bots_config=bots_config,
|
||||
)
|
||||
|
||||
@mock.patch('sys.argv', ['zulip-botserver', '--config-file', '/foo/bar/baz.conf'])
|
||||
@mock.patch("sys.argv", ["zulip-botserver", "--config-file", "/foo/bar/baz.conf"])
|
||||
def test_argument_parsing_defaults(self) -> None:
|
||||
opts = parse_args()
|
||||
assert opts.config_file == '/foo/bar/baz.conf'
|
||||
assert opts.config_file == "/foo/bar/baz.conf"
|
||||
assert opts.bot_name is None
|
||||
assert opts.bot_config_file is None
|
||||
assert opts.hostname == '127.0.0.1'
|
||||
assert opts.hostname == "127.0.0.1"
|
||||
assert opts.port == 5002
|
||||
|
||||
def test_read_config_from_env_vars(self) -> None:
|
||||
|
@ -162,29 +162,29 @@ class BotServerTests(BotServerTestCase):
|
|||
# the stringified environment variable is standard even on
|
||||
# Python 3.7 and earlier.
|
||||
bots_config = OrderedDict()
|
||||
bots_config['hello_world'] = {
|
||||
'email': 'helloworld-bot@zulip.com',
|
||||
'key': 'value',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
bots_config["hello_world"] = {
|
||||
"email": "helloworld-bot@zulip.com",
|
||||
"key": "value",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
}
|
||||
bots_config['giphy'] = {
|
||||
'email': 'giphy-bot@zulip.com',
|
||||
'key': 'value2',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
bots_config["giphy"] = {
|
||||
"email": "giphy-bot@zulip.com",
|
||||
"key": "value2",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
}
|
||||
os.environ['ZULIP_BOTSERVER_CONFIG'] = json.dumps(bots_config)
|
||||
os.environ["ZULIP_BOTSERVER_CONFIG"] = json.dumps(bots_config)
|
||||
|
||||
# No bot specified; should read all bot configs
|
||||
assert server.read_config_from_env_vars() == bots_config
|
||||
|
||||
# Specified bot exists; should read only that section.
|
||||
assert server.read_config_from_env_vars("giphy") == {'giphy': bots_config['giphy']}
|
||||
assert server.read_config_from_env_vars("giphy") == {"giphy": bots_config["giphy"]}
|
||||
|
||||
# Specified bot doesn't exist; should read the first section of the config.
|
||||
assert server.read_config_from_env_vars("redefined_bot") == {
|
||||
'redefined_bot': bots_config['hello_world']
|
||||
"redefined_bot": bots_config["hello_world"]
|
||||
}
|
||||
|
||||
def test_read_config_file(self) -> None:
|
||||
|
@ -195,17 +195,17 @@ class BotServerTests(BotServerTestCase):
|
|||
# No bot specified; should read all bot configs.
|
||||
bot_conf1 = server.read_config_file(os.path.join(current_dir, "test.conf"))
|
||||
expected_config1 = {
|
||||
'helloworld': {
|
||||
'email': 'helloworld-bot@zulip.com',
|
||||
'key': 'value',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"helloworld": {
|
||||
"email": "helloworld-bot@zulip.com",
|
||||
"key": "value",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
},
|
||||
'giphy': {
|
||||
'email': 'giphy-bot@zulip.com',
|
||||
'key': 'value2',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"giphy": {
|
||||
"email": "giphy-bot@zulip.com",
|
||||
"key": "value2",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
},
|
||||
}
|
||||
assert json.dumps(bot_conf1, sort_keys=True) == json.dumps(expected_config1, sort_keys=True)
|
||||
|
@ -213,11 +213,11 @@ class BotServerTests(BotServerTestCase):
|
|||
# Specified bot exists; should read only that section.
|
||||
bot_conf3 = server.read_config_file(os.path.join(current_dir, "test.conf"), "giphy")
|
||||
expected_config3 = {
|
||||
'giphy': {
|
||||
'email': 'giphy-bot@zulip.com',
|
||||
'key': 'value2',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"giphy": {
|
||||
"email": "giphy-bot@zulip.com",
|
||||
"key": "value2",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
}
|
||||
}
|
||||
assert json.dumps(bot_conf3, sort_keys=True) == json.dumps(expected_config3, sort_keys=True)
|
||||
|
@ -225,11 +225,11 @@ class BotServerTests(BotServerTestCase):
|
|||
# Specified bot doesn't exist; should read the first section of the config.
|
||||
bot_conf2 = server.read_config_file(os.path.join(current_dir, "test.conf"), "redefined_bot")
|
||||
expected_config2 = {
|
||||
'redefined_bot': {
|
||||
'email': 'helloworld-bot@zulip.com',
|
||||
'key': 'value',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
"redefined_bot": {
|
||||
"email": "helloworld-bot@zulip.com",
|
||||
"key": "value",
|
||||
"site": "http://localhost",
|
||||
"token": "abcd1234",
|
||||
}
|
||||
}
|
||||
assert json.dumps(bot_conf2, sort_keys=True) == json.dumps(expected_config2, sort_keys=True)
|
||||
|
@ -238,18 +238,18 @@ class BotServerTests(BotServerTestCase):
|
|||
# This testcase requires hardcoded paths, which here is a good thing so if we ever
|
||||
# restructure zulip_bots, this test would fail and we would also update Botserver
|
||||
# at the same time.
|
||||
helloworld = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot='helloworld'))
|
||||
helloworld = import_module("zulip_bots.bots.{bot}.{bot}".format(bot="helloworld"))
|
||||
root_dir = Path(__file__).parents[2].as_posix()
|
||||
# load valid module name
|
||||
module = server.load_lib_modules(['helloworld'])['helloworld']
|
||||
module = server.load_lib_modules(["helloworld"])["helloworld"]
|
||||
assert module == helloworld
|
||||
|
||||
# load valid file path
|
||||
path = Path(
|
||||
root_dir, 'zulip_bots/zulip_bots/bots/{bot}/{bot}.py'.format(bot='helloworld')
|
||||
root_dir, "zulip_bots/zulip_bots/bots/{bot}/{bot}.py".format(bot="helloworld")
|
||||
).as_posix()
|
||||
module = server.load_lib_modules([path])[path]
|
||||
assert module.__name__ == 'custom_bot_module'
|
||||
assert module.__name__ == "custom_bot_module"
|
||||
assert module.__file__ == path
|
||||
assert isinstance(module, ModuleType)
|
||||
|
||||
|
@ -257,23 +257,23 @@ class BotServerTests(BotServerTestCase):
|
|||
with self.assertRaisesRegexp(
|
||||
SystemExit,
|
||||
'Error: Bot "botserver-test-case-random-bot" doesn\'t exist. '
|
||||
'Please make sure you have set up the botserverrc file correctly.',
|
||||
"Please make sure you have set up the botserverrc file correctly.",
|
||||
):
|
||||
module = server.load_lib_modules(['botserver-test-case-random-bot'])[
|
||||
'botserver-test-case-random-bot'
|
||||
module = server.load_lib_modules(["botserver-test-case-random-bot"])[
|
||||
"botserver-test-case-random-bot"
|
||||
]
|
||||
|
||||
# load invalid file path
|
||||
with self.assertRaisesRegexp(
|
||||
SystemExit,
|
||||
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
|
||||
'Please make sure you have set up the botserverrc file correctly.'.format(root_dir),
|
||||
"Please make sure you have set up the botserverrc file correctly.".format(root_dir),
|
||||
):
|
||||
path = Path(
|
||||
root_dir, 'zulip_bots/zulip_bots/bots/{bot}.py'.format(bot='helloworld')
|
||||
root_dir, "zulip_bots/zulip_bots/bots/{bot}.py".format(bot="helloworld")
|
||||
).as_posix()
|
||||
module = server.load_lib_modules([path])[path]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
@ -2,51 +2,51 @@ import argparse
|
|||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
usage = '''
|
||||
usage = """
|
||||
zulip-botserver --config-file <path/to/botserverrc> [--hostname=<address>] [--port=<port>]
|
||||
'''
|
||||
"""
|
||||
|
||||
parser = argparse.ArgumentParser(usage=usage)
|
||||
mutually_exclusive_args = parser.add_mutually_exclusive_group(required=True)
|
||||
# config-file or use-env-vars made mutually exclusive to prevent conflicts
|
||||
mutually_exclusive_args.add_argument(
|
||||
'--config-file',
|
||||
'-c',
|
||||
action='store',
|
||||
help='Config file for the Botserver. Use your `botserverrc` for multiple bots or'
|
||||
'`zuliprc` for a single bot.',
|
||||
"--config-file",
|
||||
"-c",
|
||||
action="store",
|
||||
help="Config file for the Botserver. Use your `botserverrc` for multiple bots or"
|
||||
"`zuliprc` for a single bot.",
|
||||
)
|
||||
mutually_exclusive_args.add_argument(
|
||||
'--use-env-vars',
|
||||
'-e',
|
||||
action='store_true',
|
||||
help='Load configuration from JSON in ZULIP_BOTSERVER_CONFIG environment variable.',
|
||||
"--use-env-vars",
|
||||
"-e",
|
||||
action="store_true",
|
||||
help="Load configuration from JSON in ZULIP_BOTSERVER_CONFIG environment variable.",
|
||||
)
|
||||
parser.add_argument(
|
||||
'--bot-config-file',
|
||||
action='store',
|
||||
"--bot-config-file",
|
||||
action="store",
|
||||
default=None,
|
||||
help='Config file for bots. Only needed when one of '
|
||||
'the bots you want to run requires a config file.',
|
||||
help="Config file for bots. Only needed when one of "
|
||||
"the bots you want to run requires a config file.",
|
||||
)
|
||||
parser.add_argument(
|
||||
'--bot-name',
|
||||
'-b',
|
||||
action='store',
|
||||
help='Run a single bot BOT_NAME. Use this option to run the Botserver '
|
||||
'with a `zuliprc` config file.',
|
||||
"--bot-name",
|
||||
"-b",
|
||||
action="store",
|
||||
help="Run a single bot BOT_NAME. Use this option to run the Botserver "
|
||||
"with a `zuliprc` config file.",
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hostname',
|
||||
action='store',
|
||||
"--hostname",
|
||||
action="store",
|
||||
default="127.0.0.1",
|
||||
help='Address on which you want to run the Botserver. (default: %(default)s)',
|
||||
help="Address on which you want to run the Botserver. (default: %(default)s)",
|
||||
)
|
||||
parser.add_argument(
|
||||
'--port',
|
||||
action='store',
|
||||
"--port",
|
||||
action="store",
|
||||
default=5002,
|
||||
type=int,
|
||||
help='Port on which you want to run the Botserver. (default: %(default)d)',
|
||||
help="Port on which you want to run the Botserver. (default: %(default)d)",
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
|
|
@ -23,17 +23,17 @@ from zulip_botserver.input_parameters import parse_args
|
|||
|
||||
def read_config_section(parser: configparser.ConfigParser, section: str) -> Dict[str, str]:
|
||||
section_info = {
|
||||
"email": parser.get(section, 'email'),
|
||||
"key": parser.get(section, 'key'),
|
||||
"site": parser.get(section, 'site'),
|
||||
"token": parser.get(section, 'token'),
|
||||
"email": parser.get(section, "email"),
|
||||
"key": parser.get(section, "key"),
|
||||
"site": parser.get(section, "site"),
|
||||
"token": parser.get(section, "token"),
|
||||
}
|
||||
return section_info
|
||||
|
||||
|
||||
def read_config_from_env_vars(bot_name: Optional[str] = None) -> Dict[str, Dict[str, str]]:
|
||||
bots_config = {} # type: Dict[str, Dict[str, str]]
|
||||
json_config = os.environ.get('ZULIP_BOTSERVER_CONFIG')
|
||||
json_config = os.environ.get("ZULIP_BOTSERVER_CONFIG")
|
||||
|
||||
if json_config is None:
|
||||
raise OSError(
|
||||
|
@ -126,15 +126,15 @@ def load_lib_modules(available_bots: List[str]) -> Dict[str, Any]:
|
|||
bots_lib_module = {}
|
||||
for bot in available_bots:
|
||||
try:
|
||||
if bot.endswith('.py') and os.path.isfile(bot):
|
||||
if bot.endswith(".py") and os.path.isfile(bot):
|
||||
lib_module = load_module_from_file(bot)
|
||||
else:
|
||||
module_name = 'zulip_bots.bots.{bot}.{bot}'.format(bot=bot)
|
||||
module_name = "zulip_bots.bots.{bot}.{bot}".format(bot=bot)
|
||||
lib_module = import_module(module_name)
|
||||
bots_lib_module[bot] = lib_module
|
||||
except ImportError:
|
||||
error_message = (
|
||||
"Error: Bot \"{}\" doesn't exist. Please make sure "
|
||||
'Error: Bot "{}" doesn\'t exist. Please make sure '
|
||||
"you have set up the botserverrc file correctly.\n".format(bot)
|
||||
)
|
||||
if bot == "api":
|
||||
|
@ -157,7 +157,7 @@ def load_bot_handlers(
|
|||
api_key=bots_config[bot]["key"],
|
||||
site=bots_config[bot]["site"],
|
||||
)
|
||||
bot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bots', bot)
|
||||
bot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "bots", bot)
|
||||
bot_handler = lib.ExternalBotHandler(
|
||||
client, bot_dir, bot_details={}, bot_config_parser=third_party_bot_conf
|
||||
)
|
||||
|
@ -184,12 +184,12 @@ app = Flask(__name__)
|
|||
bots_config = {} # type: Dict[str, Dict[str, str]]
|
||||
|
||||
|
||||
@app.route('/', methods=['POST'])
|
||||
@app.route("/", methods=["POST"])
|
||||
def handle_bot() -> str:
|
||||
event = request.get_json(force=True)
|
||||
assert event is not None
|
||||
for bot_name, config in bots_config.items():
|
||||
if config['email'] == event['bot_email']:
|
||||
if config["email"] == event["bot_email"]:
|
||||
bot = bot_name
|
||||
bot_config = config
|
||||
break
|
||||
|
@ -197,27 +197,27 @@ def handle_bot() -> str:
|
|||
raise BadRequest(
|
||||
"Cannot find a bot with email {} in the Botserver "
|
||||
"configuration file. Do the emails in your botserverrc "
|
||||
"match the bot emails on the server?".format(event['bot_email'])
|
||||
"match the bot emails on the server?".format(event["bot_email"])
|
||||
)
|
||||
if bot_config['token'] != event['token']:
|
||||
if bot_config["token"] != event["token"]:
|
||||
raise Unauthorized(
|
||||
"Request token does not match token found for bot {} in the "
|
||||
"Botserver configuration file. Do the outgoing webhooks in "
|
||||
"Zulip point to the right Botserver?".format(event['bot_email'])
|
||||
"Zulip point to the right Botserver?".format(event["bot_email"])
|
||||
)
|
||||
app.config.get("BOTS_LIB_MODULES", {})[bot]
|
||||
bot_handler = app.config.get("BOT_HANDLERS", {})[bot]
|
||||
message_handler = app.config.get("MESSAGE_HANDLERS", {})[bot]
|
||||
is_mentioned = event['trigger'] == "mention"
|
||||
is_private_message = event['trigger'] == "private_message"
|
||||
is_mentioned = event["trigger"] == "mention"
|
||||
is_private_message = event["trigger"] == "private_message"
|
||||
message = event["message"]
|
||||
message['full_content'] = message['content']
|
||||
message["full_content"] = message["content"]
|
||||
# Strip at-mention botname from the message
|
||||
if is_mentioned:
|
||||
# message['content'] will be None when the bot's @-mention is not at the beginning.
|
||||
# In that case, the message shall not be handled.
|
||||
message['content'] = lib.extract_query_without_mention(message=message, client=bot_handler)
|
||||
if message['content'] is None:
|
||||
message["content"] = lib.extract_query_without_mention(message=message, client=bot_handler)
|
||||
if message["content"] is None:
|
||||
return json.dumps(dict(response_not_required=True))
|
||||
|
||||
if is_private_message or is_mentioned:
|
||||
|
@ -261,5 +261,5 @@ def main() -> None:
|
|||
app.run(host=options.hostname, port=int(options.port), debug=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue