-
Notifications
You must be signed in to change notification settings - Fork 0
/
dingding.go
166 lines (142 loc) · 3.9 KB
/
dingding.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 dingding
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/tristan-club/kit/config"
"github.com/tristan-club/kit/log"
"io/ioutil"
"net/http"
"net/url"
"time"
)
var defaultBot *Robot
type Robot struct {
token, secret, appkey, secretKey string
}
func init() {
token, secret := config.GetDingConfig()
if len(token) != 0 || len(secret) != 0 {
defaultBot = NewRobot(token, secret, "", "")
}
}
func NewRobot(token, secret, appKey, secretKey string) *Robot {
return &Robot{
token: token,
secret: secret,
appkey: appKey,
secretKey: secretKey,
}
}
func Default() *Robot {
if defaultBot != nil {
return defaultBot
}
log.Error().Msgf("dingding bot config error")
return &Robot{}
}
func sign(t int64, secret string) string {
strToHash := fmt.Sprintf("%d\n%s", t, secret)
hmac256 := hmac.New(sha256.New, []byte(secret))
hmac256.Write([]byte(strToHash))
data := hmac256.Sum(nil)
return base64.StdEncoding.EncodeToString(data)
}
func (robot *Robot) SendMessage(msg interface{}) error {
if config.IgnoreDingMsg() {
return nil
}
body := bytes.NewBuffer(nil)
err := json.NewEncoder(body).Encode(msg)
if err != nil {
return fmt.Errorf("msg json failed, msg: %v, err: %v", msg, err.Error())
}
value := url.Values{}
value.Set("access_token", robot.token)
if robot.secret != "" {
t := time.Now().UnixNano() / 1e6
value.Set("timestamp", fmt.Sprintf("%d", t))
value.Set("sign", sign(t, robot.secret))
}
request, err := http.NewRequest(http.MethodPost, "https://oapi.dingtalk.com/robot/send", body)
if err != nil {
return fmt.Errorf("error request: %v", err.Error())
}
request.URL.RawQuery = value.Encode()
request.Header.Add("Content-Type", "application/json;charset=utf-8")
res, err := (&http.Client{}).Do(request)
if err != nil {
return fmt.Errorf("send dingTalk message failed, error: %v", err.Error())
}
defer func() { _ = res.Body.Close() }()
result, err := ioutil.ReadAll(res.Body)
if res.StatusCode != 200 {
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, "http code is not 200"))
}
if err != nil {
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, err.Error()))
}
type response struct {
ErrCode int `json:"errcode"`
}
var ret response
if err := json.Unmarshal(result, &ret); err != nil {
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, err.Error()))
}
if ret.ErrCode != 0 {
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, "errcode is not 0"))
}
return nil
}
func httpError(request *http.Request, response *http.Response, body []byte, error string) string {
return fmt.Sprintf(
"http request failure, error: %s, status code: %d, %s %s, body:\n%s",
error,
response.StatusCode,
request.Method,
request.URL.String(),
string(body),
)
}
func (robot *Robot) SendTextMessage(content string, atMobiles []string, isAtAll bool) error {
msg := map[string]interface{}{
"msgtype": "text",
"text": map[string]string{
"content": content,
},
"at": map[string]interface{}{
"atMobiles": atMobiles,
"isAtAll": isAtAll,
},
}
return robot.SendMessage(msg)
}
func (robot *Robot) SendMarkdownMessage(title string, text string, atMobiles []string, isAtAll bool) error {
msg := map[string]interface{}{
"msgtype": "markdown",
"markdown": map[string]string{
"title": title,
"text": text,
},
"at": map[string]interface{}{
"atMobiles": atMobiles,
"isAtAll": isAtAll,
},
}
return robot.SendMessage(msg)
}
func (robot *Robot) SendLinkMessage(title string, text string, messageUrl string, picUrl string) error {
msg := map[string]interface{}{
"msgtype": "link",
"link": map[string]string{
"title": title,
"text": text,
"messageUrl": messageUrl,
"picUrl": picUrl,
},
}
return robot.SendMessage(msg)
}