-
Notifications
You must be signed in to change notification settings - Fork 25
/
ntfy.go
37 lines (33 loc) · 773 Bytes
/
ntfy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"context"
"net/http"
"strings"
"github.com/carlmjohnson/requests"
"github.com/samber/lo"
)
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
}
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())
}