~xenrox/ntfy-alertmanager

bd1abedac1b72ba1b049d2f4b6520c2ae00c8292 — Thorben Günther 30 days ago 84a45a9
Allow setting topic through URL query parameters

Closes: https://codeberg.org/xenrox/ntfy-alertmanager/issues/2
2 files changed, 13 insertions(+), 4 deletions(-)

M config.scfg
M main.go
M config.scfg => config.scfg +2 -0
@@ 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

M main.go => main.go +11 -4
@@ 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()))