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
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.actions import check_send_stream_message
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2021-08-21 19:24:20 +02:00
|
|
|
from zerver.lib.request import REQ, RequestNotes, has_request_variables
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
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)
|
2017-01-30 21:18:41 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_slack_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
user_name: str = REQ(),
|
|
|
|
text: str = REQ(),
|
|
|
|
channel_name: str = REQ(),
|
2021-02-12 08:20:45 +01:00
|
|
|
stream: str = REQ(default="slack"),
|
|
|
|
channels_map_to_topics: str = REQ(default="1"),
|
2021-08-19 10:45:20 +02:00
|
|
|
) -> HttpResponse:
|
2017-01-30 21:18:41 +01:00
|
|
|
|
|
|
|
if channels_map_to_topics not in list(VALID_OPTIONS.values()):
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Error: channels_map_to_topics parameter other than 0 or 1"))
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if channels_map_to_topics == VALID_OPTIONS["SHOULD_BE_MAPPED"]:
|
2020-06-09 00:25:09 +02:00
|
|
|
subject = f"channel: {channel_name}"
|
2017-01-30 21:18:41 +01:00
|
|
|
else:
|
|
|
|
stream = channel_name
|
|
|
|
subject = _("Message from Slack")
|
|
|
|
|
|
|
|
content = ZULIP_MESSAGE_TEMPLATE.format(message_sender=user_name, text=text)
|
2021-08-21 19:24:20 +02:00
|
|
|
client = RequestNotes.get_notes(request).client
|
2021-07-09 18:10:51 +02:00
|
|
|
assert client is not None
|
|
|
|
check_send_stream_message(user_profile, client, stream, subject, content)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|