-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
345 lines (296 loc) · 8.3 KB
/
message.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
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
package dps
import (
"encoding/json"
"errors"
"fmt"
)
// Dingtalk constants
const (
// 钉钉机器人发消息地址
APIEndpoint = "https://oapi.dingtalk.com/robot/send"
)
// Message types
const (
TypeText = "text"
TypeLink = "link"
TypeMarkdown = "markdown"
TypeActionCard = "actionCard"
TypeFeedCard = "feedCard"
)
type BtnOrientationType string
// BtnOrientation
const (
BtnOrientationV BtnOrientationType = "0" // 按钮竖直排列
BtnOrientationH BtnOrientationType = "1" // 按钮横向排列
)
// IMessage is any config type that can be sent.
type IMessage interface {
SetMessageType()
GetMessageType() string
MergeMessage(boundary string, m1, m2 IMessage) []IMessage
}
// BaseMessage is base type for all message config types.
type BaseMessage struct {
MessageType string `json:"msgtype"` // 消息类型
}
// text message config.
type TextConfig struct {
BaseMessage
Text TextFieldText `json:"text"`
At TextFieldAt `json:"at"`
}
// text message 'text' field struct.
type TextFieldText struct {
Content string `json:"content"` // 消息内容。
}
// text message 'at' field struct.
type TextFieldAt struct {
AtMobiles []string `json:"atMobiles"` // 被@人的手机号。
AtUserIds []string `json:"atUserIds"` // 被@人的用户userid。
IsAtAll bool `json:"isAtAll"` // 是否@所有人。
}
// link message config.
type LinkConfig struct {
BaseMessage
Link LinkFieldLink `json:"link"`
}
// link message 'link' field struct.
type LinkFieldLink struct {
Text string `json:"text"` // 消息内容。如果太长只会部分展示。
Title string `json:"title"` // 消息标题。
PicUrl string `json:"picUrl"` // 图片URL。
MessageUrl string `json:"messageUrl"` // 点击消息跳转的URL。
}
// markdown message config.
/*
目前只支持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
*/
type MarkdownConfig struct {
BaseMessage
Markdown MarkdownFieldMarkdown `json:"markdown"`
At MarkdownFieldAt `json:"at"`
}
// markdown message 'markdown' field struct.
type MarkdownFieldMarkdown struct {
Title string `json:"title"` // 首屏会话透出的展示内容。
Text string `json:"text"` // markdown格式的消息。
}
// text message 'at' field struct.
type MarkdownFieldAt struct {
AtMobiles []string `json:"atMobiles"` // 被@人的手机号。
AtUserIds []string `json:"atUserIds"` // 被@人的用户userid。
IsAtAll bool `json:"isAtAll"` // 是否@所有人。
}
// actionCard message config.
type ActionCardConfig struct {
BaseMessage
ActionCard ActionCardFieldActionCard `json:"actionCard"`
}
// actionCard message 'actionCard' field struct.
type ActionCardFieldActionCard struct {
Title string `json:"title"` // 首屏会话透出的展示内容。
Text string `json:"text"` // markdown格式的消息。
SingleTitle string `json:"singleTitle"` // 单个按钮的标题。(设置此项和singleURL后,btns无效。)
SingleUrl string `json:"singleURL"` // 点击singleTitle按钮触发的URL。
BtnOrientation BtnOrientationType `json:"btnOrientation"` // 0:按钮竖直排列 1:按钮横向排列
Btns []ActionCardFieldBtns `json:"btns"` // 按钮。
}
// actionCard message 'btns' field struct.
type ActionCardFieldBtns struct {
Title string `json:"title"` // 按钮标题。
ActionUrl string `json:"actionURL"` // 点击按钮触发的URL。
}
// feedCard message config.
type FeedCardConfig struct {
BaseMessage
FeedCard FeedCardFieldFeedCard `json:"feedCard"`
}
// feedCard message 'feedCard' field struct.
type FeedCardFieldFeedCard struct {
Links []FeedFieldLinks `json:"links"`
}
// feedCard message 'links' field struct.
type FeedFieldLinks struct {
Title string `json:"title"` // 单条信息文本。
MessageUrl string `json:"messageURL"` // 点击单条信息到跳转链接。
PicUrl string `json:"picURL"` // 单条信息后面图片的URL。
}
// set text message type
func (self *TextConfig) SetMessageType() {
self.MessageType = TypeText
}
// set link message type
func (self *LinkConfig) SetMessageType() {
self.MessageType = TypeLink
}
// set markdown message type
func (self *MarkdownConfig) SetMessageType() {
self.MessageType = TypeMarkdown
}
// set actionCard message type
func (self *ActionCardConfig) SetMessageType() {
self.MessageType = TypeActionCard
}
// set feedCard message type
func (self *FeedCardConfig) SetMessageType() {
self.MessageType = TypeFeedCard
}
// get text message type
func (self *BaseMessage) GetMessageType() string {
return self.MessageType
}
// merge text type message
func (self *TextConfig) MergeMessage(boundary string, im1, im2 IMessage) []IMessage {
m1, ok := im1.(*TextConfig);
if !ok {
return []IMessage{im1, im2}
}
m2, ok := im2.(*TextConfig);
if !ok {
return []IMessage{im1, im2}
}
rm := &TextConfig{
BaseMessage: BaseMessage{
MessageType: m1.MessageType,
},
Text: TextFieldText{},
At: TextFieldAt{},
}
if m1.At.IsAtAll || m2.At.IsAtAll {
rm.At.IsAtAll = true
}
rm.Text.Content = fmt.Sprintf("%s\n%s\n%s", m1.Text.Content, boundary, m2.Text.Content)
rm.At.AtMobiles = append(m1.At.AtMobiles, m2.At.AtMobiles...)
rm.At.AtUserIds = append(m1.At.AtUserIds, m2.At.AtUserIds...)
return []IMessage{rm}
}
// merge link type message
func (self *LinkConfig) MergeMessage(boundary string, im1, im2 IMessage) []IMessage {
// cannot merge link type messages
return []IMessage{im1, im2}
}
// merge markdown type message
func (self *MarkdownConfig) MergeMessage(boundary string, im1, im2 IMessage) []IMessage {
m1, ok := im1.(*MarkdownConfig);
if !ok {
return []IMessage{im1, im2}
}
m2, ok := im2.(*MarkdownConfig);
if !ok {
return []IMessage{im1, im2}
}
rm := &MarkdownConfig{
BaseMessage: BaseMessage{
MessageType: m1.MessageType,
},
Markdown: MarkdownFieldMarkdown{},
At: MarkdownFieldAt{},
}
if m1.At.IsAtAll || m2.At.IsAtAll {
rm.At.IsAtAll = true
}
rm.Markdown.Title = fmt.Sprintf("%s|%s", m1.Markdown.Title, m2.Markdown.Title)
rm.Markdown.Text = fmt.Sprintf(`%s
%s
%s`, m1.Markdown.Text, boundary, m2.Markdown.Text)
rm.At.AtMobiles = append(m1.At.AtMobiles, m2.At.AtMobiles...)
rm.At.AtUserIds = append(m1.At.AtUserIds, m2.At.AtUserIds...)
return []IMessage{rm}
}
// merge actionCard type message
func (self *ActionCardConfig) MergeMessage(boundary string, im1, im2 IMessage) []IMessage {
// cannot merge actionCard type messages
return []IMessage{im1, im2}
}
// merge feedCard type message
func (self *FeedCardConfig) MergeMessage(boundary string, im1, im2 IMessage) []IMessage {
m1, ok := im1.(*FeedCardConfig);
if !ok {
return []IMessage{im1, im2}
}
m2, ok := im2.(*FeedCardConfig);
if !ok {
return []IMessage{im1, im2}
}
rm := &FeedCardConfig{
BaseMessage: BaseMessage{},
FeedCard: FeedCardFieldFeedCard{
Links: append(m1.FeedCard.Links, m2.FeedCard.Links...),
},
}
return []IMessage{rm}
}
// unmarshal IMessage bytes.
func UnmarshalBytes(bytes []byte) (im IMessage, err error) {
base := BaseMessage{}
err = json.Unmarshal(bytes, &base)
if err != nil {
return
}
msgType := base.GetMessageType()
switch msgType {
case TypeText:
config := &TextConfig{}
err = json.Unmarshal(bytes, config)
if err != nil {
return
}
im = config
return
case TypeLink:
config := &LinkConfig{}
err = json.Unmarshal(bytes, config)
if err != nil {
return
}
im = config
return
case TypeMarkdown:
config := &MarkdownConfig{}
err = json.Unmarshal(bytes, config)
if err != nil {
return
}
im = config
return
case TypeActionCard:
config := &ActionCardConfig{}
err = json.Unmarshal(bytes, config)
if err != nil {
return
}
im = config
return
case TypeFeedCard:
config := &FeedCardConfig{}
err = json.Unmarshal(bytes, config)
if err != nil {
return
}
im = config
return
}
return nil, errors.New("Failed to unmarshal message.")
}