func (s *Service) Register(oldService types.Service, client types.MatrixClient) error {
	s.WebhookURL = s.webhookEndpointURL
	for _, templates := range s.Rooms {
		if templates.TextTemplate == "" {
			return fmt.Errorf("plain text template missing")
		}
		_, err := text.New("textTemplate").Parse(templates.TextTemplate)
		if err != nil {
			return fmt.Errorf("plain text template is invalid: %v", err)
		}
		if templates.HTMLTemplate != "" {
			_, err := html.New("htmlTemplate").Parse(templates.HTMLTemplate)
			if err != nil {
				return fmt.Errorf("html template is invalid: %v", err)
			}
		}
		if templates.MsgType != "m.notice" && templates.MsgType != "m.text" {
			return fmt.Errorf("msg_type is neither 'm.notice' nor 'm.text'")
		}
	}
	s.joinRooms(client)
	return nil
}
Register makes sure the Config information supplied is valid.
package alertmanager
import (
	"bytes"
	"encoding/json"
	"fmt"
	html "html/template"
	"net/http"
	"strings"
	text "text/template"
	"github.com/matrix-org/go-neb/database"
	"github.com/matrix-org/go-neb/types"
	log "github.com/sirupsen/logrus"
	mevt "maunium.net/go/mautrix/event"
	"maunium.net/go/mautrix/id"
)
const ServiceType = "alertmanager"
type Service struct {
	types.DefaultService
	webhookEndpointURL string
	WebhookURL string `json:"webhook_url"`
	Rooms map[id.RoomID]struct {
		TextTemplate string           `json:"text_template"`
		HTMLTemplate string           `json:"html_template"`
		MsgType      mevt.MessageType `json:"msg_type"`
	} `json:"rooms"`
}
type WebhookNotification struct {
	Version           string            `json:"version"`
	GroupKey          string            `json:"groupKey"`
	Status            string            `json:"status"`
	Receiver          string            `json:"receiver"`
	GroupLabels       map[string]string `json:"groupLabels"`
	CommonLabels      map[string]string `json:"commonLabels"`
	CommonAnnotations map[string]string `json:"commonAnnotations"`
	ExternalURL       string            `json:"externalURL"`
	Alerts            []struct {
		Status       string            `json:"status"`
		Labels       map[string]string `json:"labels"`
		Annotations  map[string]string `json:"annotations"`
		StartsAt     string            `json:"startsAt"`
		EndsAt       string            `json:"endsAt"`
		GeneratorURL string            `json:"generatorURL"`
		SilenceURL   string
	} `json:"alerts"`
}
func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli types.MatrixClient) {
	decoder := json.NewDecoder(req.Body)
	var notif WebhookNotification
	if err := decoder.Decode(&notif); err != nil {
		log.WithError(err).Error("Alertmanager webhook received an invalid JSON payload")
		w.WriteHeader(400)
		return
	}
	for i := range notif.Alerts {
		alert := &notif.Alerts[i]
		filters := []string{}
		for label, val := range alert.Labels {
			filters = append(filters, fmt.Sprintf("%s%%3D\"%s\"", label, val))
		}
		alert.SilenceURL = fmt.Sprintf("%s#silences/new?filter={%s}", notif.ExternalURL, strings.Join(filters, ","))
	}
	for roomID, templates := range s.Rooms {
		var msg interface{}
		textTemplate, _ := text.New("textTemplate").Parse(templates.TextTemplate)
		var bodyBuffer bytes.Buffer
		if err := textTemplate.Execute(&bodyBuffer, notif); err != nil {
			log.WithError(err).Error("Alertmanager webhook failed to execute text template")
			w.WriteHeader(500)
			return
		}
		if templates.HTMLTemplate != "" {
			htmlTemplate, _ := html.New("htmlTemplate").Parse(templates.HTMLTemplate)
			var formattedBodyBuffer bytes.Buffer
			if err := htmlTemplate.Execute(&formattedBodyBuffer, notif); err != nil {
				log.WithError(err).Error("Alertmanager webhook failed to execute HTML template")
				w.WriteHeader(500)
				return
			}
			msg = mevt.MessageEventContent{
				Body:          bodyBuffer.String(),
				MsgType:       templates.MsgType,
				Format:        mevt.FormatHTML,
				FormattedBody: formattedBodyBuffer.String(),
			}
		} else {
			msg = mevt.MessageEventContent{
				Body:    bodyBuffer.String(),
				MsgType: templates.MsgType,
			}
		}
		log.WithFields(log.Fields{
			"message": msg,
			"room_id": roomID,
		}).Print("Sending Alertmanager notification to room")
		if _, e := cli.SendMessageEvent(roomID, mevt.EventMessage, msg); e != nil {
			log.WithError(e).WithField("room_id", roomID).Print(
				"Failed to send Alertmanager notification to room.")
		}
	}
	w.WriteHeader(200)
}