webhooks.py 1.7 KB

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