-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
75 lines (63 loc) · 1.42 KB
/
notify.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package notifyxf
import (
"bytes"
"encoding/json"
"errors"
"log"
"net/http"
)
const (
url = "https://notifyxf.com/api/message"
)
type Notifier struct {
token string
handle string
parseMode string
redact bool
preview bool
}
// NewNotifier will create a new Notifier service
func NewNotifier(token string, opts ...Option) (*Notifier, error) {
if token == "" {
return nil, errors.New("`token` is empty")
}
n := &Notifier{token: token}
for _, opt := range opts {
opt(n)
}
return n, nil
}
// Notify will notify with given `message`
func (n *Notifier) Notify(message string) error {
if message == "" {
log.Fatalln(errors.New("`msg` is empty"))
}
dat := struct {
Token string `json:"access_token"`
Message string `json:"message"`
Handle string `json:"handle,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
Redact bool `json:"redact,omitempty"`
Preview bool `json:"disable_web_page_preview,omitempty"`
}{
Token: n.token,
Message: message,
Handle: n.handle,
ParseMode: n.parseMode,
Redact: n.redact,
Preview: n.preview,
}
payload := new(bytes.Buffer)
err := json.NewEncoder(payload).Encode(dat)
if err != nil {
return err
}
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
return nil
}