40 lines
789 B
Go
40 lines
789 B
Go
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)
|
|
})
|
|
}
|