-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
243 lines (217 loc) · 6.05 KB
/
base.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
package feishu
type MsgType string
const (
// TextMsg 文本类型
TextMsg MsgType = "text"
// PostMsg 富文本
PostMsg MsgType = "post"
// ImageMsg 图片
ImageMsg MsgType = "image"
// InteractiveMsg 卡片
InteractiveMsg MsgType = "interactive"
)
// Message feishu robot send message struct
type Message interface {
// ToMessageMap for JSON serialization
ToMessageMap() map[string]interface{}
}
// TextMessage 文本消息
type TextMessage struct {
// Text 文本消息内容
Text string
// AtAll 是否@所有人
AtAll bool
}
// NewTextMessage create TextMessage
func NewTextMessage(text string, atAll bool) *TextMessage {
return &TextMessage{
Text: text,
AtAll: atAll,
}
}
func (text *TextMessage) ToMessageMap() map[string]interface{} {
var isAtAll = ""
if text.AtAll {
isAtAll = "\r\n<at user_id=\"all\"> </at>"
}
textMsg := map[string]interface{}{}
textMsg["text"] = text.Text + isAtAll
msg := map[string]interface{}{}
msg["msg_type"] = TextMsg
msg["content"] = textMsg
return msg
}
// PostMessage post message
type PostMessage struct {
LangItem []*LangPostItem
}
// NewPostMessage create PostMessage
func NewPostMessage(langItem ...*LangPostItem) *PostMessage {
return &PostMessage{
LangItem: langItem,
}
}
// AddLangItem add LangPostItem
func (post *PostMessage) AddLangItem(langItem ...*LangPostItem) *PostMessage {
post.LangItem = append(post.LangItem, langItem...)
return post
}
func (post *PostMessage) ToMessageMap() map[string]interface{} {
langMsg := map[string]interface{}{}
for _, lang := range post.LangItem {
for k, v := range lang.ToMessageMap() {
langMsg[k] = v
}
}
postMsg := map[string]interface{}{
"post": langMsg,
}
msg := map[string]interface{}{}
msg["msg_type"] = PostMsg
msg["content"] = postMsg
return msg
}
// ImageMessage 图片
type ImageMessage struct {
// ImageKey image_key
ImageKey string
}
// NewImageMessage create ImageMessage
func NewImageMessage(imageKey string) *ImageMessage {
return &ImageMessage{
ImageKey: imageKey,
}
}
func (image *ImageMessage) ToMessageMap() map[string]interface{} {
imgMsg := map[string]interface{}{
"image_key": image.ImageKey,
}
msg := map[string]interface{}{}
msg["msg_type"] = ImageMsg
msg["content"] = imgMsg
return msg
}
// InteractiveMessage 消息卡片
type InteractiveMessage struct {
// Config 用于描述卡片的功能属性
Config *CardConfig
// Header 用于配置卡片标题内容
Header *CardHeader
// CarLink 指定卡片整体的点击跳转链接
CarLink *CardLinkElement
// Elements 用于定义卡片正文内容
Elements []CardContent
// i18nElements 为卡片的正文部分定义多语言内容
i18nElements *I18nInteractiveElement
}
// NewInteractiveMessage create InteractiveMessage
func NewInteractiveMessage() *InteractiveMessage {
return &InteractiveMessage{
Elements: []CardContent{},
}
}
// SetConfig set InteractiveMessage.Config
func (message *InteractiveMessage) SetConfig(config *CardConfig) *InteractiveMessage {
message.Config = config
return message
}
// SetHeader set InteractiveMessage.Header
func (message *InteractiveMessage) SetHeader(header *CardHeader) *InteractiveMessage {
message.Header = header
return message
}
// SetCardLink set InteractiveMessage.CarLink
func (message *InteractiveMessage) SetCardLink(link *CardLinkElement) *InteractiveMessage {
message.CarLink = link
return message
}
// SetElements set InteractiveMessage.Elements
func (message *InteractiveMessage) SetElements(elements ...CardContent) *InteractiveMessage {
message.Elements = elements
return message
}
// AddElements add InteractiveMessage.Elements
func (message *InteractiveMessage) AddElements(elements ...CardContent) *InteractiveMessage {
message.Elements = append(message.Elements, elements...)
return message
}
// SetI18nElements set InteractiveMessage.i18nElements
func (message *InteractiveMessage) SetI18nElements(i18nElements *I18nInteractiveElement) *InteractiveMessage {
message.i18nElements = i18nElements
return message
}
func (message *InteractiveMessage) ToMessageMap() map[string]interface{} {
interactiveMsg := map[string]interface{}{}
if message.Header != nil {
interactiveMsg["header"] = message.Header.ToMessage()
}
if message.Config != nil {
interactiveMsg["config"] = message.Config.ToMessage()
}
if message.CarLink != nil {
interactiveMsg["card_link"] = message.CarLink.ToMessage()
}
if len(message.Elements) > 0 {
var eles []map[string]interface{}
for _, ele := range message.Elements {
eles = append(eles, ele.ToMessage())
}
interactiveMsg["elements"] = eles
}
if message.i18nElements != nil {
interactiveMsg["i18n_elements"] = message.i18nElements.ToMap()
}
return map[string]interface{}{
"msg_type": InteractiveMsg,
"card": interactiveMsg,
}
}
// I18nInteractiveElement 为卡片的正文部分定义多语言内容
type I18nInteractiveElement struct {
// Elements 多语言内容
//
//"en_us": [
// //English - card content
// {module-1},
// {module-2},
// {module-3},
// ......
// ],
// "zh_cn": [
// //Chinese - card content
// {module-1},
// {module-2},
// {module-3},
// ......
// ],
Elements map[string][]CardContent
}
// NewI18nInteractiveElement create I18nInteractiveElement
func NewI18nInteractiveElement(elements map[string][]CardContent) *I18nInteractiveElement {
return &I18nInteractiveElement{
Elements: elements,
}
}
// Put add i18n contents
func (element *I18nInteractiveElement) Put(lang string, contents ...CardContent) *I18nInteractiveElement {
eles, ok := element.Elements[lang]
if ok {
eles = append(eles, contents...)
} else {
eles = contents
}
element.Elements[lang] = eles
return element
}
// ToMap to map
func (element *I18nInteractiveElement) ToMap() map[string]interface{} {
msg := map[string]interface{}{}
for k, eles := range element.Elements {
var contents []map[string]interface{}
for _, el := range eles {
contents = append(contents, el.ToMessage())
}
msg[k] = contents
}
return msg
}