2022-06-08 13:21:55 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2020-02-17 23:46:15 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2022-01-13 22:59:43 +01:00
|
|
|
from django.utils.translation import gettext as _
|
2020-02-17 23:46:15 +01:00
|
|
|
|
2022-04-14 23:50:10 +02:00
|
|
|
from zerver.actions.message_send import send_rate_limited_pm_notification_to_bot_owner
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.response import json_success
|
2020-02-17 23:46:15 +01:00
|
|
|
from zerver.lib.send_email import FromAddress
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import WebhookPayload, typed_endpoint
|
|
|
|
from zerver.lib.validator import WildValue, check_string
|
2020-02-17 23:46:15 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
MISCONFIGURED_PAYLOAD_ERROR_MESSAGE = """
|
2021-12-17 05:14:58 +01:00
|
|
|
Hi there! Your bot {bot_name} just received a UptimeRobot payload that is missing
|
2020-02-17 23:46:15 +01:00
|
|
|
some data that Zulip requires. This usually indicates a configuration issue
|
2021-12-17 05:14:58 +01:00
|
|
|
in your UptimeRobot webhook settings. Please make sure that you set the required parameters
|
|
|
|
when configuring the UptimeRobot webhook. Contact {support_email} if you
|
2020-02-17 23:46:15 +01:00
|
|
|
need further help!
|
|
|
|
"""
|
|
|
|
|
|
|
|
UPTIMEROBOT_TOPIC_TEMPLATE = "{monitor_friendly_name}"
|
|
|
|
UPTIMEROBOT_MESSAGE_UP_TEMPLATE = """
|
|
|
|
{monitor_friendly_name} ({monitor_url}) is back UP ({alert_details}).
|
|
|
|
It was down for {alert_friendly_duration}.
|
|
|
|
""".strip()
|
|
|
|
UPTIMEROBOT_MESSAGE_DOWN_TEMPLATE = (
|
|
|
|
"{monitor_friendly_name} ({monitor_url}) is DOWN ({alert_details})."
|
|
|
|
)
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = ["up", "down"]
|
2020-02-17 23:46:15 +01:00
|
|
|
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
@webhook_view("UptimeRobot", all_event_types=ALL_EVENT_TYPES)
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2020-02-17 23:46:15 +01:00
|
|
|
def api_uptimerobot_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
|
|
|
payload: WebhookPayload[WildValue],
|
2020-02-17 23:46:15 +01:00
|
|
|
) -> HttpResponse:
|
2022-06-08 13:21:55 +02:00
|
|
|
event_type = payload["alert_type_friendly_name"].tame(check_string)
|
|
|
|
if event_type == "Up":
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "up"
|
2022-06-08 13:21:55 +02:00
|
|
|
elif event_type == "Down":
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "down"
|
2020-02-17 23:46:15 +01:00
|
|
|
|
|
|
|
try:
|
2022-06-08 13:21:55 +02:00
|
|
|
body = get_body_for_http_request(payload, event_type)
|
2023-07-12 13:37:08 +02:00
|
|
|
topic = get_topic_for_http_request(payload)
|
2022-06-08 13:21:55 +02:00
|
|
|
except ValidationError:
|
2020-02-17 23:46:15 +01:00
|
|
|
message = MISCONFIGURED_PAYLOAD_ERROR_MESSAGE.format(
|
|
|
|
bot_name=user_profile.full_name,
|
|
|
|
support_email=FromAddress.SUPPORT,
|
|
|
|
).strip()
|
|
|
|
send_rate_limited_pm_notification_to_bot_owner(user_profile, user_profile.realm, message)
|
|
|
|
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Invalid payload"))
|
2020-02-17 23:46:15 +01:00
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, event)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-02-17 23:46:15 +01:00
|
|
|
|
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
def get_topic_for_http_request(payload: WildValue) -> str:
|
2022-06-08 13:21:55 +02:00
|
|
|
return UPTIMEROBOT_TOPIC_TEMPLATE.format(
|
|
|
|
monitor_friendly_name=payload["monitor_friendly_name"].tame(check_string)
|
|
|
|
)
|
2020-02-17 23:46:15 +01:00
|
|
|
|
|
|
|
|
2022-06-08 13:21:55 +02:00
|
|
|
def get_body_for_http_request(payload: WildValue, event_type: str) -> str:
|
|
|
|
if event_type == "Up":
|
|
|
|
monitor_friendly_name = payload["monitor_friendly_name"].tame(check_string)
|
|
|
|
monitor_url = payload["monitor_url"].tame(check_string)
|
|
|
|
alert_details = payload["alert_details"].tame(check_string)
|
|
|
|
alert_friendly_duration = payload["alert_friendly_duration"].tame(check_string)
|
|
|
|
body = UPTIMEROBOT_MESSAGE_UP_TEMPLATE.format(
|
|
|
|
monitor_friendly_name=monitor_friendly_name,
|
|
|
|
monitor_url=monitor_url,
|
|
|
|
alert_details=alert_details,
|
|
|
|
alert_friendly_duration=alert_friendly_duration,
|
|
|
|
)
|
|
|
|
elif event_type == "Down":
|
|
|
|
monitor_friendly_name = payload["monitor_friendly_name"].tame(check_string)
|
|
|
|
monitor_url = payload["monitor_url"].tame(check_string)
|
|
|
|
alert_details = payload["alert_details"].tame(check_string)
|
|
|
|
body = UPTIMEROBOT_MESSAGE_DOWN_TEMPLATE.format(
|
|
|
|
monitor_friendly_name=monitor_friendly_name,
|
|
|
|
monitor_url=monitor_url,
|
|
|
|
alert_details=alert_details,
|
|
|
|
)
|
2020-02-17 23:46:15 +01:00
|
|
|
|
|
|
|
return body
|