From 760e90ba44eb04255a17040c245ae1f615a9c59f Mon Sep 17 00:00:00 2001 From: xxjwxc Date: Tue, 19 Apr 2022 18:55:16 +0800 Subject: [PATCH] 1 --- myding/myding.go | 83 +++++++++++++++++++++++++++++++++++++++++++ myding/myding_test.go | 18 ++++++++++ tools/timeTools.go | 10 +++--- 3 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 myding/myding.go create mode 100644 myding/myding_test.go diff --git a/myding/myding.go b/myding/myding.go new file mode 100644 index 0000000..7d588b0 --- /dev/null +++ b/myding/myding.go @@ -0,0 +1,83 @@ +package myding + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/base64" + "fmt" + "net/url" + "time" + + "github.com/xxjwxc/public/myhttp" + "github.com/xxjwxc/public/tools" +) + +type robot struct { + accessToken string + secret string +} +type MsgInfo struct { + Msgtype string `json:"msgtype"` + At At + Markdown *Markdown `json:"markdown,omitempty"` + Text *Text `json:"text,omitempty"` + Link *Link `json:"link,omitempty"` +} +type At struct { + AtMobiles []string `json:"atMobiles"` + IsAtAll bool `json:"isAtAll"` +} + +type Markdown struct { + Title string `json:"title"` + Text string `json:"text"` +} + +type Text struct { + Content string `json:"content"` +} + +type Link struct { + Text string `json:"text"` + Title string `json:"title"` + PicUrl string `json:"picUrl"` + MessageUrl string `json:"messageUrl"` +} + +type Resp struct { + Errcode int `json:"errcode"` + Errmsg string `json:"errmsg"` +} + +func NewRobot(accessToken, secret string) *robot { + return &robot{accessToken: accessToken, secret: secret} +} + +func (r *robot) SendMsg(msg MsgInfo) error { + if msg.Text != nil { + msg.Msgtype = "text" + } else if msg.Markdown != nil { + msg.Msgtype = "markdown" + } else if msg.Link != nil { + msg.Msgtype = "link" + } + timestamp := time.Now().UnixMilli() + out, err := myhttp.OnPostJSON(fmt.Sprintf("https://oapi.dingtalk.com/robot/send?access_token=%v×tamp=%v&sign=%v", r.accessToken, timestamp, r.Sign(timestamp)), tools.JSONDecode(msg)) + if err != nil { + return err + } + var resp Resp + tools.JSONEncode(string(out), &resp) + if resp.Errcode != 0 { + return fmt.Errorf("ding send err:%v", resp.Errmsg) + } + return nil +} + +func (r *robot) Sign(timestamp int64) string { + stringToSign := fmt.Sprintf("%d\n%s", timestamp, r.secret) + hash := hmac.New(sha256.New, []byte(r.secret)) + hash.Write([]byte(stringToSign)) + signData := hash.Sum(nil) + return url.QueryEscape(base64.StdEncoding.EncodeToString(signData)) +} diff --git a/myding/myding_test.go b/myding/myding_test.go new file mode 100644 index 0000000..4ae6fcd --- /dev/null +++ b/myding/myding_test.go @@ -0,0 +1,18 @@ +package myding + +import ( + "testing" +) + +func Test1(t *testing.T) { + robot := NewRobot("", "") + robot.SendMsg(MsgInfo{ + Msgtype: "markdown", + At: At{IsAtAll: true}, + Markdown: &Markdown{Title: "招聘信息", Text: `## 姓名: jnn + ### 电话: 123456 + ### 公司: 公司 + ### 微信: wechat + #### 详情: 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发`}, + }) +} diff --git a/tools/timeTools.go b/tools/timeTools.go index bcce012..870b9e4 100644 --- a/tools/timeTools.go +++ b/tools/timeTools.go @@ -11,6 +11,11 @@ func GetUtcTime(tm time.Time) int64 { return tm.Unix() //- 8*60*60 } +// UnixToTime 时间戳转换为time +func UnixToTime(timestamp int64) time.Time { + return time.Unix(timestamp, 0) +} + // GetHour 当前时间向上取整点 func GetHour(timestamp int64) int { // formaTime := time.Format("2006-01-02 15:04:05") @@ -137,11 +142,6 @@ func TimerByHour(f func()) { } } -// UnixToTime 时间戳转换为time -func UnixToTime(timestamp int64) time.Time { - return time.Unix(timestamp, 0) -} - // GetLocalTime 获取本地时间 func GetLocalTime(tm time.Time) time.Time { local, _ := time.LoadLocation("Local")