-
Notifications
You must be signed in to change notification settings - Fork 1
/
proto.go
240 lines (194 loc) · 4.99 KB
/
proto.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
package chatbot
import (
"bytes"
"github.com/golang/protobuf/proto"
"github.com/nicksnyder/go-i18n/v2/i18n"
chatbotbase "github.com/zhs007/chatbot/base"
chatbotpb "github.com/zhs007/chatbot/chatbotpb"
)
// BuildUserAppInfo - build UserAppInfo
func BuildUserAppInfo(appType chatbotpb.ChatAppType, usernameAppServ string,
appuid string, appuname string, lang string) *chatbotpb.UserAppInfo {
return &chatbotpb.UserAppInfo{
App: appType,
UsernameAppServ: usernameAppServ,
Appuid: appuid,
Appuname: appuname,
Lang: lang,
}
}
// BuildGroupAppInfo - build GroupAppInfo
func BuildGroupAppInfo(appType chatbotpb.ChatAppType, usernameAppServ string,
groupid string, groupname string) *chatbotpb.GroupAppInfo {
return &chatbotpb.GroupAppInfo{
App: appType,
UsernameAppServ: usernameAppServ,
Groupid: groupid,
Groupname: groupname,
}
}
// BuildTextChatMsg - build ChatMsg
func BuildTextChatMsg(msg string, uai *chatbotpb.UserAppInfo, token string,
sessionid string) *chatbotpb.ChatMsg {
return &chatbotpb.ChatMsg{
Msg: msg,
Uai: uai,
Token: token,
SessionID: sessionid,
}
}
// BuildFileChatMsg - build ChatMsg
func BuildFileChatMsg(msg string, fd *chatbotpb.FileData, uai *chatbotpb.UserAppInfo, token string,
sessionid string) *chatbotpb.ChatMsg {
return &chatbotpb.ChatMsg{
Msg: msg,
Uai: uai,
Token: token,
SessionID: sessionid,
File: fd,
}
}
// BuildErrorChatMsg - build ChatMsg
func BuildErrorChatMsg(err error, uai *chatbotpb.UserAppInfo, token string,
sessionid string) *chatbotpb.ChatMsg {
return &chatbotpb.ChatMsg{
Error: err.Error(),
Uai: uai,
Token: token,
SessionID: sessionid,
}
}
// BuildChatMsgStream - ChatMsg -> ChatMsgStream
func BuildChatMsgStream(chat *chatbotpb.ChatMsg) ([]*chatbotpb.ChatMsgStream, error) {
buf, err := proto.Marshal(chat)
if err != nil {
return nil, err
}
bl := len(buf)
if bl <= chatbotbase.BigMsgLength {
stream := &chatbotpb.ChatMsgStream{
Chat: chat,
Token: chat.Token,
SessionID: chat.SessionID,
}
return []*chatbotpb.ChatMsgStream{stream}, nil
}
var lst []*chatbotpb.ChatMsgStream
st := 0
for st < bl {
isend := false
cl := chatbotbase.BigMsgLength
if cl > bl-st {
cl = bl - st
isend = true
}
cb := buf[st:(st + cl)]
cs := &chatbotpb.ChatMsgStream{
TotalLength: int32(bl),
CurStart: int32(st),
CurLength: int32(cl),
HashData: chatbotbase.MD5Buffer(cb),
Data: cb,
Token: chat.Token,
SessionID: chat.SessionID,
}
st += cl
if isend {
cs.TotalHashData = chatbotbase.MD5Buffer(buf)
}
lst = append(lst, cs)
}
return lst, nil
}
// BuildChatMsg - []ChatMsgStream => ChatMsg
func BuildChatMsg(lst []*chatbotpb.ChatMsgStream) (*chatbotpb.ChatMsg, error) {
if len(lst) == 1 {
if lst[0].Chat != nil {
return lst[0].Chat, nil
}
return nil, chatbotbase.ErrStreamNoMsg
}
var lstbytes [][]byte
totalmd5inmsg := ""
st := 0
ct := 0
for i, v := range lst {
if st != int(v.CurStart) {
return nil, chatbotbase.ErrInvalidStartInStream
}
if len(v.Data) != int(v.CurLength) {
return nil, chatbotbase.ErrInvalidLengthInStream
}
curmd5 := chatbotbase.MD5Buffer(v.Data)
if curmd5 != v.HashData {
return nil, chatbotbase.ErrInvalidHashInStream
}
lstbytes = append(lstbytes, v.Data)
st += len(v.Data)
ct += len(v.Data)
if i == len(lst)-1 {
totalmd5inmsg = v.TotalHashData
if ct != int(v.TotalLength) {
return nil, chatbotbase.ErrInvalidTotalLengthInStream
}
}
}
buf := bytes.Join(lstbytes, []byte(""))
totalmd5 := chatbotbase.MD5Buffer(buf)
if totalmd5 != totalmd5inmsg {
return nil, chatbotbase.ErrInvalidTotalHashInStream
}
chatmsg := &chatbotpb.ChatMsg{}
err := proto.Unmarshal(buf, chatmsg)
if err != nil {
return nil, err
}
return chatmsg, nil
}
// BuildChatMsgList - []ChatMsgStream => []ChatMsg
func BuildChatMsgList(lst []*chatbotpb.ChatMsgStream) ([]*chatbotpb.ChatMsg, error) {
var lstret []*chatbotpb.ChatMsg
var curlst []*chatbotpb.ChatMsgStream
for _, v := range lst {
if v.IsNoMsg {
continue
}
if v.Chat != nil {
cr, err := BuildChatMsg([]*chatbotpb.ChatMsgStream{v})
if err != nil {
return nil, err
}
if cr != nil {
lstret = append(lstret, cr)
}
} else {
curlst = append(curlst, v)
if v.TotalHashData != "" {
cr, err := BuildChatMsg([]*chatbotpb.ChatMsgStream{v})
if err != nil {
return nil, err
}
if cr != nil {
lstret = append(lstret, cr)
}
curlst = nil
}
}
}
return lstret, nil
}
// NewChatMsgWithText - new ChatMsg with TextMgr
func NewChatMsgWithText(l *i18n.Localizer, msgid string, tempParams map[string]interface{}, uai *chatbotpb.UserAppInfo) (
*chatbotpb.ChatMsg, error) {
txt, err := l.Localize(&i18n.LocalizeConfig{
MessageID: msgid,
TemplateData: tempParams,
})
if err != nil {
return nil, err
}
return &chatbotpb.ChatMsg{
Msg: txt,
Uai: uai,
}, nil
}