Use HTML formatting for Matrix messages

This commit is contained in:
Timothée Floure 2020-05-03 10:23:05 +02:00
parent fd35273ed4
commit 8c8a5a83cb

20
main.go
View file

@ -26,8 +26,18 @@ type Configuration struct {
HTTPToken string HTTPToken string
} }
func generateMatrixMessageBody(alert template.Alert) string { func getMatrixMessageFor(alert template.Alert) gomatrix.HTMLMessage {
return fmt.Sprintf("**%v** %v.", alert.Status, alert.Annotations["summary"]) var prefix string
switch alert.Status {
case "firing":
prefix = "<strong><font color=\"#ff0000\">FIRING</font></strong> "
case "resolved":
prefix = "<strong><font color=\"#33cc33\">RESOLVED</font></strong> "
default:
prefix = fmt.Sprintf("<strong>%v</strong> ", alert.Status)
}
return gomatrix.GetHTMLMessage("m.text", prefix + alert.Annotations["summary"])
} }
func getMatrixClient(homeserver string, user string, token string, targetRoomID string) *gomatrix.Client { 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) logger.Printf("Received valid hook from %v", r.RemoteAddr)
for _, alert := range payload.Alerts { for _, alert := range payload.Alerts {
body := generateMatrixMessageBody(alert) msg := getMatrixMessageFor(alert)
logger.Printf("> %v", body) logger.Printf("> %v", msg.Body)
_, err := matrixClient.SendText(targetRoomID, body) _, err := matrixClient.SendMessageEvent(targetRoomID, "m.room.message", msg)
if err != nil { if err != nil {
logger.Printf(">> Could not forward to Matrix: %v", err) logger.Printf(">> Could not forward to Matrix: %v", err)
} }