-
Notifications
You must be signed in to change notification settings - Fork 3
/
robot.go
151 lines (127 loc) · 2.9 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package robot
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
"github.com/iaping/go-dingtalk-robot/message"
"github.com/iaping/go-dingtalk-robot/response"
)
const (
Timeout = 5 * time.Second
Webhook = "https://oapi.dingtalk.com/robot/send"
)
type Robot struct {
Token, Secret string
Client *http.Client
}
func New(token, secret string) *Robot {
return &Robot{
Token: token,
Secret: secret,
}
}
func (bot *Robot) Send(message message.IMessage) (*response.Response, error) {
var (
data []byte
err error
)
if data, err = json.Marshal(message); err != nil {
return nil, err
}
var result []byte
if result, err = bot.send(data); err != nil {
return nil, err
}
var resp = new(response.Response)
if err = json.Unmarshal(result, &resp); err != nil {
return nil, err
}
if resp.Status > 0 {
resp.Code = resp.Status
resp.Message = fmt.Sprintf("punish[%s] wait[%d]", resp.Punish, resp.Wait)
}
return resp, nil
}
// http request
func (bot *Robot) send(data []byte) ([]byte, error) {
var (
req *http.Request
err error
)
if req, err = bot.buildRequest(data, http.Header{}); err != nil {
return nil, err
}
if bot.Client == nil {
bot.setDefaultClient()
}
var resp *http.Response
if resp, err = bot.Client.Do(req); err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error http code: %d", resp.StatusCode)
}
var body []byte
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return nil, err
}
return body, err
}
// build http post request
func (bot *Robot) buildRequest(data []byte, header http.Header) (*http.Request, error) {
var (
url *url.URL
err error
)
if url, err = bot.buildWebHook(); err != nil {
return nil, err
}
var req *http.Request
if req, err = http.NewRequest("POST", url.String(), bytes.NewReader(data)); err != nil {
return nil, err
}
header.Set("Content-Type", "application/json;charset=utf-8")
req.Header = header
return req, nil
}
// build robot webhook
func (bot *Robot) buildWebHook() (*url.URL, error) {
var (
url *url.URL
err error
)
if url, err = url.Parse(Webhook); err != nil {
return nil, err
}
query := url.Query()
query.Set("access_token", bot.Token)
if bot.Secret != "" {
timestamp, sign := bot.sign()
query.Add("timestamp", strconv.FormatInt(timestamp, 10))
query.Add("sign", sign)
}
url.RawQuery = query.Encode()
return url, nil
}
// default http client
func (bot *Robot) setDefaultClient() *Robot {
bot.Client = &http.Client{Timeout: Timeout}
return bot
}
// signature
func (bot *Robot) sign() (int64, string) {
timestamp := time.Now().Unix() * 1000
src := fmt.Sprintf("%d\n%s", timestamp, bot.Secret)
sha256 := hmac.New(sha256.New, []byte(bot.Secret))
sha256.Write([]byte(src))
return timestamp, base64.StdEncoding.EncodeToString(sha256.Sum(nil))
}