webhooks.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from datetime import datetime
  2. from alerts.constants import RecipientType
  3. from alerts.models import AlertRecipient
  4. from alerts.webhooks import (
  5. DiscordEmbed,
  6. MSTeamsSection,
  7. WebhookAttachment,
  8. send_discord_webhook,
  9. send_webhook,
  10. )
  11. from .models import MonitorCheck
  12. def send_uptime_as_webhook(
  13. recipient: AlertRecipient,
  14. monitor_check_id: int,
  15. went_down: bool,
  16. last_change: datetime,
  17. ):
  18. """
  19. Notification about uptime event via webhook.
  20. """
  21. monitor_check = MonitorCheck.objects.get(pk=monitor_check_id)
  22. monitor = monitor_check.monitor
  23. message = (
  24. "The monitored site has gone down."
  25. if went_down
  26. else "The monitored site is back up."
  27. )
  28. subject = "GlitchTip Uptime Alert"
  29. if recipient.recipient_type == RecipientType.GENERAL_WEBHOOK:
  30. attachment = WebhookAttachment(
  31. str(monitor.name), monitor.get_detail_url(), message
  32. )
  33. section = MSTeamsSection(str(monitor.name), message)
  34. return send_webhook(recipient.url, subject, [attachment], [section])
  35. elif recipient.recipient_type == RecipientType.DISCORD:
  36. embed = DiscordEmbed(
  37. title=monitor,
  38. description=message,
  39. color=None,
  40. fields=[],
  41. url=monitor.get_detail_url(),
  42. )
  43. return send_discord_webhook(recipient.url, subject, [embed])