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
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
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")
|
2016-05-07 20:07:48 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_zendesk_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
ticket_title: str = REQ(),
|
|
|
|
ticket_id: str = REQ(),
|
|
|
|
message: str = REQ(),
|
|
|
|
) -> 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.
|
|
|
|
"""
|
2023-07-12 13:37:08 +02:00
|
|
|
topic = truncate(f"#{ticket_id}: {ticket_title}", 60)
|
|
|
|
check_send_webhook_message(request, user_profile, topic, message)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|