Ai
1 Star 1 Fork 0

李光春/Golang DingTalk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dingtalk.go 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
李光春 提交于 2021-07-19 17:34 +08:00 . update *
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&timestamp=%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)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dtapps/go-dingtalk.git
git@gitee.com:dtapps/go-dingtalk.git
dtapps
go-dingtalk
Golang DingTalk
v1.0.4

搜索帮助