package webhook import ( "bytes" "encoding/json" "log" "net/http" "maunium.net/go/mautrix/event" ) const ( messageTemplate = `@room Alerts for group "{{ .GroupKey }}":{{ range .Alerts }} * {{ .Annotations.Description }}{{ end }}` ) func (s Server) handleWebhook(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Content-Type") != "application/json" { writeStatus(w, http.StatusBadRequest) return } decoder := json.NewDecoder(r.Body) var payload Payload err := decoder.Decode(&payload) if err != nil { writeStatus(w, http.StatusBadRequest) return } if payload.Version != "4" { writeStatus(w, http.StatusBadRequest) return } if payload.Status != StatusFiring { writeStatus(w, http.StatusOK) return } var message bytes.Buffer err = s.tmpl.Execute(&message, payload) if err != nil { log.Print(err) writeStatus(w, http.StatusInternalServerError) return } evt := event.MessageEventContent{ MsgType: event.MsgText, Body: message.String(), } success := s.matrix.Broadcast(&evt) if !success { writeStatus(w, http.StatusInternalServerError) } }