-
Notifications
You must be signed in to change notification settings - Fork 1
/
cbs.go
403 lines (380 loc) · 11 KB
/
cbs.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package kook_CardBuild
import (
"encoding/json"
"errors"
"fmt"
"regexp"
)
type CardTheme string
type ButtonTheme string
type ButtonType string
type CardSize string
type ImgSize string
type TextColor string
type TextType string
type Position string
type NotesType string
type CountdownMode string
const (
CardSizeXs CardSize = "xs"
CardSizeSm CardSize = "sm"
CardSizeMd CardSize = "md"
CardSizeLg CardSize = "lg"
ImgSizeSizeSm ImgSize = "sm"
ImgSizeSizeLg ImgSize = "lg"
ImgType string = "image"
HeaderType string = "header"
LinkButton ButtonType = "link"
ReturnBUtton ButtonType = "return-val"
KmarkdownNoteType NotesType = "kmarkdown"
TextNoteType NotesType = "plain-text"
ImgNotetype NotesType = "image"
DayCountdown CountdownMode = "day"
HourCountdown CountdownMode = "hour"
SecondCountdown CountdownMode = "second"
CardThemePrimary CardTheme = "primary"
CardThemeSecondary CardTheme = "secondary"
CardThemeSuccess CardTheme = "success"
CardThemeWarning CardTheme = "warning"
CardThemeDanger CardTheme = "danger"
CardThemeInfo CardTheme = "info"
CardThemeNone CardTheme = "none"
ButtonThemePrimary ButtonTheme = "primary"
ButtonThemeSecondary ButtonTheme = "secondary"
ButtonThemeSuccess ButtonTheme = "success"
ButtonThemeWarning ButtonTheme = "warning"
ButtonThemeDanger ButtonTheme = "danger"
ButtonThemeInfo ButtonTheme = "info"
TextColorSuccess TextColor = "success"
TextColorPurple TextColor = "purple"
TextColorWarning TextColor = "warning"
TextColorPink TextColor = "pink"
Kmarkdown TextType = "kmarkdown"
Plain TextType = "plain-text"
Left Position = "left"
Right Position = "right"
)
type cardMessage struct {
Type string `json:"type"`
Theme CardTheme `json:"theme"`
Color string `json:"color"`
Size CardSize `json:"size"`
Modules []interface{} `json:"modules"`
}
type Text struct {
Type string `json:"type"`
Text struct {
Type string `json:"type"`
Content string `json:"content"`
} `json:"text"`
}
type KmarkdownText struct {
Type string `json:"type"`
Text struct {
Type string `json:"type"`
Content string `json:"content"`
} `json:"text"`
}
type Fields struct {
Type string `json:"type"`
Text FieledsText `json:"text"`
}
type FieledsText struct {
Type string `json:"type"`
Cols int `json:"cols"`
Fields []FiledsContent `json:"fields"`
}
type FiledsContent struct {
Type string `json:"type"`
Content string `json:"content"`
}
type TextWithPics struct {
Type string `json:"type"`
Text struct {
Type TextType `json:"type"`
Content string `json:"content"`
} `json:"text"`
Mode Position `json:"mode"`
Accessory struct {
Type string `json:"type"`
Src string `json:"src"`
Size ImgSize `json:"size"`
} `json:"accessory"`
}
type TextWithButton struct {
Type string `json:"type"`
Text struct {
Type TextType `json:"type"`
Content string `json:"content"`
} `json:"text"`
Mode string `json:"mode"`
Accessory struct {
Type string `json:"type"`
Theme ButtonTheme `json:"theme"`
Text struct {
Type string `json:"type"`
Content string `json:"content"`
} `json:"text"`
} `json:"accessory"`
}
type Image struct {
Type string `json:"type"`
Elements []struct {
Type string `json:"type"`
Src string `json:"src"`
} `json:"elements"`
}
type ImageGroup struct {
Type string `json:"type"`
Elements []imageUrlDetails `json:"elements"`
}
type imageUrlDetails struct {
Type string `json:"type"`
Src string `json:"src"`
}
type Header struct {
Type string `json:"type"`
Text struct {
Type string `json:"type"`
Content string `json:"content"`
} `json:"text"`
}
type Buttons struct {
Type string `json:"type"`
Elements []Button `json:"elements"`
}
type Button struct {
Type string `json:"type"`
Click string `json:"click"`
Theme ButtonTheme `json:"theme"`
Value string `json:"value"`
Text struct {
Type TextType `json:"type"`
Content string `json:"content"`
} `json:"text"`
}
type ButtonCreate struct {
ButtonType ButtonType
Value string
Theme ButtonTheme
ButtonText string
}
type Notes struct {
Type string `json:"type"`
Elements []interface{} `json:"elements"`
}
type noteText struct {
Type NotesType `json:"type"`
Content string `json:"content"`
}
type noteImage struct {
Type NotesType `json:"type"`
Src string `json:"src"`
}
type noteKmarkdown struct {
Type NotesType `json:"type"`
Content string `json:"content"`
}
type NotesCreate struct {
Types NotesType
Value string
}
type SecondCountDown struct {
Type string `json:"type"`
Mode CountdownMode `json:"mode"`
EndTime int64 `json:"endTime"`
StartTime int64 `json:"startTime"`
}
type DayHourCountDown struct {
Type string `json:"type"`
Mode CountdownMode `json:"mode"`
EndTime int64 `json:"endTime"`
}
func NewDefaultCard() *cardMessage {
return &cardMessage{
Color: "#666666",
Type: "card",
Theme: CardThemeSecondary,
Size: CardSizeLg,
Modules: []interface{}{},
}
}
func NewCardWithOption(t CardTheme, s CardSize, c string) (*cardMessage, error) {
if !IsHexCode(c) {
return nil, errors.New("颜色十六进制代码不正确,请检查!")
}
return &cardMessage{
Type: "card",
Theme: t,
Color: c,
Size: s,
Modules: []interface{}{},
}, nil
}
func (c *cardMessage) SetCardTheme(t CardTheme) {
c.Theme = t
return
}
func (c *cardMessage) SetCardColor(col string) error {
if !IsHexCode(col) {
return fmt.Errorf("颜色十六进制代码不正确,请检查!")
}
c.Color = col
return nil
}
func (c *cardMessage) SetCardSize(s CardSize) {
c.Size = CardSizeMd
return
}
func (c *cardMessage) AddText(s string) {
c.Modules = append(c.Modules, Text{Type: "section", Text: struct {
Type string `json:"type"`
Content string `json:"content"`
}{Type: "plain-text", Content: s}})
return
}
func (c *cardMessage) AddKmarkdown(s string) {
c.Modules = append(c.Modules, KmarkdownText{Type: "section", Text: struct {
Type string `json:"type"`
Content string `json:"content"`
}{Type: "kmarkdown", Content: s}})
return
}
func (c *cardMessage) AddFields(s []string , cols int) {
var filedsContent = []FiledsContent{}
for _, v := range s {
filedsContent = append(filedsContent, FiledsContent{Type: "kmarkdown", Content: v})
}
c.Modules = append(c.Modules, Fields{Type: "section",
Text: FieledsText{
Type: "paragraph",
Cols: cols,
Fields: filedsContent,
}})
return
}
func (c *cardMessage) AddColorText(tc TextColor, s string) {
c.Modules = append(c.Modules, KmarkdownText{Type: "section", Text: struct {
Type string `json:"type"`
Content string `json:"content"`
}{Type: "kmarkdown", Content: fmt.Sprintf("(font)%s(font)[%s]", s, tc)}})
return
}
func (c *cardMessage) AddTextWithPics(tt TextType, content string, imagePosition Position, imgUrl string, imgSize ImgSize) {
c.Modules = append(c.Modules, TextWithPics{Type: "section", Text: struct {
Type TextType `json:"type"`
Content string `json:"content"`
}{Type: tt, Content: content}, Mode: imagePosition, Accessory: struct {
Type string `json:"type"`
Src string `json:"src"`
Size ImgSize `json:"size"`
}{Type: ImgType, Src: imgUrl, Size: imgSize}})
return
}
func (c *cardMessage) AddTextWithButton(tt TextType, content string, buttonTheme ButtonTheme, buttonContent string) {
c.Modules = append(c.Modules, TextWithButton{Type: "section", Text: struct {
Type TextType `json:"type"`
Content string `json:"content"`
}{Type: tt, Content: content}, Mode: "right", Accessory: struct {
Type string `json:"type"`
Theme ButtonTheme `json:"theme"`
Text struct {
Type string `json:"type"`
Content string `json:"content"`
} `json:"text"`
}{Type: "button", Theme: buttonTheme, Text: struct {
Type string `json:"type"`
Content string `json:"content"`
}{Type: "plain-text", Content: buttonContent}}})
return
}
func (c *cardMessage) AddImage(imgUrl string) {
c.Modules = append(c.Modules, Image{Type: "container", Elements: []struct {
Type string `json:"type"`
Src string `json:"src"`
}{{
Type: ImgType,
Src: imgUrl,
}}})
return
}
func (c *cardMessage) AddImageGroup(imgUrlGroup []string) {
var ig = []imageUrlDetails{}
for _, v := range imgUrlGroup {
ig = append(ig, imageUrlDetails{Type: ImgType, Src: v})
}
c.Modules = append(c.Modules, ImageGroup{Type: "image-group", Elements: ig})
return
}
func (c *cardMessage) AddHeader(content string) {
c.Modules = append(c.Modules, Header{Type: HeaderType, Text: struct {
Type string `json:"type"`
Content string `json:"content"`
}{Type: "plain-text", Content: content}})
return
}
func (c *cardMessage) AddDivider() {
c.Modules = append(c.Modules, struct {
Type string `json:"type"`
}{Type: "divider"})
return
}
func (c *cardMessage) AddButtonGroup(ButtonGroups []ButtonCreate) {
var temB = []Button{}
for _, v := range ButtonGroups {
if v.ButtonType == LinkButton {
temB = append(temB, Button{Type: "button", Click: "link", Theme: v.Theme, Value: v.Value, Text: struct {
Type TextType `json:"type"`
Content string `json:"content"`
}{Type: Plain, Content: v.ButtonText}})
}
if v.ButtonType == ReturnBUtton {
temB = append(temB, Button{Type: "button", Click: "return-val", Theme: v.Theme, Value: v.Value, Text: struct {
Type TextType `json:"type"`
Content string `json:"content"`
}{Type: Plain, Content: v.ButtonText}})
}
}
c.Modules = append(c.Modules, Buttons{Type: "action-group", Elements: temB})
return
}
func (c *cardMessage) AddNotes(notesDetails []NotesCreate) {
var temN = []interface{}{}
for _, v := range notesDetails {
switch v.Types {
case TextNoteType:
temN = append(temN, noteText{Type: TextNoteType, Content: v.Value})
case ImgNotetype:
temN = append(temN, noteImage{Type: ImgNotetype, Src: v.Value})
case KmarkdownNoteType:
temN = append(temN, noteKmarkdown{Type: KmarkdownNoteType, Content: v.Value})
}
}
c.Modules = append(c.Modules, Notes{Type: "context", Elements: temN})
return
}
func (c *cardMessage) AddCountdown(countdownMode CountdownMode, endTime, startTime int64) {
if countdownMode == SecondCountdown {
c.Modules = append(c.Modules, SecondCountDown{Type: "countdown", Mode: countdownMode, EndTime: endTime, StartTime: startTime})
} else {
c.Modules = append(c.Modules, DayHourCountDown{Type: "countdown", Mode: countdownMode, EndTime: endTime})
}
return
}
func IsHexCode(str string) bool {
// 创建匹配十六进制代码的正则表达式
regex := regexp.MustCompile(`^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$`)
// 使用正则表达式进行匹配
return regex.MatchString(str)
}
func GenerateCardMessageContent(cards ...*cardMessage) (res string, err error) {
var cardMessageContent = []*cardMessage{}
for _, v := range cards {
cardMessageContent = append(cardMessageContent, v)
}
indent, err := json.MarshalIndent(cardMessageContent, "", " ")
if err != nil {
return "", err
}
return string(indent), nil
}