2021-12-29 06:09:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-04 08:48:37 +00:00
|
|
|
"net/http"
|
2021-12-29 06:09:42 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/carlmjohnson/requests"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ntfy *configNtfy) enabled() bool {
|
|
|
|
if ntfy == nil || !ntfy.Enabled || ntfy.Topic == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) sendNtfy(cfg *configNtfy, msg string) error {
|
|
|
|
if !cfg.enabled() {
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-04 11:07:36 +00:00
|
|
|
topic := cfg.Topic
|
|
|
|
if strings.HasPrefix(topic, "ntfy.sh/") { // Old configuration example
|
|
|
|
topic = strings.TrimPrefix(topic, "ntfy.sh/")
|
|
|
|
}
|
|
|
|
server := "https://ntfy.sh"
|
|
|
|
if cfg.Server != "" {
|
|
|
|
server = cfg.Server
|
|
|
|
}
|
|
|
|
builder := requests.
|
|
|
|
URL(server + "/" + topic).
|
2021-12-29 06:09:42 +00:00
|
|
|
Client(a.httpClient).
|
|
|
|
UserAgent(appUserAgent).
|
2022-01-04 08:48:37 +00:00
|
|
|
Method(http.MethodPost).
|
2022-04-04 11:07:36 +00:00
|
|
|
BodyReader(strings.NewReader(msg))
|
|
|
|
if cfg.User != "" {
|
|
|
|
builder.BasicAuth(cfg.User, cfg.Pass)
|
|
|
|
}
|
|
|
|
return builder.Fetch(context.Background())
|
2021-12-29 06:09:42 +00:00
|
|
|
}
|