bots: Allow incoming webhook bots to be configured via /bots.

Without disturbing the flow of the existing code for configuring
embedded bots too much, we now use the config_options feature to
allow incoming webhook type bot to be configured via. the "/bots"
endpoint of the API.
This commit is contained in:
Hemanth V. Alluri
2019-08-18 16:26:21 +05:30
committed by Tim Abbott
parent 94c351ead4
commit d73a37726d
4 changed files with 148 additions and 9 deletions

View File

@@ -49,7 +49,30 @@ def check_short_name(short_name_raw: str) -> str:
def check_valid_bot_config(bot_type: int, service_name: str,
config_data: Dict[str, str]) -> None:
if bot_type == UserProfile.EMBEDDED_BOT:
if bot_type == UserProfile.INCOMING_WEBHOOK_BOT:
from zerver.lib.integrations import WEBHOOK_INTEGRATIONS
config_options = None
for integration in WEBHOOK_INTEGRATIONS:
if integration.name == service_name:
# key: validator
config_options = {c[1]: c[2] for c in integration.config_options}
break
if not config_options:
raise JsonableError(_("Invalid integration '%s'.") % (service_name,))
missing_keys = set(config_options.keys()) - set(config_data.keys())
if missing_keys:
raise JsonableError(_("Missing configuration parameters: %s") % (
missing_keys,))
for key, validator in config_options.items():
value = config_data[key]
error = validator(key, value)
if error:
raise JsonableError(_("Invalid {} value {} ({})").format(
key, value, error))
elif bot_type == UserProfile.EMBEDDED_BOT:
try:
from zerver.lib.bot_lib import get_bot_handler
bot_handler = get_bot_handler(service_name)