package push import ( "crypto/hmac" "crypto/sha256" "encoding/base64" "fmt" "strconv" "time" log "github.com/sirupsen/logrus" "github.com/sjkhsl/study_xxqg/utils" ) type Feishu struct { HookUrl string Secret string } func (f *Feishu) GetPush() func(id string, kind string, message string) { return func(id string, kind string, message string) { if kind == "flush" { f.SendText(message) } else { if log.GetLevel() == log.DebugLevel { f.SendText(message) } } } } func (f *Feishu) SendText(text string) { msg := &FeishuMsg{ MsgType: "text", Content: map[string]any{ "text": text, }, Timestamp: "", Sign: "", } msg.buildSign(f.Secret) res := new(FeiShuResponse) _, err := utils.GetClient().R().SetBodyJsonMarshal(msg).SetResult(res).Post(f.HookUrl) if err != nil { log.Errorln("send feishu msg err: " + err.Error()) return } if res.Code != 0 { log.Errorln("send feishu msg err: " + res.Msg) } } func (f *FeishuMsg) buildSign(secret string) { timestamp := time.Now().Unix() stringToSign := fmt.Sprintf("%v", timestamp) + "\n" + secret var data []byte h := hmac.New(sha256.New, []byte(stringToSign)) _, _ = h.Write(data) signature := base64.StdEncoding.EncodeToString(h.Sum(nil)) f.Sign = signature f.Timestamp = strconv.FormatInt(timestamp, 10) } type FeishuMsg struct { MsgType string `json:"msg_type"` Content any `json:"content"` Timestamp string `json:"timestamp"` Sign string `json:"sign"` } type FeiShuResponse struct { StatusCode int `json:"StatusCode"` StatusMessage string `json:"StatusMessage"` Code int `json:"code"` Data struct { } `json:"data"` Msg string `json:"msg"` }