-
Notifications
You must be signed in to change notification settings - Fork 0
/
ding.go
166 lines (153 loc) · 3.57 KB
/
ding.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package ding
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"time"
)
type Webhook struct {
url string
secret string
}
func NewWebhook(webhook, secret string) *Webhook {
return &Webhook{
url: webhook,
secret: secret,
}
}
type response struct {
Code int `json:"errcode"`
Msg string `json:"errmsg"`
}
// SendMessageText Function to send message
//
//goland:noinspection GoUnhandledErrorResult
func (t *Webhook) SendMessageText(text string, at ...string) error {
msg := map[string]interface{}{
"msgtype": "text",
"text": map[string]string{
"content": text,
},
}
if len(at) == 0 {
} else if len(at) == 1 {
if at[0] == "*" { // at all
msg["at"] = map[string]interface{}{
"isAtAll": true,
}
} else { // at specific user
re := regexp.MustCompile(`^\+*\d{10,15}$`)
if re.MatchString(at[0]) {
msg["at"] = map[string]interface{}{
"atMobiles": at,
"isAtAll": false,
}
} else {
return errors.New(`parameter error, "at" parameter must be in "*" or mobile phone number format`)
}
}
} else {
re := regexp.MustCompile(`^\+*\d{10,15}$`)
for _, v := range at {
if !re.MatchString(v) {
return errors.New(`parameter error, "at" parameter must be in "*" or mobile phone number format`)
}
}
msg["at"] = map[string]interface{}{
"atMobiles": at,
"isAtAll": false,
}
}
b, err := json.Marshal(msg)
if err != nil {
return err
}
resp, err := http.Post(t.getURL(), "application/json", bytes.NewBuffer(b))
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var r response
err = json.Unmarshal(body, &r)
if err != nil {
return err
}
if r.Code != 0 {
return errors.New(fmt.Sprintf("response error: %s", string(body)))
}
return err
}
//goland:noinspection GoUnhandledErrorResult
func (t *Webhook) sendMessageMarkdown(title, text string, at ...string) error {
msg := map[string]any{
"msgtype": "markdown",
"markdown": map[string]string{
"title": title,
"text": text,
},
}
if len(at) == 0 {
} else if len(at) == 1 {
if at[0] == "*" { // at all
msg["at"] = map[string]interface{}{
"isAtAll": true,
}
} else { // at specific user
re := regexp.MustCompile(`^\+*\d{10,15}$`)
if re.MatchString(at[0]) {
msg["at"] = map[string]interface{}{
"atMobiles": at,
"isAtAll": false,
}
} else {
return errors.New(`parameter error, "at" parameter must be in "*" or mobile phone number format`)
}
}
} else {
re := regexp.MustCompile(`^\+*\d{10,15}$`)
for _, v := range at {
if !re.MatchString(v) {
return errors.New(`parameter error, "at" parameter must be in "*" or mobile phone number format`)
}
}
msg["at"] = map[string]interface{}{
"atMobiles": at,
"isAtAll": false,
}
}
b, err := json.Marshal(msg)
if err != nil {
return err
}
resp, err := http.Post(t.getURL(), "application/json", bytes.NewBuffer(b))
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.ReadAll(resp.Body)
return err
}
func (t *Webhook) hmacSha256(stringToSign string, secret string) string {
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(stringToSign))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func (t *Webhook) getURL() string {
wh := t.url
timestamp := time.Now().UnixNano() / 1e6
stringToSign := fmt.Sprintf("%d\n%s", timestamp, t.secret)
sign := t.hmacSha256(stringToSign, t.secret)
url := fmt.Sprintf("%s×tamp=%d&sign=%s", wh, timestamp, sign)
return url
}