webhooks.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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(
  34. title, monitor.get_detail_url(), message
  35. )
  36. section = MSTeamsSection(str(monitor.name), message)
  37. return send_webhook(recipient.url, subject, [attachment], [section])
  38. elif recipient.recipient_type == RecipientType.GOOGLE_CHAT:
  39. card = GoogleChatCard().construct_uptime_card(
  40. title=subject,
  41. subtitle=title,
  42. text=message,
  43. url=monitor.get_detail_url(),
  44. )
  45. return send_googlechat_webhook(recipient.url, [card])
  46. elif recipient.recipient_type == RecipientType.DISCORD:
  47. embed = DiscordEmbed(
  48. title=title,
  49. description=message,
  50. color=None,
  51. fields=[],
  52. url=monitor.get_detail_url(),
  53. )
  54. return send_discord_webhook(recipient.url, subject, [embed])