commit a4e36cdded2b3fbcb7939d25a51a09cf7a1394ba Author: Jeremy Baxter Date: Fri Mar 20 14:53:34 2026 +1300 initial commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..936ff83 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.baxters.nz/jeremy/ntfy + +go 1.25.7 diff --git a/ntfy.go b/ntfy.go new file mode 100644 index 0000000..efa50ec --- /dev/null +++ b/ntfy.go @@ -0,0 +1,40 @@ +package ntfy + +import ( + "errors" + "strings" + + "net/http" +) + +func Send(topic, title, message string, fn func(*http.Request)) (err error) { + req, err := http.NewRequest("POST", + "https://ntfy.sh/" + topic, strings.NewReader(message)) + if err != nil { + return + } + + req.Header.Set("Markdown", "true") + fn(req) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return + } + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return errors.New("Host returned " + resp.Status) + } + + return +} + +func SendActions(topic, title, message string, actions ...string) error { + var actionsString string + for _, a := range actions { + actionsString += "view," + a + ",clear=false;" + } + + return Send(topic, title, message, func(r *http.Request) { + r.Header.Set("Actions", actionsString) + }) +}