2019-02-02 23:53:55 +01:00
|
|
|
from typing import Any, Dict
|
2017-11-16 00:43:10 +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
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2020-08-19 22:14:40 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-12-18 02:08:22 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
TRAFFIC_SPIKE_TEMPLATE = "[{website_name}]({website_url}) has {user_num} visitors online."
|
2018-11-20 22:18:53 +01:00
|
|
|
CHAT_MESSAGE_TEMPLATE = """
|
|
|
|
The {status} **{name}** messaged:
|
|
|
|
|
|
|
|
``` quote
|
|
|
|
{content}
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
2016-12-18 02:08:22 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = ["chat_message", "traffic_spike"]
|
|
|
|
|
|
|
|
|
|
|
|
@webhook_view("GoSquared", all_event_types=ALL_EVENT_TYPES)
|
2016-12-18 02:08:22 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_gosquared_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload: Dict[str, Dict[str, Any]] = REQ(argument_type="body"),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-11-20 22:18:53 +01:00
|
|
|
body = ""
|
|
|
|
topic = ""
|
|
|
|
|
|
|
|
# Unfortunately, there is no other way to infer the event type
|
|
|
|
# than just inferring it from the payload's attributes
|
|
|
|
# Traffic spike/dip event
|
2021-02-12 08:20:45 +01:00
|
|
|
if payload.get("concurrents") is not None and payload.get("siteDetails") is not None:
|
|
|
|
domain_name = payload["siteDetails"]["domain"]
|
|
|
|
user_num = payload["concurrents"]
|
|
|
|
user_acc = payload["siteDetails"]["acct"]
|
|
|
|
acc_url = "https://www.gosquared.com/now/" + user_acc
|
2021-02-12 08:19:30 +01:00
|
|
|
body = TRAFFIC_SPIKE_TEMPLATE.format(
|
|
|
|
website_name=domain_name, website_url=acc_url, user_num=user_num
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = f"GoSquared - {domain_name}"
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, "traffic_spike")
|
2018-11-20 22:18:53 +01:00
|
|
|
|
|
|
|
# Live chat message event
|
2021-02-12 08:20:45 +01:00
|
|
|
elif payload.get("message") is not None and payload.get("person") is not None:
|
2018-11-20 22:18:53 +01:00
|
|
|
# Only support non-private messages
|
2021-02-12 08:20:45 +01:00
|
|
|
if not payload["message"]["private"]:
|
|
|
|
session_title = payload["message"]["session"]["title"]
|
2021-05-10 07:02:14 +02:00
|
|
|
topic = f"Live chat session - {session_title}"
|
2018-11-20 22:18:53 +01:00
|
|
|
body = CHAT_MESSAGE_TEMPLATE.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
status=payload["person"]["status"],
|
|
|
|
name=payload["person"]["_anon"]["name"],
|
|
|
|
content=payload["message"]["content"],
|
2018-11-20 22:18:53 +01:00
|
|
|
)
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, "chat_message")
|
2018-11-20 22:18:53 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
raise UnsupportedWebhookEventType("unknown_event")
|
2018-11-20 22:18:53 +01:00
|
|
|
|
2016-12-18 02:08:22 +01:00
|
|
|
return json_success()
|