-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
msg_buf.go
255 lines (226 loc) · 5.49 KB
/
msg_buf.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
package lark
import (
"encoding/json"
"fmt"
)
// MsgBuffer stores all the messages attached
// You can call every function, but some of which is only available for specific condition
type MsgBuffer struct {
// Message type
msgType string
// Output
message OutcomingMessage
err error
}
// NewMsgBuffer create a message buffer
func NewMsgBuffer(newMsgType string) *MsgBuffer {
msgBuffer := MsgBuffer{
message: OutcomingMessage{
MsgType: newMsgType,
},
msgType: newMsgType,
}
return &msgBuffer
}
// BindOpenID binds open_id
func (m *MsgBuffer) BindOpenID(openID string) *MsgBuffer {
m.message.OpenID = openID
m.message.UIDType = UIDOpenID
return m
}
// BindEmail binds email
func (m *MsgBuffer) BindEmail(email string) *MsgBuffer {
m.message.Email = email
m.message.UIDType = UIDEmail
return m
}
// BindChatID binds chat_id
func (m *MsgBuffer) BindChatID(chatID string) *MsgBuffer {
m.message.ChatID = chatID
m.message.UIDType = UIDChatID
return m
}
// BindOpenChatID binds open_chat_id
func (m *MsgBuffer) BindOpenChatID(openChatID string) *MsgBuffer {
m.BindChatID(openChatID)
m.message.UIDType = UIDChatID
return m
}
// BindUserID binds open_id
func (m *MsgBuffer) BindUserID(userID string) *MsgBuffer {
m.message.UserID = userID
m.message.UIDType = UIDUserID
return m
}
// BindUnionID binds union_id
func (m *MsgBuffer) BindUnionID(unionID string) *MsgBuffer {
m.message.UnionID = unionID
m.message.UIDType = UIDUnionID
return m
}
// BindReply binds root id for reply
// rootID is OpenMessageID of the message you reply
func (m *MsgBuffer) BindReply(rootID string) *MsgBuffer {
m.message.RootID = rootID
return m
}
// ReplyInThread replies message in thread
func (m *MsgBuffer) ReplyInThread(replyInThread bool) *MsgBuffer {
m.message.ReplyInThread = replyInThread
return m
}
// WithSign generates sign for notification bot check
func (m *MsgBuffer) WithSign(secret string, ts int64) *MsgBuffer {
m.message.Sign, _ = GenSign(secret, ts)
m.message.Timestamp = ts
return m
}
// WithUUID add UUID to message for idempotency
func (m *MsgBuffer) WithUUID(uuid string) *MsgBuffer {
m.message.UUID = uuid
return m
}
func (m MsgBuffer) typeError(funcName string, msgType string) error {
return fmt.Errorf("`%s` is only available to `%s`", funcName, msgType)
}
// Text attaches text
func (m *MsgBuffer) Text(text string) *MsgBuffer {
if m.msgType != MsgText {
m.err = m.typeError("Text", MsgText)
return m
}
m.message.Content.Text = &TextContent{
Text: text,
}
return m
}
// Image attaches image key
// for MsgImage only
func (m *MsgBuffer) Image(imageKey string) *MsgBuffer {
if m.msgType != MsgImage {
m.err = m.typeError("Image", MsgImage)
return m
}
m.message.Content.Image = &ImageContent{
ImageKey: imageKey,
}
return m
}
// ShareChat attaches chat id
// for MsgShareChat only
func (m *MsgBuffer) ShareChat(chatID string) *MsgBuffer {
if m.msgType != MsgShareCard {
m.err = m.typeError("ShareChat", MsgShareCard)
return m
}
m.message.Content.ShareChat = &ShareChatContent{
ChatID: chatID,
}
return m
}
// ShareUser attaches user id
// for MsgShareUser only
func (m *MsgBuffer) ShareUser(userID string) *MsgBuffer {
if m.msgType != MsgShareUser {
m.err = m.typeError("ShareUser", MsgShareUser)
return m
}
m.message.Content.ShareUser = &ShareUserContent{
UserID: userID,
}
return m
}
// File attaches file
// for MsgFile only
func (m *MsgBuffer) File(fileKey string) *MsgBuffer {
if m.msgType != MsgFile {
m.err = m.typeError("File", MsgFile)
return m
}
m.message.Content.File = &FileContent{
FileKey: fileKey,
}
return m
}
// Audio attaches audio
// for MsgAudio only
func (m *MsgBuffer) Audio(fileKey string) *MsgBuffer {
if m.msgType != MsgAudio {
m.err = m.typeError("Audio", MsgAudio)
return m
}
m.message.Content.Audio = &AudioContent{
FileKey: fileKey,
}
return m
}
// Media attaches media
// for MsgMedia only
func (m *MsgBuffer) Media(fileKey, imageKey string) *MsgBuffer {
if m.msgType != MsgMedia {
m.err = m.typeError("Media", MsgMedia)
return m
}
m.message.Content.Media = &MediaContent{
FileKey: fileKey,
ImageKey: imageKey,
}
return m
}
// Sticker attaches sticker
// for MsgSticker only
func (m *MsgBuffer) Sticker(fileKey string) *MsgBuffer {
if m.msgType != MsgSticker {
m.err = m.typeError("Sticker", MsgSticker)
return m
}
m.message.Content.Sticker = &StickerContent{
FileKey: fileKey,
}
return m
}
// Post sets raw post content
func (m *MsgBuffer) Post(postContent *PostContent) *MsgBuffer {
if m.msgType != MsgPost {
m.err = m.typeError("Post", MsgPost)
return m
}
m.message.Content.Post = postContent
return m
}
// Card binds card content with V4 format
func (m *MsgBuffer) Card(cardContent string) *MsgBuffer {
if m.msgType != MsgInteractive {
m.err = m.typeError("Card", MsgInteractive)
return m
}
card := make(CardContent)
_ = json.Unmarshal([]byte(cardContent), &card)
m.message.Content.Card = &card
return m
}
// Template sets raw template content
func (m *MsgBuffer) Template(tempateContent *TemplateContent) *MsgBuffer {
if m.msgType != MsgInteractive {
m.err = m.typeError("Template", MsgInteractive)
return m
}
m.message.Content.Template = tempateContent
return m
}
// Build message and return message body
func (m *MsgBuffer) Build() OutcomingMessage {
return m.message
}
// Error returns last error
func (m *MsgBuffer) Error() error {
return m.err
}
// Clear message in buffer
func (m *MsgBuffer) Clear() *MsgBuffer {
m.message = OutcomingMessage{
MsgType: m.msgType,
}
m.err = nil
return m
}