Files
ReSendit/internal/notify/ntfy.go
2026-03-26 14:01:08 +01:00

37 lines
709 B
Go

package notify
import (
"fmt"
"net/http"
"strings"
"time"
)
func Publish(ntfyURL, topic, title, message string) error {
ntfyURL = strings.TrimRight(ntfyURL, "/")
if ntfyURL == "" || topic == "" {
return nil // nothing configured
}
req, err := http.NewRequest("POST", ntfyURL+"/"+topic, strings.NewReader(message))
if err != nil {
return err
}
if title != "" {
req.Header.Set("Title", title)
}
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
client := &http.Client{Timeout: 3 * time.Second}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode >= 300 {
return fmt.Errorf("ntfy returned %s", res.Status)
}
return nil
}