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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
// Copyright 2017 Lunny Xiao. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package dingtalk
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
/*
{
"msgtype": "text",
"text": {
"content": "我就是我, 是不一样的烟火"
},
"at": {
"atMobiles": [
"156xxxx8827",
"189xxxx8325"
],
"isAtAll": false
}
}
{
"msgtype": "link",
"link": {
"text": "这个即将发布的新版本,创始人陈航(花名“无招”)称它为“红树林”。
而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是“红树林”?",
"title": "时代的火车向前开",
"picUrl": "",
"messageUrl": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
}
}
{
"msgtype": "markdown",
"markdown": {
"title":"杭州天气",
"text": "#### 杭州天气 @156xxxx8827\n" +
"> 9度,西北风1级,空气良89,相对温度73%\n\n" +
"> ![screenshot](http://image.jpg)\n" +
"> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n"
},
"at": {
"atMobiles": [
"156xxxx8827",
"189xxxx8325"
],
"isAtAll": false
}
}
{
"actionCard": {
"title": "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
"text": "![screenshot](@lADOpwk3K80C0M0FoA)
### 乔布斯 20 年前想打造的苹果咖啡厅
Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
"hideAvatar": "0",
"btnOrientation": "0",
"singleTitle" : "阅读全文",
"singleURL" : "https://www.dingtalk.com/",
"btns": [
{
"title": "内容不错",
"actionURL": "https://www.dingtalk.com/"
},
{
"title": "不感兴趣",
"actionURL": "https://www.dingtalk.com/"
}
]
},
"msgtype": "actionCard"
}
{
"feedCard": {
"links": [
{
"title": "时代的火车向前开",
"messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
"picURL": "https://www.dingtalk.com/"
},
{
"title": "时代的火车向前开2",
"messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
"picURL": "https://www.dingtalk.com/"
}
]
},
"msgtype": "feedCard"
}
*/
type LinkMsg struct {
Title string `json:"title"`
MessageURL string `json:"messageURL"`
PicURL string `json:"picURL"`
}
type ActionCard struct {
Text string `json:"text"`
Title string `json:"title"`
HideAvatar string `json:"hideAvatar"`
BtnOrientation string `json:"btnOrientation"`
SingleTitle string `json:"singleTitle"`
SingleURL string `json:"singleURL"`
Buttons []struct {
Title string `json:"title"`
ActionURL string `json:"actionURL"`
} `json:"btns"`
}
// Payload struct
type Payload struct {
MsgType string `json:"msgtype"`
Text struct {
Content string `json:"content"`
} `json:"text"`
Link struct {
Text string `json:"text"`
Title string `json:"title"`
PicURL string `json:"picUrl"`
MessageURL string `json:"messageUrl"`
} `json:"link"`
Markdown struct {
Text string `json:"text"`
Title string `json:"title"`
} `json:"markdown"`
ActionCard ActionCard `json:"actionCard"`
FeedCard struct {
Links []LinkMsg `json:"links"`
} `json:"feedCard"`
At struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
} `json:"at"`
}
type Webhook struct {
accessToken string
}
func NewWebhook(accessToken string) *Webhook {
return &Webhook{accessToken}
}
type Response struct {
ErrorCode int `json:"errcode"`
ErrorMessage string `json:"errmsg"`
}
// SendPayload 发送消息
func (w *Webhook) SendPayload(payload *Payload) error {
bs, err := json.Marshal(payload)
if err != nil {
return err
}
resp, err := http.Post("https://oapi.dingtalk.com/robot/send?access_token="+w.accessToken, "application/json", bytes.NewReader(bs))
if err != nil {
return err
}
bs, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("%d: %s", resp.StatusCode, string(bs))
}
var result Response
err = json.Unmarshal(bs, &result)
if err != nil {
return err
}
if result.ErrorCode != 0 {
return fmt.Errorf("%d: %s", result.ErrorCode, result.ErrorMessage)
}
return nil
}
// SendTextMsg 发送文本消息
func (w *Webhook) SendTextMsg(content string, isAtAll bool, mobiles ...string) error {
return w.SendPayload(&Payload{
MsgType: "text",
Text: struct {
Content string `json:"content"`
}{
Content: content,
},
At: struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
}{
AtMobiles: mobiles,
IsAtAll: isAtAll,
},
})
}
// SendLinkMsg 发送链接消息
func (w *Webhook) SendLinkMsg(title, content, picURL, msgURL string) error {
return w.SendPayload(&Payload{
MsgType: "link",
Link: struct {
Text string `json:"text"`
Title string `json:"title"`
PicURL string `json:"picUrl"`
MessageURL string `json:"messageUrl"`
}{
Text: content,
Title: title,
PicURL: picURL,
MessageURL: msgURL,
},
})
}
// SendMarkdownMsg 发送markdown消息,仅支持以下格式
/*
标题
# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题
引用
> A man who stands for nothing will fall for anything.
文字加粗、斜体
**bold**
*italic*
链接
[this is a link](http://name.com)
图片
![](http://name.com/pic.jpg)
无序列表
- item1
- item2
有序列表
1. item1
2. item2
*/
func (w *Webhook) SendMarkdownMsg(title, content string, isAtAll bool, mobiles ...string) error {
return w.SendPayload(&Payload{
MsgType: "markdown",
Markdown: struct {
Text string `json:"text"`
Title string `json:"title"`
}{
Text: content,
Title: title,
},
At: struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
}{
AtMobiles: mobiles,
IsAtAll: isAtAll,
},
})
}
// SendSingleActionCardMsg 发送整体跳转ActionCard类型消息
func (w *Webhook) SendSingleActionCardMsg(title, content, linkTitle, linkURL string, hideAvatar, btnOrientation bool) error {
var strHideAvatar = "0"
if hideAvatar {
strHideAvatar = "1"
}
var strBtnOrientation = "0"
if btnOrientation {
strBtnOrientation = "1"
}
return w.SendPayload(&Payload{
MsgType: "actionCard",
ActionCard: ActionCard{
Text: content,
Title: title,
HideAvatar: strHideAvatar,
BtnOrientation: strBtnOrientation,
SingleTitle: linkTitle,
SingleURL: linkURL,
},
})
}
// SendActionCardMsg 独立跳转ActionCard类型
func (w *Webhook) SendActionCardMsg(title, content string, linkTitles, linkURLs []string, hideAvatar, btnOrientation bool) error {
if len(linkTitles) == 0 || len(linkURLs) == 0 {
return errors.New("链接参数不能为空")
}
if len(linkTitles) != len(linkURLs) {
return errors.New("链接数量不匹配")
}
var strHideAvatar = "0"
if hideAvatar {
strHideAvatar = "1"
}
var strBtnOrientation = "0"
if btnOrientation {
strBtnOrientation = "1"
}
var btns []struct {
Title string `json:"title"`
ActionURL string `json:"actionURL"`
}
for i := 0; i < len(linkTitles); i++ {
btns = append(btns, struct {
Title string `json:"title"`
ActionURL string `json:"actionURL"`
}{
Title: linkTitles[i],
ActionURL: linkURLs[i],
})
}
return w.SendPayload(&Payload{
MsgType: "actionCard",
ActionCard: ActionCard{
Text: content,
Title: title,
HideAvatar: strHideAvatar,
BtnOrientation: strBtnOrientation,
Buttons: btns,
},
})
}
// SendLinkCardMsg 发送链接消息
func (w *Webhook) SendLinkCardMsg(msgs []LinkMsg) error {
return w.SendPayload(&Payload{
MsgType: "feedCard",
FeedCard: struct {
Links []LinkMsg `json:"links"`
}{
Links: msgs,
},
})
}
|