2020-02-12 23:48:55 +01:00
|
|
|
# Webhooks for external integrations.
|
2020-06-11 00:54:34 +02:00
|
|
|
import re
|
2022-06-16 22:01:03 +02:00
|
|
|
from typing import Optional
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-02-12 23:48:55 +01:00
|
|
|
|
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
|
2022-04-24 09:32:05 +02:00
|
|
|
from zerver.lib.request import REQ, RequestVariableMissingError, has_request_variables
|
2020-02-12 23:48:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2022-06-16 22:01:03 +02:00
|
|
|
from zerver.lib.validator import WildValue, WildValueDict, check_string, check_url, to_wild_value
|
2020-02-12 23:48:55 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-02-12 23:48:55 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("SlackIncoming")
|
2020-02-12 23:48:55 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_slack_incoming_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
user_specified_topic: Optional[str] = REQ("topic", default=None),
|
|
|
|
) -> 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)
|
|
|
|
user_specified_topic = re.sub("^[@#]", "", channel)
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
if user_specified_topic is None:
|
|
|
|
user_specified_topic = "(no topic)"
|
|
|
|
|
|
|
|
body = ""
|
|
|
|
|
2022-06-16 23:58:41 +02:00
|
|
|
if "blocks" in payload and payload["blocks"]:
|
2020-02-12 23:48:55 +01:00
|
|
|
for block in payload["blocks"]:
|
|
|
|
body = add_block(block, body)
|
|
|
|
|
2022-06-16 23:58:41 +02:00
|
|
|
if "attachments" in payload and payload["attachments"]:
|
2020-02-12 23:48:55 +01:00
|
|
|
for attachment in payload["attachments"]:
|
|
|
|
body = add_attachment(attachment, body)
|
|
|
|
|
2022-06-16 22:01:03 +02:00
|
|
|
if body == "" and "text" in payload and payload["text"]:
|
|
|
|
body += payload["text"].tame(check_string)
|
|
|
|
if "icon_emoji" in payload and payload["icon_emoji"]:
|
|
|
|
body = "{} {}".format(payload["icon_emoji"].tame(check_string), body)
|
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)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
|
2022-06-16 22:01:03 +02:00
|
|
|
def add_block(block: WildValue, body: str) -> str:
|
|
|
|
block_type = block["type"].tame(check_string)
|
2020-02-12 23:48:55 +01:00
|
|
|
if block_type == "section":
|
|
|
|
if "text" in block:
|
|
|
|
text = block["text"]
|
2022-06-16 22:01:03 +02:00
|
|
|
while isinstance(text, WildValueDict):
|
2020-02-12 23:48:55 +01:00
|
|
|
text = text["text"]
|
2022-06-16 22:01:03 +02:00
|
|
|
body += "\n\n" + text.tame(check_string)
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
if "accessory" in block:
|
|
|
|
accessory = block["accessory"]
|
2022-06-16 22:01:03 +02:00
|
|
|
accessory_type = accessory["type"].tame(check_string)
|
2020-02-12 23:48:55 +01:00
|
|
|
if accessory_type == "image":
|
|
|
|
# This should become ![text](url) once proper Markdown images are supported
|
2022-06-16 22:01:03 +02:00
|
|
|
alt_text = accessory["alt_text"].tame(check_string)
|
|
|
|
image_url = accessory["image_url"].tame(check_url)
|
|
|
|
body += f"\n[{alt_text}]({image_url})"
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
return body
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-16 22:01:03 +02:00
|
|
|
def add_attachment(attachment: WildValue, body: str) -> str:
|
2020-02-12 23:48:55 +01:00
|
|
|
attachment_body = ""
|
|
|
|
if "title" in attachment and "title_link" in attachment:
|
2022-06-16 22:01:03 +02:00
|
|
|
title = attachment["title"].tame(check_string)
|
|
|
|
title_link = attachment["title_link"].tame(check_string)
|
|
|
|
attachment_body += f"[{title}]({title_link})\n"
|
2020-02-12 23:48:55 +01:00
|
|
|
if "text" in attachment:
|
2022-06-16 22:01:03 +02:00
|
|
|
attachment_body += attachment["text"].tame(check_string)
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
return body + attachment_body
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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
|
2021-02-12 08:20:45 +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
|
2021-06-24 16:44:49 +02: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
|