2018-01-07 12:05:44 +01:00
|
|
|
# Webhooks for external integrations.
|
2019-04-18 22:23:36 +02:00
|
|
|
from functools import partial
|
|
|
|
from typing import Any, Dict, Optional, Callable
|
2018-01-07 12:05:44 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-04-18 22:23:36 +02:00
|
|
|
from zerver.lib.response import json_success
|
2018-04-24 20:54:13 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message, \
|
2019-07-04 20:56:14 +02:00
|
|
|
validate_extract_webhook_http_header, UnexpectedWebhookEventType, \
|
|
|
|
get_http_headers_from_filename
|
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()
|
|
|
|
|
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
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def ticket_assigned_body(payload: Dict[str, Any]) -> Optional[str]:
|
2018-01-07 12:05:44 +01:00
|
|
|
state = payload['state']
|
2019-04-18 22:23:36 +02:00
|
|
|
kwargs = {
|
|
|
|
'state': 'open' if state == 'opened' else state,
|
|
|
|
'number': payload['number'],
|
|
|
|
'title': payload['title'],
|
|
|
|
'app_url': payload['app_url']
|
|
|
|
}
|
|
|
|
|
2018-01-07 12:05:44 +01:00
|
|
|
assignee = payload['assignee']
|
|
|
|
assigned_group = payload['assigned_group']
|
|
|
|
|
|
|
|
if assignee or assigned_group:
|
|
|
|
if assignee and assigned_group:
|
2019-04-18 22:23:36 +02:00
|
|
|
kwargs['assignee_info'] = '{assignee} from {assigned_group}'.format(**payload)
|
2018-01-07 12:05:44 +01:00
|
|
|
elif assignee:
|
2019-04-18 22:23:36 +02:00
|
|
|
kwargs['assignee_info'] = '{assignee}'.format(**payload)
|
2018-01-07 12:05:44 +01:00
|
|
|
elif assigned_group:
|
2019-04-18 22:23:36 +02:00
|
|
|
kwargs['assignee_info'] = '{assigned_group}'.format(**payload)
|
|
|
|
|
|
|
|
return TICKET_ASSIGNED_TEMPLATE.format(**kwargs)
|
2018-01-07 12:05:44 +01:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
def replied_body(payload: Dict[str, Any], actor: str, action: str) -> str:
|
|
|
|
actor_url = "http://api.groovehq.com/v1/{}/".format(actor + 's')
|
|
|
|
actor = payload['links']['author']['href'].split(actor_url)[1]
|
2018-01-07 12:05:44 +01:00
|
|
|
number = payload['links']['ticket']['href'].split("http://api.groovehq.com/v1/tickets/")[1]
|
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
body = AGENT_REPLIED_TEMPLATE.format(
|
|
|
|
actor=actor,
|
|
|
|
action=action,
|
|
|
|
number=number,
|
|
|
|
app_ticket_url=payload['app_ticket_url'],
|
|
|
|
plain_text_body=payload['plain_text_body']
|
|
|
|
)
|
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
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
def get_event_handler(event: str) -> Callable[..., str]:
|
|
|
|
# The main reason for this function existance is because of mypy
|
|
|
|
handler = EVENTS_FUNCTION_MAPPER.get(event) # type: Any
|
|
|
|
if handler is None:
|
|
|
|
raise UnexpectedWebhookEventType("Groove", event)
|
|
|
|
return handler
|
2018-01-07 12:05:44 +01:00
|
|
|
|
|
|
|
@api_key_only_webhook_view('Groove')
|
|
|
|
@has_request_variables
|
|
|
|
def api_groove_webhook(request: HttpRequest, user_profile: UserProfile,
|
2018-03-16 22:53:50 +01:00
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
2018-04-24 20:54:13 +02: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
|
|
|
|
handler = get_event_handler(event)
|
|
|
|
|
|
|
|
body = handler(payload)
|
|
|
|
topic = 'notifications'
|
2018-04-24 20:54:13 +02:00
|
|
|
|
2019-04-18 22:23:36 +02:00
|
|
|
if body is not None:
|
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2018-01-07 12:05:44 +01:00
|
|
|
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
EVENTS_FUNCTION_MAPPER = {
|
|
|
|
'ticket_started': ticket_started_body,
|
|
|
|
'ticket_assigned': ticket_assigned_body,
|
2019-04-18 22:23:36 +02:00
|
|
|
'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')
|
2018-01-07 12:05:44 +01:00
|
|
|
}
|
2019-07-04 20:56:14 +02:00
|
|
|
|
|
|
|
fixture_to_headers = get_http_headers_from_filename("HTTP_X_GROOVE_EVENT")
|