From 8c8a5a83cb45d03367a835f23ad10d35f2357f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Sun, 3 May 2020 10:23:05 +0200 Subject: [PATCH] Use HTML formatting for Matrix messages --- main.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index a3c9af0..ab64cc6 100644 --- a/main.go +++ b/main.go @@ -26,8 +26,18 @@ type Configuration struct { HTTPToken string } -func generateMatrixMessageBody(alert template.Alert) string { - return fmt.Sprintf("**%v** %v.", alert.Status, alert.Annotations["summary"]) +func getMatrixMessageFor(alert template.Alert) gomatrix.HTMLMessage { + var prefix string + switch alert.Status { + case "firing": + prefix = "FIRING " + case "resolved": + prefix = "RESOLVED " + default: + prefix = fmt.Sprintf("%v ", alert.Status) + } + + return gomatrix.GetHTMLMessage("m.text", prefix + alert.Annotations["summary"]) } func getMatrixClient(homeserver string, user string, token string, targetRoomID string) *gomatrix.Client { @@ -78,9 +88,9 @@ func handleIncomingHooks( w http.ResponseWriter, r *http.Request, logger.Printf("Received valid hook from %v", r.RemoteAddr) for _, alert := range payload.Alerts { - body := generateMatrixMessageBody(alert) - logger.Printf("> %v", body) - _, err := matrixClient.SendText(targetRoomID, body) + msg := getMatrixMessageFor(alert) + logger.Printf("> %v", msg.Body) + _, err := matrixClient.SendMessageEvent(targetRoomID, "m.room.message", msg) if err != nil { logger.Printf(">> Could not forward to Matrix: %v", err) }