-
Notifications
You must be signed in to change notification settings - Fork 4
/
post.go
150 lines (138 loc) · 3.54 KB
/
post.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
package fsBotAPI
type i18nPost struct {
lang string // 富文本的语言环境
title string // 富文本标题
elements []interface{} // 段落的所有元素
}
type Post func() i18nPost
// WithPost 富文本消息, 可指定语言环境
//
// 支持元素如下:
// 普通文本: WithPostElementText
// 文字超链接: WithPostElementLink
// 图片: WithPostElementImage
// @所有人: WithPostElementMentionAll
// @指定用户(OpenID): WithPostElementMentionByOpenID
func WithPost(lang Language, title string, elements ...PostElement) Post {
return func() i18nPost {
es := make([]interface{}, 0, len(elements))
p := make([]interface{}, 0, len(elements))
for _, fn := range elements {
elem := fn()
if elem.isImage {
// 图片元素必须是独立的一个段落
es = append(es, p, []interface{}{elem.elem})
p = make([]interface{}, 0, len(elements))
} else {
p = append(p, elem.elem)
}
}
es = append(es, p)
return i18nPost{
lang: string(lang),
title: title,
elements: es,
}
}
}
type postElement struct {
elem interface{}
isImage bool // 图片元素必须是独立的一个段落
}
type PostElement func() postElement
// WithPostElementText 富文本消息的文字元素
//
// isUnescape 表示是不是 unescape 解码,默认为 false ,不用可以不填
func WithPostElementText(text string, isUnescape ...bool) PostElement {
return func() postElement {
elem := map[string]interface{}{
"tag": "text",
"text": text,
}
if len(isUnescape) != 0 && isUnescape[0] {
elem["un_escape"] = isUnescape[0]
}
return postElement{
elem: elem,
isImage: false,
}
}
}
// WithPostElementLink 富文本消息的文字超链接元素
func WithPostElementLink(text, href string) PostElement {
return func() postElement {
return postElement{
elem: map[string]interface{}{
"tag": "a",
"text": text,
"href": href,
},
isImage: false,
}
}
}
// WithPostElementImage 富文本消息的图片元素
func WithPostElementImage(imgKey string) PostElement {
return func() postElement {
return postElement{
elem: map[string]interface{}{
"tag": "img",
"image_key": imgKey,
},
isImage: true,
}
}
}
// WithPostElementMentionAll 富文本消息的 @所有人
func WithPostElementMentionAll() PostElement {
return func() postElement {
return postElement{
elem: map[string]interface{}{
"tag": "at",
"user_id": "all",
},
isImage: false,
}
}
}
// WithPostElementMentionByOpenID 富文本消息的 @用户
//
// Open ID 必须是有效值,否则仅显示 `@` 符号(实际效果不同于 PushText 时会显示 name)
//
// https://open.feishu.cn/document/ugTN1YjL4UTN24CO1UjN/uUzN1YjL1cTN24SN3UjN?from=mcb#acc98e1b
func WithPostElementMentionByOpenID(id string, name ...string) PostElement {
return func() postElement {
elem := map[string]interface{}{
"tag": "at",
"user_id": id,
}
if len(name) != 0 {
elem["user_name"] = name[0]
}
return postElement{
elem: elem,
isImage: false,
}
}
}
func newMsgPost(p Post, more ...Post) map[string]interface{} {
more = append([]Post{p}, more...)
i18nPosts := make([]i18nPost, 0, len(more))
for _, fn := range more {
i18nPosts = append(i18nPosts, fn())
}
post := make(map[string]interface{}, 3)
for _, p := range i18nPosts {
post[p.lang] = map[string]interface{}{
"title": p.title,
"content": p.elements,
}
}
msgType := "post"
return map[string]interface{}{
"msg_type": msgType,
"content": map[string]interface{}{
msgType: post,
},
}
}