-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.go
71 lines (61 loc) · 1.95 KB
/
robot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package dingding
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"github.com/hopeio/cherry/utils/net/http/client"
"net/url"
"strconv"
"strings"
"time"
)
const (
//VERSION is SDK version
VERSION = "0.1"
//ROOT is the root url
ROOT = "https://oapi.dingtalk.com/"
)
func SendRobotMessage(accessToken, secret, title, content string, msgType MsgType) error {
signUrl, err := RobotUrl(accessToken, secret)
if err != nil {
return err
}
body := strings.NewReader(msgType.Body(title, content))
return client.DoPost(ROOT+signUrl, body, nil)
}
func RobotUrl(accessToken, secret string) (string, error) {
if accessToken == "" {
return "", errors.New("token不能为为空")
}
if secret != "" {
// 密钥加签处理
now := time.Now().UnixNano() / int64(time.Millisecond)
timestampStr := strconv.FormatInt(now, 10)
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(timestampStr + "\n" + secret))
sum := h.Sum(nil)
return fmt.Sprintf("robot/send?access_token=%s×tamp=%s&sign=%s", accessToken, timestampStr, url.QueryEscape(base64.StdEncoding.EncodeToString(sum))), nil
}
return fmt.Sprintf("robot/send?access_token=%s", accessToken), nil
}
// SendRobotTextMessage can send a text message to a group chat
func SendRobotTextMessage(accessToken string, content string) error {
return SendRobotMessage(accessToken, "", "", content, MsgTypeText)
}
func SendRobotTextMessageWithSecret(accessToken, secret, content string) error {
if secret == "" {
return errors.New("secret不能为空")
}
return SendRobotMessage(accessToken, secret, "", content, MsgTypeText)
}
func SendRobotMarkDownMessage(token, title, content string) error {
return SendRobotMessage(token, "", title, content, MsgTypeMarkdown)
}
func SendRobotMarkDownMessageWithSecret(token, secret, title, content string) error {
if secret == "" {
return errors.New("secret不能为空")
}
return SendRobotMessage(token, secret, title, content, MsgTypeMarkdown)
}