37 lines
709 B
Go
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
|
|
}
|