2020-02-12 23:48:55 +01:00
|
|
|
# Webhooks for external integrations.
|
2020-06-11 00:54:34 +02:00
|
|
|
import re
|
2024-10-18 13:41:48 +02:00
|
|
|
from collections.abc import Callable
|
|
|
|
from functools import wraps
|
2020-02-12 23:48:55 +01:00
|
|
|
|
2024-10-18 13:41:48 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse, JsonResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2024-10-18 13:41:48 +02:00
|
|
|
from typing_extensions import ParamSpec
|
2020-02-12 23:48:55 +01:00
|
|
|
|
2024-08-08 13:22:36 +02:00
|
|
|
from zerver.data_import.slack_message_conversion import render_attachment, render_block
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2022-06-16 22:01:03 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.request import RequestVariableMissingError
|
2020-02-12 23:48:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
2024-08-08 13:22:36 +02:00
|
|
|
from zerver.lib.validator import check_string, to_wild_value
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.webhooks.common import OptionalUserSpecifiedTopicStr, check_send_webhook_message
|
2020-02-12 23:48:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2024-10-18 13:41:48 +02:00
|
|
|
ParamT = ParamSpec("ParamT")
|
|
|
|
|
|
|
|
|
|
|
|
def slack_error_handler(view_func: Callable[..., HttpResponse]) -> Callable[..., HttpResponse]:
|
|
|
|
"""
|
|
|
|
A decorator that catches JsonableError exceptions and returns a
|
|
|
|
Slack-compatible error response in the format:
|
|
|
|
{ok: false, error: "error message"}.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@wraps(view_func)
|
|
|
|
def wrapped_view(
|
|
|
|
request: HttpRequest, *args: ParamT.args, **kwargs: ParamT.kwargs
|
|
|
|
) -> HttpResponse:
|
|
|
|
try:
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
except JsonableError as error:
|
|
|
|
return JsonResponse({"ok": False, "error": error.msg}, status=error.http_status_code)
|
|
|
|
|
|
|
|
return wrapped_view
|
|
|
|
|
2020-02-12 23:48:55 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("SlackIncoming")
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2024-10-18 13:41:48 +02:00
|
|
|
@slack_error_handler
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_slack_incoming_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
|
|
|
user_specified_topic: OptionalUserSpecifiedTopicStr = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2020-02-12 23:48:55 +01:00
|
|
|
# Slack accepts webhook payloads as payload="encoded json" as
|
|
|
|
# application/x-www-form-urlencoded, as well as in the body as
|
2022-04-24 09:32:05 +02:00
|
|
|
# application/json.
|
|
|
|
if request.content_type == "application/json":
|
2020-02-12 23:48:55 +01:00
|
|
|
try:
|
2022-04-24 09:32:05 +02:00
|
|
|
val = request.body.decode(request.encoding or "utf-8")
|
|
|
|
except UnicodeDecodeError: # nocoverage
|
|
|
|
raise JsonableError(_("Malformed payload"))
|
|
|
|
else:
|
|
|
|
req_var = "payload"
|
|
|
|
if req_var in request.POST:
|
|
|
|
val = request.POST[req_var]
|
|
|
|
elif req_var in request.GET: # nocoverage
|
|
|
|
val = request.GET[req_var]
|
|
|
|
else:
|
|
|
|
raise RequestVariableMissingError(req_var)
|
2022-06-16 22:01:03 +02:00
|
|
|
|
|
|
|
payload = to_wild_value("payload", val)
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
if user_specified_topic is None and "channel" in payload:
|
2022-06-16 22:01:03 +02:00
|
|
|
channel = payload["channel"].tame(check_string)
|
2024-04-26 20:30:22 +02:00
|
|
|
user_specified_topic = re.sub(r"^[@#]", "", channel)
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
if user_specified_topic is None:
|
|
|
|
user_specified_topic = "(no topic)"
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
pieces: list[str] = []
|
2024-02-02 01:22:50 +01:00
|
|
|
if payload.get("blocks"):
|
2023-07-31 22:52:35 +02:00
|
|
|
pieces += map(render_block, payload["blocks"])
|
2020-02-12 23:48:55 +01:00
|
|
|
|
2024-02-02 01:22:50 +01:00
|
|
|
if payload.get("attachments"):
|
2023-07-31 22:52:35 +02:00
|
|
|
pieces += map(render_attachment, payload["attachments"])
|
2022-06-17 00:02:24 +02:00
|
|
|
|
|
|
|
body = "\n\n".join(piece.strip() for piece in pieces if piece.strip() != "")
|
2020-02-12 23:48:55 +01:00
|
|
|
|
2024-02-02 01:22:50 +01:00
|
|
|
if body == "" and payload.get("text"):
|
|
|
|
if payload.get("icon_emoji"):
|
2022-06-17 00:02:24 +02:00
|
|
|
body = payload["icon_emoji"].tame(check_string) + " "
|
|
|
|
body += payload["text"].tame(check_string)
|
|
|
|
body = body.strip()
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
if body != "":
|
|
|
|
body = replace_formatting(replace_links(body).strip())
|
|
|
|
check_send_webhook_message(request, user_profile, user_specified_topic, body)
|
2024-10-04 17:42:27 +02:00
|
|
|
return json_success(request, data={"ok": True})
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
def replace_links(text: str) -> str:
|
|
|
|
return re.sub(r"<(\w+?:\/\/.*?)\|(.*?)>", r"[\2](\1)", text)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-02-12 23:48:55 +01:00
|
|
|
def replace_formatting(text: str) -> str:
|
|
|
|
# Slack uses *text* for bold, whereas Zulip interprets that as italics
|
2022-11-29 22:10:34 +01:00
|
|
|
text = re.sub(r"([^\w]|^)\*(?!\s+)([^\*\n]+)(?<!\s)\*((?=[^\w])|$)", r"\1**\2**\3", text)
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
# Slack uses _text_ for emphasis, whereas Zulip interprets that as nothing
|
2022-11-29 22:10:34 +01:00
|
|
|
text = re.sub(r"([^\w]|^)[_](?!\s+)([^\_\n]+)(?<!\s)[_]((?=[^\w])|$)", r"\1*\2*\3", text)
|
2020-02-12 23:48:55 +01:00
|
|
|
return text
|