Skip to content

Commit cff3d83

Browse files
committed
支持 feishu 签名消息
1 parent 916983c commit cff3d83

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project_name:=supervisor-event-listener
2-
project_version:=1.2.1
2+
project_version:=1.2.2
33
root_dir := $(abspath $(CURDIR))
44
build_dir := $(root_dir)/build
55
GOPATH := ${HOME}/go

notify/feishu.go

+35-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9+
"net/url"
10+
"strings"
11+
"time"
912

1013
"github.com/ouqiang/supervisor-event-listener/conf"
1114
"github.com/ouqiang/supervisor-event-listener/event"
@@ -18,8 +21,28 @@ type Feishu conf.Feishu
1821
func (this *Feishu) Send(msg *event.Message) error {
1922
url := this.URL
2023
timeout := this.Timeout
21-
calcSign := func(secret string, timestamp int64) string {
22-
//timestamp + key 做sha256, 再进行base64 encode
24+
return send2feishu(url, msg.String(), timeout)
25+
}
26+
27+
func send2feishu(_url string, text string, timeout int) error {
28+
parse := func(_url string) (string, string) {
29+
tmpArr := strings.Split(_url, "?")
30+
if len(tmpArr) == 1 {
31+
return _url, ""
32+
} else if len(tmpArr) == 2 {
33+
_url = tmpArr[0]
34+
q, err := url.ParseQuery(tmpArr[1])
35+
if err != nil {
36+
panic(err)
37+
}
38+
return _url, q.Get("signKey")
39+
} else {
40+
panic(fmt.Errorf("invalid url: %s", _url))
41+
}
42+
}
43+
44+
sign := func(secret string, timestamp int64) string {
45+
//timestamp + key do sha256, then base64 encode
2346
stringToSign := fmt.Sprintf("%v\n%s", timestamp, secret)
2447

2548
var data []byte
@@ -30,24 +53,28 @@ func (this *Feishu) Send(msg *event.Message) error {
3053
}
3154
return base64.StdEncoding.EncodeToString(h.Sum(nil))
3255
}
33-
_ = calcSign
56+
3457
params := map[string]interface{}{
3558
"msg_type": "text",
3659
"content": map[string]interface{}{
37-
"text": msg.String(),
60+
"text": text,
3861
},
3962
}
63+
64+
_url, signKey := parse(_url)
65+
if signKey != "" {
66+
ts := time.Now().Unix()
67+
params["timestamp"] = ts
68+
params["sign"] = sign(signKey, ts)
69+
}
4070
body, err := json.Marshal(params)
4171
if err != nil {
4272
return err
4373
}
44-
resp := httpclient.PostJson(url, string(body), timeout)
74+
resp := httpclient.PostJson(_url, string(body), timeout)
4575
if !resp.IsOK() {
4676
errlog.Error("params: %v err: %v", params, resp.Error())
4777
return resp
4878
}
4979
return nil
50-
5180
}
52-
53-
// sleep and rety

supervisor-event-listener.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ timeout = 6
2424

2525

2626
[feishu]
27-
url = "https://hook.feishu.com/xxx/xxxx"
27+
url = "https://hook.feishu.com/xxx/xxxx?signKey=it_is_optional"
2828
timeout = 6

0 commit comments

Comments
 (0)