2018-02-08 19:46:15 +01:00
|
|
|
from typing import Any, Dict, Optional, Text, Tuple
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_error, json_success
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2018-02-08 19:46:15 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2018-03-12 18:46:42 +01:00
|
|
|
def get_message_data(payload: Dict[Text, Any]) -> Tuple[Text, Text, Text, Text]:
|
|
|
|
link = "https://app.frontapp.com/open/" + payload['target']['data']['id']
|
|
|
|
outbox = payload['conversation']['recipient']['handle']
|
|
|
|
inbox = payload['source']['data'][0]['address']
|
|
|
|
subject = payload['conversation']['subject']
|
2018-02-08 19:46:15 +01:00
|
|
|
return link, outbox, inbox, subject
|
|
|
|
|
2018-03-12 18:46:42 +01:00
|
|
|
def get_source_name(payload: Dict[Text, Any]) -> Text:
|
|
|
|
first_name = payload['source']['data']['first_name']
|
|
|
|
last_name = payload['source']['data']['last_name']
|
2018-02-08 19:46:15 +01:00
|
|
|
return "%s %s" % (first_name, last_name)
|
|
|
|
|
2018-03-12 18:46:42 +01:00
|
|
|
def get_target_name(payload: Dict[Text, Any]) -> Text:
|
|
|
|
first_name = payload['target']['data']['first_name']
|
|
|
|
last_name = payload['target']['data']['last_name']
|
2018-02-08 19:46:15 +01:00
|
|
|
return "%s %s" % (first_name, last_name)
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Front')
|
|
|
|
@has_request_variables
|
|
|
|
def api_front_webhook(request: HttpRequest, user_profile: UserProfile,
|
2018-03-16 22:53:50 +01:00
|
|
|
payload: Dict[Text, Any]=REQ(argument_type='body')) -> HttpResponse:
|
2018-03-12 18:46:42 +01:00
|
|
|
|
|
|
|
event_type = payload['type']
|
|
|
|
conversation_id = payload['conversation']['id']
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
# Each topic corresponds to a separate conversation in Front.
|
|
|
|
topic = conversation_id
|
|
|
|
|
|
|
|
# Inbound message
|
|
|
|
if event_type == 'inbound':
|
2018-03-12 18:46:42 +01:00
|
|
|
link, outbox, inbox, subject = get_message_data(payload)
|
2018-02-08 19:46:15 +01:00
|
|
|
body = "[Inbound message]({link}) from **{outbox}** to **{inbox}**.\n" \
|
|
|
|
"```quote\n*Subject*: {subject}\n```" \
|
|
|
|
.format(link=link, outbox=outbox, inbox=inbox, subject=subject)
|
|
|
|
|
|
|
|
# Outbound message
|
|
|
|
elif event_type == 'outbound':
|
2018-03-12 18:46:42 +01:00
|
|
|
link, outbox, inbox, subject = get_message_data(payload)
|
2018-02-08 19:46:15 +01:00
|
|
|
body = "[Outbound message]({link}) from **{inbox}** to **{outbox}**.\n" \
|
|
|
|
"```quote\n*Subject*: {subject}\n```" \
|
|
|
|
.format(link=link, inbox=inbox, outbox=outbox, subject=subject)
|
|
|
|
|
|
|
|
# Outbound reply
|
|
|
|
elif event_type == 'out_reply':
|
2018-03-12 18:46:42 +01:00
|
|
|
link, outbox, inbox, subject = get_message_data(payload)
|
2018-02-08 19:46:15 +01:00
|
|
|
body = "[Outbound reply]({link}) from **{inbox}** to **{outbox}**." \
|
|
|
|
.format(link=link, inbox=inbox, outbox=outbox)
|
|
|
|
|
|
|
|
# Comment or mention
|
|
|
|
elif event_type == 'comment' or event_type == 'mention':
|
2018-03-12 18:46:42 +01:00
|
|
|
name = get_source_name(payload)
|
|
|
|
comment = payload['target']['data']['body']
|
2018-02-08 19:46:15 +01:00
|
|
|
body = "**{name}** left a comment:\n```quote\n{comment}\n```" \
|
|
|
|
.format(name=name, comment=comment)
|
|
|
|
|
|
|
|
# Conversation assigned
|
|
|
|
elif event_type == 'assign':
|
|
|
|
source_name = get_source_name(payload)
|
|
|
|
target_name = get_target_name(payload)
|
|
|
|
|
|
|
|
if source_name == target_name:
|
|
|
|
body = "**{source_name}** assigned themselves." \
|
|
|
|
.format(source_name=source_name)
|
|
|
|
else:
|
|
|
|
body = "**{source_name}** assigned **{target_name}**." \
|
|
|
|
.format(source_name=source_name, target_name=target_name)
|
|
|
|
|
|
|
|
# Conversation unassigned
|
|
|
|
elif event_type == 'unassign':
|
|
|
|
name = get_source_name(payload)
|
|
|
|
body = "Unassined by **{name}**.".format(name=name)
|
|
|
|
|
|
|
|
# Conversation archived
|
|
|
|
elif event_type == 'archive':
|
|
|
|
name = get_source_name(payload)
|
|
|
|
body = "Archived by **{name}**.".format(name=name)
|
|
|
|
|
|
|
|
# Conversation reopened
|
|
|
|
elif event_type == 'reopen':
|
|
|
|
name = get_source_name(payload)
|
|
|
|
body = "Reopened by **{name}**.".format(name=name)
|
|
|
|
|
|
|
|
# Conversation deleted
|
|
|
|
elif event_type == 'trash':
|
|
|
|
name = get_source_name(payload)
|
|
|
|
body = "Deleted by **{name}**.".format(name=name)
|
|
|
|
|
|
|
|
# Conversation restored
|
|
|
|
elif event_type == 'restore':
|
|
|
|
name = get_source_name(payload)
|
|
|
|
body = "Restored by **{name}**.".format(name=name)
|
|
|
|
|
|
|
|
# Conversation tagged
|
|
|
|
elif event_type == 'tag':
|
2018-03-12 18:46:42 +01:00
|
|
|
name = get_source_name(payload)
|
|
|
|
tag = payload['target']['data']['name']
|
2018-02-08 19:46:15 +01:00
|
|
|
body = "**{name}** added tag **{tag}**.".format(name=name, tag=tag)
|
|
|
|
|
|
|
|
# Conversation untagged
|
|
|
|
elif event_type == 'untag':
|
2018-03-12 18:46:42 +01:00
|
|
|
name = get_source_name(payload)
|
|
|
|
tag = payload['target']['data']['name']
|
2018-02-08 19:46:15 +01:00
|
|
|
body = "**{name}** removed tag **{tag}**.".format(name=name, tag=tag)
|
|
|
|
else:
|
|
|
|
return json_error(_("Unknown webhook request"))
|
|
|
|
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
return json_success()
|