diff --git a/config.scfg b/config.scfg index 4763a63..976bede 100644 --- a/config.scfg +++ b/config.scfg @@ -69,6 +69,8 @@ ntfy { server https://ntfy.sh # Name of the ntfy topic. For backwards compatibility you can specify the full URL of the # topic (e.g. https://ntfy.sh/alertmanager-alerts) and the server will be parsed from it. + # Furthermore the topic name can be optionally set by using URL parameters with the webhook + # endpoint: https://ntfy-alertmanager.example.com/?topic=foobar # This setting is required. # Default: "" topic alertmanager-alerts diff --git a/main.go b/main.go index b1780f1..0922877 100644 --- a/main.go +++ b/main.go @@ -304,8 +304,13 @@ func (br *bridge) multiAlertNotification(p *payload) *notification { return n } -func (br *bridge) publish(n *notification) error { - url, err := br.topicURL(n.topic) +func (br *bridge) publish(n *notification, topicParam string) error { + // precedence: topicParam > n.topic > cfg.Ntfy.Topic + if topicParam == "" { + topicParam = n.topic + } + + url, err := br.topicURL(topicParam) if err != nil { return err } @@ -437,6 +442,8 @@ func (br *bridge) handleWebhooks(w http.ResponseWriter, r *http.Request) { return } + topicParam := r.URL.Query().Get("topic") + var event payload if err := json.NewDecoder(r.Body).Decode(&event); err != nil { http.Error(w, "Failed to parse payload", http.StatusInternalServerError) @@ -451,7 +458,7 @@ func (br *bridge) handleWebhooks(w http.ResponseWriter, r *http.Request) { if br.cfg.AlertMode == config.Single { notifications := br.singleAlertNotifications(&event) for _, n := range notifications { - err := br.publish(n) + err := br.publish(n, topicParam) if err != nil { logger.Error("Failed to publish notification", slog.String("error", err.Error())) @@ -465,7 +472,7 @@ func (br *bridge) handleWebhooks(w http.ResponseWriter, r *http.Request) { } } else { notification := br.multiAlertNotification(&event) - err := br.publish(notification) + err := br.publish(notification, topicParam) if err != nil { logger.Error("Failed to publish notification", slog.String("error", err.Error()))