2018-01-07 12:05:44 +01:00
|
|
|
# Webhooks for external integrations.
|
2024-07-12 02:30:17 +02:00
|
|
|
from typing import Callable, Optional
|
2018-01-07 12:05:44 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
2024-04-29 23:20:36 +02:00
|
|
|
from zerver.lib.partial import partial
|
2019-04-18 22:23:36 +02:00
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_int, check_none_or, check_string, check_url
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.webhooks.common import (
|
|
|
|
check_send_webhook_message,
|
|
|
|
get_http_headers_from_filename,
|
|
|
|
validate_extract_webhook_http_header,
|
|
|
|
)
|
2018-01-07 12:05:44 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
TICKET_STARTED_TEMPLATE = """
|
|
|
|
{customer_name} submitted new ticket [#{number}: {title}]({app_url}):
|
|
|
|
|
|
|
|
``` quote
|
|
|
|
{summary}
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
TICKET_ASSIGNED_TEMPLATE = "[#{number}: {title}]({app_url}) ({state}) assigned to {assignee_info}."
|
|
|
|
|
|
|
|
AGENT_REPLIED_TEMPLATE = """
|
|
|
|
{actor} {action} [ticket #{number}]({app_ticket_url}):
|
|
|
|
|
|
|
|
``` quote
|
|
|
|
{plain_text_body}
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 19:42:15 +02:00
|
|
|
def ticket_started_body(payload: WildValue) -> str:
|
|
|
|
return TICKET_STARTED_TEMPLATE.format(
|
|
|
|
customer_name=payload["customer_name"].tame(check_string),
|
|
|
|
number=payload["number"].tame(check_int),
|
|
|
|
title=payload["title"].tame(check_string),
|
|
|
|
app_url=payload["app_url"].tame(check_url),
|
|
|
|
summary=payload["summary"].tame(check_string),
|
|
|
|
)
|
2018-01-07 12:05:44 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 19:42:15 +02:00
|
|
|
def ticket_assigned_body(payload: WildValue) -> Optional[str]:
|
|
|
|
state = payload["state"].tame(check_string)
|
2019-04-18 22:23:36 +02:00
|
|
|
kwargs = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"state": "open" if state == "opened" else state,
|
2022-10-08 19:42:15 +02:00
|
|
|
"number": payload["number"].tame(check_int),
|
|
|
|
"title": payload["title"].tame(check_string),
|
|
|
|
"app_url": payload["app_url"].tame(check_url),
|
2019-04-18 22:23:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-08 19:42:15 +02:00
|
|
|
assignee = payload["assignee"].tame(check_none_or(check_string))
|
|
|
|
assigned_group = payload["assigned_group"].tame(check_none_or(check_string))
|
2018-01-07 12:05:44 +01:00
|
|
|
|
|
|
|
if assignee or assigned_group:
|
|
|
|
if assignee and assigned_group:
|
2023-07-19 23:06:38 +02:00
|
|
|
kwargs["assignee_info"] = f"{assignee} from {assigned_group}"
|
2018-01-07 12:05:44 +01:00
|
|
|
elif assignee:
|
2023-01-18 03:23:15 +01:00
|
|
|
kwargs["assignee_info"] = f"{assignee}"
|
2018-01-07 12:05:44 +01:00
|
|
|
elif assigned_group:
|
2023-01-18 03:23:15 +01:00
|
|
|
kwargs["assignee_info"] = f"{assigned_group}"
|
2019-04-18 22:23:36 +02:00
|
|
|
|
|
|
|
return TICKET_ASSIGNED_TEMPLATE.format(**kwargs)
|
2018-01-07 12:05:44 +01:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-04-29 23:52:26 +02:00
|
|
|
def replied_body(actor: str, action: str, payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
actor_url = "http://api.groovehq.com/v1/{}/".format(actor + "s")
|
2022-10-08 19:42:15 +02:00
|
|
|
actor = payload["links"]["author"]["href"].tame(check_url).split(actor_url)[1]
|
|
|
|
number = (
|
|
|
|
payload["links"]["ticket"]["href"]
|
|
|
|
.tame(check_url)
|
|
|
|
.split("http://api.groovehq.com/v1/tickets/")[1]
|
|
|
|
)
|
2018-01-07 12:05:44 +01:00
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
body = AGENT_REPLIED_TEMPLATE.format(
|
|
|
|
actor=actor,
|
|
|
|
action=action,
|
|
|
|
number=number,
|
2022-10-08 19:42:15 +02:00
|
|
|
app_ticket_url=payload["app_ticket_url"].tame(check_url),
|
|
|
|
plain_text_body=payload["plain_text_body"].tame(check_string),
|
2019-04-18 22:23:36 +02:00
|
|
|
)
|
2018-01-07 12:05:44 +01:00
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
return body
|
2018-01-07 12:05:44 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
EVENTS_FUNCTION_MAPPER: dict[str, Callable[[WildValue], Optional[str]]] = {
|
2021-07-16 11:40:46 +02:00
|
|
|
"ticket_started": ticket_started_body,
|
|
|
|
"ticket_assigned": ticket_assigned_body,
|
2024-04-29 23:52:26 +02:00
|
|
|
"agent_replied": partial(replied_body, "agent", "replied to"),
|
|
|
|
"customer_replied": partial(replied_body, "customer", "replied to"),
|
|
|
|
"note_added": partial(replied_body, "agent", "left a note on"),
|
2021-07-16 11:40:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ALL_EVENT_TYPES = list(EVENTS_FUNCTION_MAPPER.keys())
|
|
|
|
|
|
|
|
|
|
|
|
@webhook_view("Groove", all_event_types=ALL_EVENT_TYPES)
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_groove_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2022-05-12 06:54:12 +02:00
|
|
|
event = validate_extract_webhook_http_header(request, "X-Groove-Event", "Groove")
|
2021-02-16 00:17:17 +01:00
|
|
|
handler = EVENTS_FUNCTION_MAPPER.get(event)
|
|
|
|
if handler is None:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event)
|
2019-04-18 22:23:36 +02:00
|
|
|
|
|
|
|
body = handler(payload)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = "notifications"
|
2018-04-24 20:54:13 +02:00
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
if body is not None:
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic_name, body, event)
|
2018-01-07 12:05:44 +01:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2018-01-07 12:05:44 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-04 20:56:14 +02:00
|
|
|
fixture_to_headers = get_http_headers_from_filename("HTTP_X_GROOVE_EVENT")
|