mirror of
https://github.com/xxjwxc/public.git
synced 2025-09-26 20:01:19 +08:00
1
This commit is contained in:
83
myding/myding.go
Normal file
83
myding/myding.go
Normal file
@@ -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))
|
||||
}
|
18
myding/myding_test.go
Normal file
18
myding/myding_test.go
Normal file
@@ -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
|
||||
#### 详情: 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发 打单发`},
|
||||
})
|
||||
}
|
@@ -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")
|
||||
|
Reference in New Issue
Block a user