2016-03-13 15:10:12 +01:00
|
|
|
# Webhooks for external integrations.
|
2017-11-16 00:43:10 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import authenticated_rest_api_view
|
2017-11-16 00:43:10 +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
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2016-03-13 15:10:12 +01:00
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def truncate(string: str, length: int) -> str:
|
2016-03-13 15:10:12 +01:00
|
|
|
if len(string) > length:
|
2021-02-12 08:20:45 +01:00
|
|
|
string = string[: length - 3] + "..."
|
2016-03-13 15:10:12 +01:00
|
|
|
return string
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-16 23:37:32 +01:00
|
|
|
@authenticated_rest_api_view(webhook_client_name="Zendesk")
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_zendesk_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
|
|
|
ticket_title: str,
|
|
|
|
ticket_id: str,
|
|
|
|
message: str,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2016-03-13 15:10:12 +01:00
|
|
|
"""
|
2020-03-28 01:25:56 +01:00
|
|
|
Zendesk uses triggers with message templates. This webhook uses the
|
2023-07-12 13:37:08 +02:00
|
|
|
ticket_id and ticket_title to create a topic. And passes with zendesk
|
2016-03-13 15:10:12 +01:00
|
|
|
user's configured message to zulip.
|
|
|
|
"""
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = truncate(f"#{ticket_id}: {ticket_title}", 60)
|
|
|
|
check_send_webhook_message(request, user_profile, topic_name, message)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|