代码拉取完成,页面将自动刷新
package dingtalk
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"gitee.com/dtapps/go-dingtalk/dingtalk/message"
"io/ioutil"
"net/http"
"strings"
"time"
)
const api = "https://oapi.dingtalk.com/robot/send"
type DingBot struct {
Secret string
AccessToken string
}
type response struct {
Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"`
}
func (bot *DingBot) Send(msg message.Message) (response, error) {
timestamp := time.Now().UnixNano() / 1e6
var response response
signStr := sign(timestamp, bot.Secret)
dingUrl := fmt.Sprintf("%s?access_token=%s×tamp=%d&sign=%s", api, bot.AccessToken, timestamp, signStr)
j, e := json.Marshal(msg)
if e != nil {
return response, e
}
resp, e := http.Post(dingUrl, "application/json", strings.NewReader(string(j)))
if e != nil {
return response, e
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
e = json.Unmarshal(body, &response)
if e != nil {
return response, e
}
return response, nil
}
func sign(t int64, secret string) string {
secStr := fmt.Sprintf("%d\n%s", t, secret)
hmac256 := hmac.New(sha256.New, []byte(secret))
hmac256.Write([]byte(secStr))
result := hmac256.Sum(nil)
return base64.StdEncoding.EncodeToString(result)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。