1
mirror of https://github.com/jlelse/GoBlog synced 2024-07-15 23:52:58 +00:00
GoBlog/ntfy.go

35 lines
743 B
Go

package main
import (
"context"
"net/http"
"strings"
"github.com/carlmjohnson/requests"
"github.com/samber/lo"
)
func (ntfy *configNtfy) enabled() bool {
return ntfy != nil && ntfy.Enabled && ntfy.Topic != ""
}
func (a *goBlog) sendNtfy(cfg *configNtfy, msg string) error {
if !cfg.enabled() {
return nil
}
topic := strings.TrimPrefix(cfg.Topic, "ntfy.sh/")
server, _ := lo.Coalesce(cfg.Server, "https://ntfy.sh")
builder := requests.
URL(server + "/" + topic).
Client(a.httpClient).
Method(http.MethodPost).
BodyReader(strings.NewReader(msg))
if cfg.User != "" {
builder.BasicAuth(cfg.User, cfg.Pass)
}
if cfg.Email != "" {
builder.Header("X-Email", cfg.Email)
}
return builder.Fetch(context.Background())
}