2018-01-07 12:05:44 +01:00
|
|
|
# Webhooks for external integrations.
|
2019-04-18 22:23:36 +02:00
|
|
|
from functools import partial
|
2020-01-14 22:06:24 +01:00
|
|
|
from typing import Any, Callable, Dict, 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
|
2020-08-19 22:26:38 +02:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventType
|
2018-01-07 12:05:44 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-04-18 22:23:36 +02:00
|
|
|
from zerver.lib.response import json_success
|
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
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def ticket_started_body(payload: Dict[str, Any]) -> str:
|
2019-04-18 22:23:36 +02:00
|
|
|
return TICKET_STARTED_TEMPLATE.format(**payload)
|
2018-01-07 12:05:44 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def ticket_assigned_body(payload: Dict[str, Any]) -> Optional[str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
state = payload["state"]
|
2019-04-18 22:23:36 +02:00
|
|
|
kwargs = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"state": "open" if state == "opened" else state,
|
|
|
|
"number": payload["number"],
|
|
|
|
"title": payload["title"],
|
|
|
|
"app_url": payload["app_url"],
|
2019-04-18 22:23:36 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee = payload["assignee"]
|
|
|
|
assigned_group = payload["assigned_group"]
|
2018-01-07 12:05:44 +01:00
|
|
|
|
|
|
|
if assignee or assigned_group:
|
|
|
|
if assignee and assigned_group:
|
2021-02-12 08:20:45 +01:00
|
|
|
kwargs["assignee_info"] = "{assignee} from {assigned_group}".format(**payload)
|
2018-01-07 12:05:44 +01:00
|
|
|
elif assignee:
|
2021-02-12 08:20:45 +01:00
|
|
|
kwargs["assignee_info"] = "{assignee}".format(**payload)
|
2018-01-07 12:05:44 +01:00
|
|
|
elif assigned_group:
|
2021-02-12 08:20:45 +01:00
|
|
|
kwargs["assignee_info"] = "{assigned_group}".format(**payload)
|
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
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
def replied_body(payload: Dict[str, Any], actor: str, action: str) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
actor_url = "http://api.groovehq.com/v1/{}/".format(actor + "s")
|
|
|
|
actor = payload["links"]["author"]["href"].split(actor_url)[1]
|
|
|
|
number = payload["links"]["ticket"]["href"].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,
|
2021-02-12 08:20:45 +01:00
|
|
|
app_ticket_url=payload["app_ticket_url"],
|
|
|
|
plain_text_body=payload["plain_text_body"],
|
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
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
EVENTS_FUNCTION_MAPPER: Dict[str, Callable[[Dict[str, Any]], Optional[str]]] = {
|
|
|
|
"ticket_started": ticket_started_body,
|
|
|
|
"ticket_assigned": ticket_assigned_body,
|
|
|
|
"agent_replied": partial(replied_body, actor="agent", action="replied to"),
|
|
|
|
"customer_replied": partial(replied_body, actor="customer", action="replied to"),
|
|
|
|
"note_added": partial(replied_body, actor="agent", action="left a note on"),
|
|
|
|
}
|
|
|
|
|
|
|
|
ALL_EVENT_TYPES = list(EVENTS_FUNCTION_MAPPER.keys())
|
|
|
|
|
|
|
|
|
|
|
|
@webhook_view("Groove", all_event_types=ALL_EVENT_TYPES)
|
2018-01-07 12:05:44 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_groove_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload: Dict[str, Any] = REQ(argument_type="body"),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
event = validate_extract_webhook_http_header(request, "X_GROOVE_EVENT", "Groove")
|
2019-04-18 22:23:36 +02:00
|
|
|
assert event is not None
|
2021-02-16 00:17:17 +01:00
|
|
|
handler = EVENTS_FUNCTION_MAPPER.get(event)
|
|
|
|
if handler is None:
|
|
|
|
raise UnsupportedWebhookEventType(event)
|
2019-04-18 22:23:36 +02:00
|
|
|
|
|
|
|
body = handler(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = "notifications"
|
2018-04-24 20:54:13 +02:00
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
if body is not None:
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, event)
|
2018-01-07 12:05:44 +01:00
|
|
|
|
|
|
|
return json_success()
|
|
|
|
|
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")
|