package notify import ( "fmt" "net/http" "os" "strings" "time" ) func Publish(ntfyURL, topic, title, message string, clickUrl 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") domain := os.Getenv("DOMAIN") req.Header.Set("Click", fmt.Sprintf("%s%s", domain, clickUrl)) 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 }