forked from go-lark/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_v2.go
106 lines (97 loc) · 2.09 KB
/
message_v2.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
package lark
import (
"encoding/json"
)
// BuildMessage .
func BuildMessage(om OutcomingMessage) (*IMMessageRequest, error) {
req := IMMessageRequest{
MsgType: string(om.MsgType),
Content: buildContent(om),
ReceiveID: buildReceiveID(om),
}
if req.ReceiveID == "" {
return nil, ErrInvalidReceiveID
}
if req.Content == "" {
return nil, ErrMessageNotBuild
}
if om.UUID != "" {
req.UUID = om.UUID
}
return &req, nil
}
func buildReplyMessage(om OutcomingMessage) (*IMMessageRequest, error) {
req := IMMessageRequest{
MsgType: string(om.MsgType),
Content: buildContent(om),
ReceiveID: buildReceiveID(om),
}
if req.Content == "" {
return nil, ErrMessageNotBuild
}
if om.ReplyInThread == true {
req.ReplyInThread = om.ReplyInThread
}
return &req, nil
}
func buildUpdateMessage(om OutcomingMessage) (*IMMessageRequest, error) {
req := IMMessageRequest{
Content: buildContent(om),
}
if om.MsgType != MsgInteractive {
req.MsgType = om.MsgType
}
if req.Content == "" {
return nil, ErrMessageNotBuild
}
return &req, nil
}
func buildContent(om OutcomingMessage) string {
var (
content = ""
b []byte
err error
)
switch om.MsgType {
case MsgText:
b, err = json.Marshal(om.Content.Text)
case MsgImage:
b, err = json.Marshal(om.Content.Image)
case MsgFile:
b, err = json.Marshal(om.Content.File)
case MsgShareCard:
b, err = json.Marshal(om.Content.ShareChat)
case MsgShareUser:
b, err = json.Marshal(om.Content.ShareUser)
case MsgPost:
b, err = json.Marshal(om.Content.Post)
case MsgInteractive:
b, err = json.Marshal(om.Content.Card)
case MsgAudio:
b, err = json.Marshal(om.Content.Audio)
case MsgMedia:
b, err = json.Marshal(om.Content.Media)
case MsgSticker:
b, err = json.Marshal(om.Content.Sticker)
}
if err != nil {
return ""
}
content = string(b)
return content
}
func buildReceiveID(om OutcomingMessage) string {
switch om.UIDType {
case UIDEmail:
return om.Email
case UIDUserID:
return om.UserID
case UIDOpenID:
return om.OpenID
case UIDChatID:
return om.ChatID
case UIDUnionID:
return om.UnionID
}
return ""
}