Fetch the repository succeeded.
package feishu
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"time"
)
type SecretMethod struct {
WhileIpMethod
secret string
}
func (m *SecretMethod) Init(hook, secret string) {
m.hook, m.secret = hook, secret
}
func (m *SecretMethod) sign(timestamp int64) (string, error) {
//timestamp + key 做sha256, 再进行base64 encode
stringToSign := fmt.Sprintf("%v", timestamp) + "\n" + m.secret
var data []byte
h := hmac.New(sha256.New, []byte(stringToSign))
_, err := h.Write(data)
if err != nil {
return "", err
}
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
return signature, nil
}
func (m *SecretMethod) getBody(val map[string]interface{}) ([]byte, error) {
tm := time.Now().Unix()
sign, err := m.sign(tm)
if err != nil {
return nil, err
}
val["timestamp"] = fmt.Sprintf("%d", tm)
val["sign"] = sign
return json.Marshal(val)
}
func (m *SecretMethod) SendSimpleMsg(content string) (bool, error) {
body, err := m.getBody(getSimpleMsg(content))
if err != nil {
return false, err
}
return post(m.hook, body)
}
func (m *SecretMethod) SendRichMsg(title string, content RichContent) (bool, error) {
body, err := m.getBody(getRichMsg(title, content))
if err != nil {
return false, err
}
return post(m.hook, body)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。