2019-02-02 23:53:55 +01:00
|
|
|
from django.http import HttpRequest
|
2021-08-19 10:45:20 +02:00
|
|
|
from django.http.response import HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.response import json_success
|
2023-08-12 09:35:14 +02:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
2024-02-15 12:25:57 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2022-01-08 03:06:51 +01:00
|
|
|
ZULIP_MESSAGE_TEMPLATE = "**{message_sender}**: {text}"
|
2021-02-12 08:20:45 +01:00
|
|
|
VALID_OPTIONS = {"SHOULD_NOT_BE_MAPPED": "0", "SHOULD_BE_MAPPED": "1"}
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("Slack", notify_bot_owner_on_invalid_json=False)
|
2023-08-12 09:35:14 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_slack_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:35:14 +02:00
|
|
|
*,
|
|
|
|
user_name: str,
|
|
|
|
text: str,
|
|
|
|
channel_name: str,
|
2024-07-12 02:30:23 +02:00
|
|
|
channels_map_to_topics: str | None = None,
|
2021-08-19 10:45:20 +02:00
|
|
|
) -> HttpResponse:
|
2024-02-15 12:25:57 +01:00
|
|
|
content = ZULIP_MESSAGE_TEMPLATE.format(message_sender=user_name, text=text)
|
|
|
|
topic_name = "Message from Slack"
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2024-02-15 12:25:57 +01:00
|
|
|
if channels_map_to_topics is None:
|
|
|
|
check_send_webhook_message(
|
|
|
|
request,
|
|
|
|
user_profile,
|
|
|
|
topic_name,
|
|
|
|
content,
|
|
|
|
)
|
|
|
|
elif channels_map_to_topics == VALID_OPTIONS["SHOULD_BE_MAPPED"]:
|
|
|
|
# If the webhook URL has a user_specified_topic,
|
|
|
|
# then this topic-channel mapping will not be used.
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = f"channel: {channel_name}"
|
2024-02-15 12:25:57 +01:00
|
|
|
check_send_webhook_message(
|
|
|
|
request,
|
|
|
|
user_profile,
|
|
|
|
topic_name,
|
|
|
|
content,
|
|
|
|
)
|
|
|
|
elif channels_map_to_topics == VALID_OPTIONS["SHOULD_NOT_BE_MAPPED"]:
|
2024-05-04 22:39:36 +02:00
|
|
|
# This channel-channel mapping will be used even if
|
|
|
|
# there is a channel specified in the webhook URL.
|
2024-02-15 12:25:57 +01:00
|
|
|
check_send_webhook_message(
|
|
|
|
request,
|
|
|
|
user_profile,
|
|
|
|
topic_name,
|
|
|
|
content,
|
|
|
|
stream=channel_name,
|
|
|
|
)
|
2017-01-30 21:18:41 +01:00
|
|
|
else:
|
2024-02-15 12:25:57 +01:00
|
|
|
raise JsonableError(_("Error: channels_map_to_topics parameter other than 0 or 1"))
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|