-
Notifications
You must be signed in to change notification settings - Fork 1
/
lark.go
224 lines (189 loc) · 5.35 KB
/
lark.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
package main
import (
"encoding/json"
"fmt"
"regexp"
"strings"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
)
type MessageResponse struct {
MessageID string `json:"message_id"`
RootID string `json:"root_id"`
ParentID string `json:"parent_id"`
ThreadID string `json:"thread_id"`
MsgType string `json:"msg_type"`
Content Content `json:"content"`
}
// Function to map ReplyMessageResp to your custom MessageResponse
func mapToMessageResponse(replyResp *larkim.ReplyMessageResp) (*MessageResponse, error) {
if replyResp == nil || replyResp.Data == nil {
return nil, fmt.Errorf("invalid response: data is nil")
}
// var sender Sender
// idType := getStringValue(replyResp.Data.Sender.IdType)
// switch idType {
// case "open_id":
// sender.OpenID = getStringValue(replyResp.Data.Sender.Id)
// default: // No matching ID type
// return nil, fmt.Errorf("invalid sender ID type: %s", idType)
// }
messageResponse := &MessageResponse{
MessageID: getStringValue(replyResp.Data.MessageId),
RootID: getStringValue(replyResp.Data.RootId),
ParentID: getStringValue(replyResp.Data.ParentId),
ThreadID: getStringValue(replyResp.Data.ThreadId),
MsgType: getStringValue(replyResp.Data.MsgType),
}
// Handle the message body if it is present
if replyResp.Data.Body != nil && replyResp.Data.Body.Content != nil {
var content Content
content.Text = getStringValue(replyResp.Data.Body.Content)
messageResponse.Content = content
}
return messageResponse, nil
}
// Helper function to get string value from a pointer
func getStringValue(strPtr *string) string {
if strPtr != nil {
return *strPtr
}
return ""
}
type EventBody struct {
Event Event `json:"event"`
}
type Event struct {
Sender Sender `json:"sender"`
Message Message `json:"message"`
}
type Sender struct {
UnionID string `json:"union_id"`
UserID string `json:"user_id"`
OpenID string `json:"open_id"`
}
func (s *Sender) UnmarshalJSON(data []byte) error {
type Alias struct {
SenderID struct {
UnionId string `json:"union_id"`
UserId string `json:"user_id"`
OpenId string `json:"open_id"`
} `json:"sender_id"`
}
var a Alias
if err := json.Unmarshal(data, &a); err != nil {
return err
}
s.UnionID = a.SenderID.UnionId
s.UserID = a.SenderID.UserId
s.OpenID = a.SenderID.OpenId
return nil
}
func (s Sender) MarshalJSON() ([]byte, error) {
type Alias struct {
SenderID struct {
UnionId string `json:"union_id"`
UserId string `json:"user_id"`
OpenId string `json:"open_id"`
} `json:"sender_id"`
}
var a Alias
a.SenderID.UnionId = s.UnionID
a.SenderID.UserId = s.UserID
a.SenderID.OpenId = s.OpenID
return json.Marshal(a)
}
type Message struct {
MessageID string `json:"message_id"`
RootID string `json:"root_id"`
ParentID string `json:"parent_id"`
ThreadID string `json:"thread_id"`
Content Content `json:"content"`
Mentions []string `json:"mentions"`
// etc.
}
func (m *Message) UnmarshalJSON(data []byte) error {
type Alias struct {
MessageID string `json:"message_id"`
RootID string `json:"root_id"`
ParentID string `json:"parent_id"`
ThreadID string `json:"thread_id"`
Content string `json:"content"`
Mentions []struct {
Key string `json:"key"`
Name string `json:"name"`
} `json:"mentions"`
}
var a Alias
if err := json.Unmarshal(data, &a); err != nil {
return err
}
m.MessageID = a.MessageID
m.RootID = a.RootID
m.ParentID = a.ParentID
m.ThreadID = a.ThreadID
if err := json.Unmarshal([]byte(a.Content), &m.Content); err != nil {
return fmt.Errorf("Error unmarshalling Content field: %v", err)
}
// Remove mention keys from content text
if a.Mentions != nil {
var mentionKeys []string
for _, mention := range a.Mentions {
mentionKeys = append(mentionKeys, mention.Key)
m.Mentions = append(m.Mentions, mention.Name)
}
// Remove mention keys from the content text
cleanedText := removeMentions(m.Content.Text, mentionKeys)
m.Content.Text = cleanedText
}
return nil
}
func (m Message) MarshalJSON() ([]byte, error) {
type Alias struct {
MessageID string `json:"message_id"`
RootID string `json:"root_id"`
ParentID string `json:"parent_id"`
ThreadID string `json:"thread_id"`
Content string `json:"content"`
}
var a Alias
a.MessageID = m.MessageID
a.RootID = m.RootID
a.ParentID = m.ParentID
a.ThreadID = m.ThreadID
contentBytes, err := json.Marshal(m.Content)
if err != nil {
return nil, fmt.Errorf("Error marshalling Content field: %v", err)
}
a.Content = string(contentBytes)
return json.Marshal(a)
}
func (m *Message) ContainesBotMention() bool {
for _, m := range m.Mentions {
if m == "VM-Manager" {
return true
}
}
return false
}
func (m *Message) ContainsMention(mention string) bool {
for _, m := range m.Mentions {
if m == mention {
return true
}
}
return false
}
type Content struct {
Text string `json:"text"`
}
// Helper function to remove mention keys from content text
func removeMentions(text string, mentionKeys []string) string {
cleanedText := text
for _, key := range mentionKeys {
// Use regex to remove mention key along with any surrounding whitespace
regex := regexp.MustCompile(fmt.Sprintf(`\s*%s\s*`, regexp.QuoteMeta(key)))
cleanedText = regex.ReplaceAllString(cleanedText, " ")
}
// Trim any leading/trailing whitespace after removal
return strings.TrimSpace(cleanedText)
}