forked from go-lark/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_message.go
396 lines (354 loc) · 12.1 KB
/
api_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
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
package lark
import "fmt"
const (
messageURL = "/open-apis/im/v1/messages?receive_id_type=%s"
replyMessageURL = "/open-apis/im/v1/messages/%s/reply"
reactionsMessageURL = "/open-apis/im/v1/messages/%s/reactions"
deleteReactionsMessageURL = "/open-apis/im/v1/messages/%s/reactions/%s"
getMessageURL = "/open-apis/im/v1/messages/%s"
updateMessageURL = "/open-apis/im/v1/messages/%s"
recallMessageURL = "/open-apis/im/v1/messages/%s"
messageReceiptURL = "/open-apis/message/v4/read_info/"
ephemeralMessageURL = "/open-apis/ephemeral/v1/send"
deleteEphemeralMessageURL = "/open-apis/ephemeral/v1/delete"
pinMessageURL = "/open-apis/im/v1/pins"
unpinMessageURL = "/open-apis/im/v1/pins/%s"
forwardMessageURL = "/open-apis/im/v1/messages/%s/forward?receive_id_type=%s"
)
// PostMessageResponse .
type PostMessageResponse struct {
BaseResponse
Data IMMessage `json:"data"`
}
// IMMessageRequest .
type IMMessageRequest struct {
Content string `json:"content"`
MsgType string `json:"msg_type,omitempty"`
ReceiveID string `json:"receive_id,omitempty"`
UUID string `json:"uuid,omitempty"`
ReplyInThread bool `json:"reply_in_thread,omitempty"`
}
// IMSender .
type IMSender struct {
ID string `json:"id"`
IDType string `json:"id_type"`
SenderType string `json:"sender_type"`
TenantKey string `json:"tenant_key"`
}
// IMMention .
type IMMention struct {
ID string `json:"id"`
IDType string `json:"id_type"`
Key string `json:"key"`
Name string `json:"name"`
}
// IMBody .
type IMBody struct {
Content string `json:"content"`
}
// IMMessage .
type IMMessage struct {
MessageID string `json:"message_id"`
UpperMessageID string `json:"upper_message_id"`
RootID string `json:"root_id"`
ParentID string `json:"parent_id"`
ThreadID string `json:"thread_id"`
ChatID string `json:"chat_id"`
MsgType string `json:"msg_type"`
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
Deleted bool `json:"deleted"`
Updated bool `json:"updated"`
Sender IMSender `json:"sender"`
Mentions []IMMention `json:"mentions"`
Body IMBody `json:"body"`
}
// ReactionResponse .
type ReactionResponse struct {
BaseResponse
Data struct {
ReactionID string `json:"reaction_id"`
Operator struct {
OperatorID string `json:"operator_id"`
OperatorType string `json:"operator_type"`
ActionTime string `json:"action_time"`
} `json:"operator"`
ReactionType struct {
EmojiType EmojiType `json:"emoji_type"`
} `json:"reaction_type"`
} `json:"data"`
}
// GetMessageResponse .
type GetMessageResponse struct {
BaseResponse
Data struct {
Items []IMMessage `json:"items"`
} `json:"data"`
}
// PostEphemeralMessageResponse .
type PostEphemeralMessageResponse struct {
BaseResponse
Data struct {
MessageID string `json:"message_id"`
} `json:"data"`
}
// DeleteEphemeralMessageResponse .
type DeleteEphemeralMessageResponse = BaseResponse
// RecallMessageResponse .
type RecallMessageResponse = BaseResponse
// UpdateMessageResponse .
type UpdateMessageResponse = BaseResponse
// ForwardMessageResponse .
type ForwardMessageResponse = PostMessageResponse
// MessageReceiptResponse .
type MessageReceiptResponse struct {
BaseResponse
Data struct {
ReadUsers []struct {
OpenID string `json:"open_id"`
UserID string `json:"user_id"`
Timestamp string `json:"timestamp"`
} `json:"read_users"`
} `json:"data"`
}
// PinMessageResponse .
type PinMessageResponse struct {
BaseResponse
Data struct {
Pin struct {
MessageID string `json:"message_id"`
ChatID string `json:"chat_id"`
OperatorID string `json:"operator_id"`
OperatorIDType string `json:"operator_id_type"`
CreateTime string `json:"create_time"`
} `json:"pin"`
} `json:"data"`
}
// UnpinMessageResponse .
type UnpinMessageResponse = BaseResponse
func newMsgBufWithOptionalUserID(msgType string, userID *OptionalUserID) *MsgBuffer {
mb := NewMsgBuffer(msgType)
realID := userID.RealID
switch userID.UIDType {
case "email":
mb.BindEmail(realID)
case "open_id":
mb.BindOpenID(realID)
case "chat_id":
mb.BindChatID(realID)
case "user_id":
mb.BindUserID(realID)
case "union_id":
mb.BindUnionID(realID)
default:
return nil
}
return mb
}
// PostText is a simple way to send text messages
func (bot Bot) PostText(text string, userID *OptionalUserID) (*PostMessageResponse, error) {
mb := newMsgBufWithOptionalUserID(MsgText, userID)
if mb == nil {
return nil, ErrParamUserID
}
om := mb.Text(text).Build()
return bot.PostMessage(om)
}
// PostRichText is a simple way to send rich text messages
func (bot Bot) PostRichText(postContent *PostContent, userID *OptionalUserID) (*PostMessageResponse, error) {
mb := newMsgBufWithOptionalUserID(MsgPost, userID)
if mb == nil {
return nil, ErrParamUserID
}
om := mb.Post(postContent).Build()
return bot.PostMessage(om)
}
// PostTextMention is a simple way to send text messages with @user
func (bot Bot) PostTextMention(text string, atUserID string, userID *OptionalUserID) (*PostMessageResponse, error) {
mb := newMsgBufWithOptionalUserID(MsgText, userID)
if mb == nil {
return nil, ErrParamUserID
}
tb := NewTextBuilder()
om := mb.Text(tb.Text(text).Mention(atUserID).Render()).Build()
return bot.PostMessage(om)
}
// PostTextMentionAll is a simple way to send text messages with @all
func (bot Bot) PostTextMentionAll(text string, userID *OptionalUserID) (*PostMessageResponse, error) {
mb := newMsgBufWithOptionalUserID(MsgText, userID)
if mb == nil {
return nil, ErrParamUserID
}
tb := NewTextBuilder()
om := mb.Text(tb.Text(text).MentionAll().Render()).Build()
return bot.PostMessage(om)
}
// PostTextMentionAndReply is a simple way to send text messages with @user and reply a message
func (bot Bot) PostTextMentionAndReply(text string, atUserID string, userID *OptionalUserID, replyID string) (*PostMessageResponse, error) {
mb := newMsgBufWithOptionalUserID(MsgText, userID)
if mb == nil {
return nil, ErrParamUserID
}
tb := NewTextBuilder()
om := mb.Text(tb.Text(text).Mention(atUserID).Render()).BindReply(replyID).Build()
return bot.PostMessage(om)
}
// PostImage is a simple way to send image
func (bot Bot) PostImage(imageKey string, userID *OptionalUserID) (*PostMessageResponse, error) {
mb := newMsgBufWithOptionalUserID(MsgImage, userID)
if mb == nil {
return nil, ErrParamUserID
}
om := mb.Image(imageKey).Build()
return bot.PostMessage(om)
}
// PostShareChat is a simple way to share chat
func (bot Bot) PostShareChat(chatID string, userID *OptionalUserID) (*PostMessageResponse, error) {
mb := newMsgBufWithOptionalUserID(MsgShareCard, userID)
if mb == nil {
return nil, ErrParamUserID
}
om := mb.ShareChat(chatID).Build()
return bot.PostMessage(om)
}
// PostShareUser is a simple way to share user
func (bot Bot) PostShareUser(openID string, userID *OptionalUserID) (*PostMessageResponse, error) {
mb := newMsgBufWithOptionalUserID(MsgShareUser, userID)
if mb == nil {
return nil, ErrParamUserID
}
om := mb.ShareUser(openID).Build()
return bot.PostMessage(om)
}
// PostMessage posts a message
func (bot Bot) PostMessage(om OutcomingMessage) (*PostMessageResponse, error) {
req, err := BuildMessage(om)
if err != nil {
return nil, err
}
var respData PostMessageResponse
if om.RootID == "" {
err = bot.PostAPIRequest("PostMessage", fmt.Sprintf(messageURL, om.UIDType), true, req, &respData)
} else {
resp, err := bot.ReplyMessage(om)
return resp, err
}
return &respData, err
}
// ReplyMessage replies a message
func (bot Bot) ReplyMessage(om OutcomingMessage) (*PostMessageResponse, error) {
req, err := buildReplyMessage(om)
if err != nil {
return nil, err
}
if om.RootID == "" {
return nil, ErrParamMessageID
}
var respData PostMessageResponse
err = bot.PostAPIRequest("ReplyMessage", fmt.Sprintf(replyMessageURL, om.RootID), true, req, &respData)
return &respData, err
}
// AddReaction adds reaction to a message
func (bot Bot) AddReaction(messageID string, emojiType EmojiType) (*ReactionResponse, error) {
req := map[string]interface{}{
"reaction_type": map[string]interface{}{
"emoji_type": emojiType,
},
}
var respData ReactionResponse
err := bot.PostAPIRequest("AddReaction", fmt.Sprintf(reactionsMessageURL, messageID), true, req, &respData)
return &respData, err
}
// DeleteReaction deletes reaction of a message
func (bot Bot) DeleteReaction(messageID string, reactionID string) (*ReactionResponse, error) {
var respData ReactionResponse
err := bot.DeleteAPIRequest("DeleteReaction", fmt.Sprintf(deleteReactionsMessageURL, messageID, reactionID), true, nil, &respData)
return &respData, err
}
// UpdateMessage updates a message
func (bot Bot) UpdateMessage(messageID string, om OutcomingMessage) (*UpdateMessageResponse, error) {
if om.MsgType != MsgInteractive &&
om.MsgType != MsgText &&
om.MsgType != MsgPost {
return nil, ErrMessageType
}
req, err := buildUpdateMessage(om)
if err != nil {
return nil, err
}
url := fmt.Sprintf(updateMessageURL, messageID)
var respData UpdateMessageResponse
if om.MsgType == MsgInteractive {
err = bot.PatchAPIRequest("UpdateMessage", url, true, req, &respData)
} else {
err = bot.PutAPIRequest("UpdateMessage", url, true, req, &respData)
}
return &respData, err
}
// GetMessage gets a message with im/v1
func (bot Bot) GetMessage(messageID string) (*GetMessageResponse, error) {
var respData GetMessageResponse
err := bot.GetAPIRequest("GetMessage", fmt.Sprintf(getMessageURL, messageID), true, nil, &respData)
return &respData, err
}
// RecallMessage recalls a message with ID
func (bot Bot) RecallMessage(messageID string) (*RecallMessageResponse, error) {
url := fmt.Sprintf(recallMessageURL, messageID)
var respData RecallMessageResponse
err := bot.DeleteAPIRequest("RecallMessage", url, true, nil, &respData)
return &respData, err
}
// MessageReadReceipt queries message read receipt
func (bot Bot) MessageReadReceipt(messageID string) (*MessageReceiptResponse, error) {
params := map[string]interface{}{
"message_id": messageID,
}
var respData MessageReceiptResponse
err := bot.PostAPIRequest("MessageReadReceipt", messageReceiptURL, true, params, &respData)
return &respData, err
}
// PostEphemeralMessage posts an ephemeral message
func (bot Bot) PostEphemeralMessage(om OutcomingMessage) (*PostEphemeralMessageResponse, error) {
if om.UIDType == UIDUnionID {
return nil, ErrUnsupportedUIDType
}
params := BuildOutcomingMessageReq(om)
var respData PostEphemeralMessageResponse
err := bot.PostAPIRequest("PostEphemeralMessage", ephemeralMessageURL, true, params, &respData)
return &respData, err
}
// DeleteEphemeralMessage deletes an ephemeral message
func (bot Bot) DeleteEphemeralMessage(messageID string) (*DeleteEphemeralMessageResponse, error) {
params := map[string]interface{}{
"message_id": messageID,
}
var respData DeleteEphemeralMessageResponse
err := bot.PostAPIRequest("DeleteEphemeralMessage", deleteEphemeralMessageURL, true, params, &respData)
return &respData, err
}
// PinMessage pins a message
func (bot Bot) PinMessage(messageID string) (*PinMessageResponse, error) {
params := map[string]interface{}{
"message_id": messageID,
}
var respData PinMessageResponse
err := bot.PostAPIRequest("PinMessage", pinMessageURL, true, params, &respData)
return &respData, err
}
// UnpinMessage unpins a message
func (bot Bot) UnpinMessage(messageID string) (*UnpinMessageResponse, error) {
url := fmt.Sprintf(unpinMessageURL, messageID)
var respData UnpinMessageResponse
err := bot.DeleteAPIRequest("PinMessage", url, true, nil, &respData)
return &respData, err
}
// ForwardMessage forwards a message
func (bot Bot) ForwardMessage(messageID string, receiveID *OptionalUserID) (*ForwardMessageResponse, error) {
url := fmt.Sprintf(forwardMessageURL, messageID, receiveID.UIDType)
params := map[string]interface{}{
"receive_id": receiveID.RealID,
}
var respData ForwardMessageResponse
err := bot.PostAPIRequest("ForwardMessage", url, true, params, &respData)
return &respData, err
}