-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdingtalkrus.go
148 lines (127 loc) · 3.13 KB
/
dingtalkrus.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
package dingtalkrus
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"net/url"
"time"
)
var(
dtHost="https://oapi.dingtalk.com/robot/send"
)
type DingTalkHook struct {
Token string
Secret string
AcceptLevels []logrus.Level
}
type DingTalkResponse struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
}
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 NewHook(token string,secret string,levels []logrus.Level) *DingTalkHook {
return &DingTalkHook{
Token: token,
Secret: secret,
AcceptLevels: levels,
}
}
func (dh *DingTalkHook) Levels()[]logrus.Level {
if dh.AcceptLevels==nil{
return AllLevels
}
return dh.AcceptLevels
}
func (dh *DingTalkHook) Fire(entry *logrus.Entry) error {
b, err := json.Marshal(entry.Data)
if err != nil {
return errors.New("Marshal Fields to JSON error: " + err.Error())
}
// Filter message type
isMatch:= dingTalkMsgFilter(b)
if !isMatch{
return nil
}
body := ioutil.NopCloser(bytes.NewBuffer(b))
value:=url.Values{}
value.Set("access_token",dh.Token)
if dh.Secret!=""{
t := time.Now().UnixNano() / 1e6
value.Set("timestamp",fmt.Sprintf("%d",t))
value.Set("sign",sign(t,dh.Secret))
}
request,err:=http.NewRequest(http.MethodPost,dtHost,body)
if err!=nil{
return errors.New("Error request: " + err.Error())
}
request.URL.RawQuery=value.Encode()
request.Header.Set("Content-Type", "application/json; charset=utf-8")
client := http.Client{}
response,err:=client.Do(request)
if err != nil {
return errors.New("Send to DingTalk error: " + err.Error())
}
defer response.Body.Close()
rb, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.New("Read DingTalk response error: " + err.Error())
}
dr := &DingTalkResponse{}
err = json.Unmarshal(rb, dr)
if err != nil {
return errors.New("Unmarshal DingTalk response to JSON error: " + err.Error())
}
if dr.ErrCode != 0 {
return errors.New("DingTalk return error message: " + dr.ErrMsg)
}
return nil
}
func SendTextMsg(content string, atMobiles []string, isAtAll bool) logrus.Fields {
return map[string]interface{}{
"msgtype":"text",
"text":map[string]string{
"content":content,
},
"at": map[string]interface{}{
"atMobiles": atMobiles,
"isAtAll": isAtAll,
},
}
}
func SendMarkdownMsg(title string, text string, atMobiles []string, isAtAll bool)logrus.Fields {
return map[string]interface{}{
"msgtype": "markdown",
"markdown": map[string]string{
"title": title,
"text": text,
},
"at": map[string]interface{}{
"atMobiles": atMobiles,
"isAtAll": isAtAll,
},
}
}
func SendLinkMsg(title string, text string, messageUrl string, picUrl string) logrus.Fields {
return map[string]interface{}{
"msgtype": "link",
"link": map[string]string{
"title": title,
"text": text,
"messageUrl": messageUrl,
"picUrl": picUrl,
},
}
}