2020-02-12 23:48:55 +01:00
|
|
|
# Webhooks for external integrations.
|
2020-06-11 00:54:34 +02:00
|
|
|
import re
|
2023-01-04 21:08:26 +01:00
|
|
|
from itertools import zip_longest
|
2023-08-12 09:34:31 +02:00
|
|
|
from typing import List, Literal, TypedDict, cast
|
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
|
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
|
2022-06-17 01:11:12 +02:00
|
|
|
from zerver.lib.types import Validator
|
|
|
|
from zerver.lib.validator import (
|
|
|
|
WildValue,
|
|
|
|
check_dict,
|
|
|
|
check_int,
|
2023-01-04 21:08:26 +01:00
|
|
|
check_list,
|
2022-06-17 01:11:12 +02:00
|
|
|
check_string,
|
|
|
|
check_string_in,
|
|
|
|
check_url,
|
|
|
|
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
|
|
|
|
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
|
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)
|
|
|
|
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)"
|
|
|
|
|
2023-07-31 22:52:35 +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)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
|
2022-06-17 00:02:24 +02:00
|
|
|
def render_block(block: WildValue) -> str:
|
2022-06-17 01:11:12 +02:00
|
|
|
# https://api.slack.com/reference/block-kit/blocks
|
|
|
|
block_type = block["type"].tame(
|
|
|
|
check_string_in(["actions", "context", "divider", "header", "image", "input", "section"])
|
|
|
|
)
|
|
|
|
if block_type == "actions":
|
|
|
|
# Unhandled
|
|
|
|
return ""
|
|
|
|
elif block_type == "context" and block.get("elements"):
|
|
|
|
pieces = []
|
|
|
|
# Slack renders these pieces left-to-right, packed in as
|
|
|
|
# closely as possible. We just render them above each other,
|
|
|
|
# for simplicity.
|
|
|
|
for element in block["elements"]:
|
|
|
|
element_type = element["type"].tame(check_string_in(["image", "plain_text", "mrkdwn"]))
|
|
|
|
if element_type == "image":
|
|
|
|
pieces.append(render_block_element(element))
|
|
|
|
else:
|
2023-01-04 21:05:18 +01:00
|
|
|
pieces.append(element.tame(check_text_block())["text"])
|
2022-06-17 01:11:12 +02:00
|
|
|
return "\n\n".join(piece.strip() for piece in pieces if piece.strip() != "")
|
|
|
|
elif block_type == "divider":
|
|
|
|
return "----"
|
|
|
|
elif block_type == "header":
|
2023-01-04 21:05:18 +01:00
|
|
|
return "## " + block["text"].tame(check_text_block(plain_text_only=True))["text"]
|
2022-06-17 01:11:12 +02:00
|
|
|
elif block_type == "image":
|
|
|
|
image_url = block["image_url"].tame(check_url)
|
|
|
|
alt_text = block["alt_text"].tame(check_string)
|
|
|
|
if "title" in block:
|
2023-01-04 21:05:18 +01:00
|
|
|
alt_text = block["title"].tame(check_text_block(plain_text_only=True))["text"]
|
2022-06-17 01:11:12 +02:00
|
|
|
return f"[{alt_text}]({image_url})"
|
|
|
|
elif block_type == "input":
|
|
|
|
# Unhandled
|
|
|
|
pass
|
|
|
|
elif block_type == "section":
|
|
|
|
pieces = []
|
2020-02-12 23:48:55 +01:00
|
|
|
if "text" in block:
|
2023-01-04 21:05:18 +01:00
|
|
|
pieces.append(block["text"].tame(check_text_block())["text"])
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
if "accessory" in block:
|
2022-06-17 01:11:12 +02:00
|
|
|
pieces.append(render_block_element(block["accessory"]))
|
|
|
|
|
|
|
|
if "fields" in block:
|
2023-01-04 21:08:26 +01:00
|
|
|
fields = block["fields"].tame(check_list(check_text_block()))
|
|
|
|
if len(fields) == 1:
|
|
|
|
# Special-case a single field to display a bit more
|
|
|
|
# nicely, without extraneous borders and limitations
|
|
|
|
# on its contents.
|
|
|
|
pieces.append(fields[0]["text"])
|
|
|
|
else:
|
|
|
|
# It is not possible to have newlines in a table, nor
|
|
|
|
# escape the pipes that make it up; replace them with
|
|
|
|
# whitespace.
|
|
|
|
field_text = [f["text"].replace("\n", " ").replace("|", " ") for f in fields]
|
|
|
|
# Because Slack formats this as two columns, but not
|
|
|
|
# necessarily a table with a bold header, we emit a
|
|
|
|
# blank header row first.
|
|
|
|
table = "| | |\n|-|-|\n"
|
|
|
|
# Then take the fields two-at-a-time to make the table
|
|
|
|
iters = [iter(field_text)] * 2
|
|
|
|
for left, right in zip_longest(*iters, fillvalue=""):
|
|
|
|
table += f"| {left} | {right} |\n"
|
|
|
|
pieces.append(table)
|
2022-06-17 01:11:12 +02:00
|
|
|
|
|
|
|
return "\n\n".join(piece.strip() for piece in pieces if piece.strip() != "")
|
2020-02-12 23:48:55 +01:00
|
|
|
|
2022-06-17 00:02:24 +02:00
|
|
|
return ""
|
2020-02-12 23:48:55 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-01-04 21:05:18 +01:00
|
|
|
class TextField(TypedDict):
|
|
|
|
text: str
|
|
|
|
type: Literal["plain_text", "mrkdwn"]
|
|
|
|
|
|
|
|
|
|
|
|
def check_text_block(plain_text_only: bool = False) -> Validator[TextField]:
|
2022-06-17 01:11:12 +02:00
|
|
|
if plain_text_only:
|
|
|
|
type_validator = check_string_in(["plain_text"])
|
|
|
|
else:
|
2023-01-04 21:05:18 +01:00
|
|
|
type_validator = check_string_in(["plain_text", "mrkdwn"])
|
2022-06-17 01:11:12 +02:00
|
|
|
|
2023-01-04 21:05:18 +01:00
|
|
|
def f(var_name: str, val: object) -> TextField:
|
2022-06-17 01:11:12 +02:00
|
|
|
block = check_dict(
|
|
|
|
[
|
|
|
|
("type", type_validator),
|
|
|
|
("text", check_string),
|
|
|
|
],
|
|
|
|
)(var_name, val)
|
|
|
|
|
2023-01-04 21:05:18 +01:00
|
|
|
return cast(TextField, block)
|
2022-06-17 01:11:12 +02:00
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
|
|
def render_block_element(element: WildValue) -> str:
|
|
|
|
# https://api.slack.com/reference/block-kit/block-elements
|
|
|
|
# Zulip doesn't support interactive elements, so we only render images here
|
|
|
|
element_type = element["type"].tame(check_string)
|
|
|
|
if element_type == "image":
|
|
|
|
image_url = element["image_url"].tame(check_url)
|
|
|
|
alt_text = element["alt_text"].tame(check_string)
|
|
|
|
return f"[{alt_text}]({image_url})"
|
|
|
|
else:
|
|
|
|
# Unsupported
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2022-06-17 00:02:24 +02:00
|
|
|
def render_attachment(attachment: WildValue) -> str:
|
2022-06-17 01:11:12 +02:00
|
|
|
# https://api.slack.com/reference/messaging/attachments
|
2022-08-16 21:53:31 +02:00
|
|
|
# Slack recommends the usage of "blocks" even within attachments; the
|
|
|
|
# rest of the fields we handle here are legacy fields. These fields are
|
|
|
|
# optional and may contain null values.
|
2022-06-17 01:11:12 +02:00
|
|
|
pieces = []
|
2024-02-02 01:22:50 +01:00
|
|
|
if attachment.get("title"):
|
2022-06-16 22:01:03 +02:00
|
|
|
title = attachment["title"].tame(check_string)
|
2024-02-02 01:22:50 +01:00
|
|
|
if attachment.get("title_link"):
|
2022-06-17 01:11:12 +02:00
|
|
|
title_link = attachment["title_link"].tame(check_url)
|
|
|
|
pieces.append(f"## [{title}]({title_link})")
|
|
|
|
else:
|
|
|
|
pieces.append(f"## {title}")
|
2024-02-02 01:22:50 +01:00
|
|
|
if attachment.get("pretext"):
|
2022-06-17 01:11:12 +02:00
|
|
|
pieces.append(attachment["pretext"].tame(check_string))
|
2024-02-02 01:22:50 +01:00
|
|
|
if attachment.get("text"):
|
2022-06-17 01:11:12 +02:00
|
|
|
pieces.append(attachment["text"].tame(check_string))
|
|
|
|
if "fields" in attachment:
|
|
|
|
fields = []
|
|
|
|
for field in attachment["fields"]:
|
2022-11-29 22:11:07 +01:00
|
|
|
if "title" in field and "value" in field and field["title"] and field["value"]:
|
2022-08-16 21:53:31 +02:00
|
|
|
title = field["title"].tame(check_string)
|
|
|
|
value = field["value"].tame(check_string)
|
|
|
|
fields.append(f"*{title}*: {value}")
|
2024-02-02 01:22:50 +01:00
|
|
|
elif field.get("title"):
|
2022-08-16 21:53:31 +02:00
|
|
|
title = field["title"].tame(check_string)
|
|
|
|
fields.append(f"*{title}*")
|
2024-02-02 01:22:50 +01:00
|
|
|
elif field.get("value"):
|
2022-08-16 21:53:31 +02:00
|
|
|
value = field["value"].tame(check_string)
|
|
|
|
fields.append(f"{value}")
|
2022-06-17 01:11:12 +02:00
|
|
|
pieces.append("\n".join(fields))
|
2024-02-02 01:22:50 +01:00
|
|
|
if attachment.get("blocks"):
|
2023-07-31 22:52:35 +02:00
|
|
|
pieces += map(render_block, attachment["blocks"])
|
2024-02-02 01:22:50 +01:00
|
|
|
if attachment.get("image_url"):
|
2022-06-17 01:11:12 +02:00
|
|
|
pieces.append("[]({})".format(attachment["image_url"].tame(check_url)))
|
2024-02-02 01:22:50 +01:00
|
|
|
if attachment.get("footer"):
|
2022-06-17 01:11:12 +02:00
|
|
|
pieces.append(attachment["footer"].tame(check_string))
|
2024-02-02 01:22:50 +01:00
|
|
|
if attachment.get("ts"):
|
2022-06-17 01:11:12 +02:00
|
|
|
time = attachment["ts"].tame(check_int)
|
|
|
|
pieces.append(f"<time:{time}>")
|
|
|
|
|
|
|
|
return "\n\n".join(piece.strip() for piece in pieces if piece.strip() != "")
|
2020-02-12 23:48:55 +01:00
|
|
|
|
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
|
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
|