Skip to content

Commit

Permalink
支持 feishu 签名消息
Browse files Browse the repository at this point in the history
  • Loading branch information
cupen committed Apr 14, 2021
1 parent 916983c commit cff3d83
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project_name:=supervisor-event-listener
project_version:=1.2.1
project_version:=1.2.2
root_dir := $(abspath $(CURDIR))
build_dir := $(root_dir)/build
GOPATH := ${HOME}/go
Expand Down
43 changes: 35 additions & 8 deletions notify/feishu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"

"github.com/ouqiang/supervisor-event-listener/conf"
"github.com/ouqiang/supervisor-event-listener/event"
Expand All @@ -18,8 +21,28 @@ type Feishu conf.Feishu
func (this *Feishu) Send(msg *event.Message) error {
url := this.URL
timeout := this.Timeout
calcSign := func(secret string, timestamp int64) string {
//timestamp + key 做sha256, 再进行base64 encode
return send2feishu(url, msg.String(), timeout)
}

func send2feishu(_url string, text string, timeout int) error {
parse := func(_url string) (string, string) {
tmpArr := strings.Split(_url, "?")
if len(tmpArr) == 1 {
return _url, ""
} else if len(tmpArr) == 2 {
_url = tmpArr[0]
q, err := url.ParseQuery(tmpArr[1])
if err != nil {
panic(err)
}
return _url, q.Get("signKey")
} else {
panic(fmt.Errorf("invalid url: %s", _url))
}
}

sign := func(secret string, timestamp int64) string {
//timestamp + key do sha256, then base64 encode
stringToSign := fmt.Sprintf("%v\n%s", timestamp, secret)

var data []byte
Expand All @@ -30,24 +53,28 @@ func (this *Feishu) Send(msg *event.Message) error {
}
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
_ = calcSign

params := map[string]interface{}{
"msg_type": "text",
"content": map[string]interface{}{
"text": msg.String(),
"text": text,
},
}

_url, signKey := parse(_url)
if signKey != "" {
ts := time.Now().Unix()
params["timestamp"] = ts
params["sign"] = sign(signKey, ts)
}
body, err := json.Marshal(params)
if err != nil {
return err
}
resp := httpclient.PostJson(url, string(body), timeout)
resp := httpclient.PostJson(_url, string(body), timeout)
if !resp.IsOK() {
errlog.Error("params: %v err: %v", params, resp.Error())
return resp
}
return nil

}

// sleep and rety
2 changes: 1 addition & 1 deletion supervisor-event-listener.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ timeout = 6


[feishu]
url = "https://hook.feishu.com/xxx/xxxx"
url = "https://hook.feishu.com/xxx/xxxx?signKey=it_is_optional"
timeout = 6

0 comments on commit cff3d83

Please sign in to comment.