From 645efa5ce4ea46b0211b3a41bc43067362b60cde Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?= <cuu508@gmail.com>
Date: Mon, 16 Dec 2024 15:20:45 +0200
Subject: [PATCH] Update Telegram notification template to include failure
 reason

cc: #1069
---
 hc/api/tests/test_notify_email.py                   |  1 -
 hc/api/tests/test_notify_matrix.py                  |  1 -
 hc/api/tests/test_notify_telegram.py                | 13 ++++++++++++-
 hc/api/transports.py                                |  1 +
 .../integrations/matrix_description_formatted.html  |  2 +-
 templates/integrations/telegram_message.html        |  2 +-
 6 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/hc/api/tests/test_notify_email.py b/hc/api/tests/test_notify_email.py
index fad6df92..d851a74e 100644
--- a/hc/api/tests/test_notify_email.py
+++ b/hc/api/tests/test_notify_email.py
@@ -121,7 +121,6 @@ class NotifyEmailTestCase(BaseTestCase):
     @override_settings(DEFAULT_FROM_EMAIL="alerts@example.org")
     def test_it_handles_reason_failure(self) -> None:
         self.flip.reason = "fail"
-        self.flip.save()
         self.channel.notify(self.flip)
 
         email = mail.outbox[0]
diff --git a/hc/api/tests/test_notify_matrix.py b/hc/api/tests/test_notify_matrix.py
index e6771583..ec9ec32f 100644
--- a/hc/api/tests/test_notify_matrix.py
+++ b/hc/api/tests/test_notify_matrix.py
@@ -68,7 +68,6 @@ class NotifyMatrixTestCase(BaseTestCase):
         mock_post.return_value.status_code = 200
 
         self.flip.reason = "fail"
-        self.flip.save()
         self.channel.notify(self.flip)
         assert Notification.objects.count() == 1
 
diff --git a/hc/api/tests/test_notify_telegram.py b/hc/api/tests/test_notify_telegram.py
index 2881410e..48d04387 100644
--- a/hc/api/tests/test_notify_telegram.py
+++ b/hc/api/tests/test_notify_telegram.py
@@ -1,4 +1,3 @@
-
 from __future__ import annotations
 
 import json
@@ -40,6 +39,7 @@ class NotifyTelegramTestCase(BaseTestCase):
         self.flip.created = now()
         self.flip.old_status = "new"
         self.flip.new_status = "down"
+        self.flip.reason = "timeout"
 
     @patch("hc.api.transports.curl.request", autospec=True)
     def test_it_works(self, mock_post: Mock) -> None:
@@ -54,6 +54,7 @@ class NotifyTelegramTestCase(BaseTestCase):
         self.assertIn("The check", payload["text"])
         self.assertIn(">DB Backup</a>", payload["text"])
         self.assertIn(self.check.cloaked_url(), payload["text"])
+        self.assertIn("grace time passed", payload["text"])
 
         self.assertIn("<b>Project:</b> Alices Project\n", payload["text"])
         self.assertIn("<b>Tags:</b> foo, bar, baz\n", payload["text"])
@@ -65,6 +66,16 @@ class NotifyTelegramTestCase(BaseTestCase):
         # other checks:
         self.assertNotIn("All the other checks are up.", payload["text"])
 
+    @patch("hc.api.transports.curl.request", autospec=True)
+    def test_it_handles_reason_failure(self, mock_post: Mock) -> None:
+        mock_post.return_value.status_code = 200
+
+        self.flip.reason = "fail"
+        self.channel.notify(self.flip)
+
+        payload = mock_post.call_args.kwargs["json"]
+        self.assertIn("received a failure signal", payload["text"])
+
     @patch("hc.api.transports.curl.request", autospec=True)
     def test_it_shows_exitstatus(self, mock_post: Mock) -> None:
         mock_post.return_value.status_code = 200
diff --git a/hc/api/transports.py b/hc/api/transports.py
index 3a107b0c..9489d001 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -927,6 +927,7 @@ class Telegram(HttpTransport):
 
         ping = self.last_ping(flip)
         ctx = {
+            "flip": flip,
             "check": flip.owner,
             "status": flip.new_status,
             "down_checks": self.down_checks(flip.owner),
diff --git a/templates/integrations/matrix_description_formatted.html b/templates/integrations/matrix_description_formatted.html
index 5c1c9c81..030db3e0 100644
--- a/templates/integrations/matrix_description_formatted.html
+++ b/templates/integrations/matrix_description_formatted.html
@@ -1,5 +1,5 @@
 {% load humanize hc_extras %}
-{% if flip.new_status == "down" %}
+{% if status == "down" %}
     🔴 The check <a href="{{ check.cloaked_url }}">{{ check.name_then_code }}</a>
     is <b>DOWN</b>{% if flip.reason %} ({{ flip.reason_long }}){% endif %}.
 {% else %}
diff --git a/templates/integrations/telegram_message.html b/templates/integrations/telegram_message.html
index 8166660b..53f5cf7c 100644
--- a/templates/integrations/telegram_message.html
+++ b/templates/integrations/telegram_message.html
@@ -1,6 +1,6 @@
 {% load hc_extras humanize linemode %}{% linemode %}
 {% if status == "down" %}
-    {% line %}🔴 The check <a href="{{ check.cloaked_url }}">{{ check.name_then_code }}</a> is <b>DOWN</b>.{% endline %}
+    {% line %}🔴 The check <a href="{{ check.cloaked_url }}">{{ check.name_then_code }}</a> is <b>DOWN</b>{% if flip.reason %} ({{ flip.reason_long }}){% endif %}.{% endline %}
 {% else %}
     {% line %}🟢 The check <a href="{{ check.cloaked_url }}">{{ check.name_then_code }}</a> is now <b>UP</b>.{% endline %}
 {% endif %}