webhooks.py 1.8 KB

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