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
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
2019-02-02 23:53:55 +01: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_bool, check_int, check_string
|
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)
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_gosquared_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:
|
2018-11-20 22:18:53 +01:00
|
|
|
body = ""
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = ""
|
2018-11-20 22:18:53 +01:00
|
|
|
|
|
|
|
# Unfortunately, there is no other way to infer the event type
|
|
|
|
# than just inferring it from the payload's attributes
|
|
|
|
# Traffic spike/dip event
|
2022-10-08 20:22:11 +02:00
|
|
|
if "concurrents" in payload and "siteDetails" in payload:
|
|
|
|
domain_name = payload["siteDetails"]["domain"].tame(check_string)
|
|
|
|
user_num = payload["concurrents"].tame(check_int)
|
|
|
|
user_acc = payload["siteDetails"]["acct"].tame(check_string)
|
2021-02-12 08:20:45 +01:00
|
|
|
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
|
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = f"GoSquared - {domain_name}"
|
|
|
|
check_send_webhook_message(request, user_profile, topic_name, 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:
|
2023-06-19 16:34:52 +02:00
|
|
|
# Only support non-direct messages
|
2022-10-08 20:22:11 +02:00
|
|
|
if not payload["message"]["private"].tame(check_bool):
|
|
|
|
session_title = payload["message"]["session"]["title"].tame(check_string)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = f"Live chat session - {session_title}"
|
2018-11-20 22:18:53 +01:00
|
|
|
body = CHAT_MESSAGE_TEMPLATE.format(
|
2022-10-08 20:22:11 +02:00
|
|
|
status=payload["person"]["status"].tame(check_string),
|
|
|
|
name=payload["person"]["_anon"]["name"].tame(check_string),
|
|
|
|
content=payload["message"]["content"].tame(check_string),
|
2018-11-20 22:18:53 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic_name, body, "chat_message")
|
2018-11-20 22:18:53 +01:00
|
|
|
else:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError("unknown_event")
|
2018-11-20 22:18:53 +01:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|