-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
miraigo.go
188 lines (168 loc) · 4.25 KB
/
miraigo.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
package utils
import (
"bytes"
"errors"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Sora233/MiraiGo-Template/bot"
)
func MessageFilter(msg []message.IMessageElement, filter func(message.IMessageElement) bool) []message.IMessageElement {
var result []message.IMessageElement
for _, e := range msg {
if filter(e) {
result = append(result, e)
}
}
return result
}
func UploadGroupImageByUrl(groupCode int64, url string, isNorm bool) (*message.GroupImageElement, error) {
img, err := ImageGet(url)
if err != nil {
return nil, err
}
return UploadGroupImage(groupCode, img, isNorm)
}
func UploadGroupImage(groupCode int64, img []byte, isNorm bool) (image *message.GroupImageElement, err error) {
if isNorm {
img, err = ImageNormSize(img)
if err != nil {
return nil, err
}
}
if !GetBot().IsOnline() {
return nil, errors.New("bot offline")
}
return bot.Instance.UploadGroupImage(groupCode, bytes.NewReader(img))
}
func UploadPrivateImage(uin int64, img []byte, isNorm bool) (*message.FriendImageElement, error) {
var err error
if isNorm {
img, err = ImageNormSize(img)
if err != nil {
return nil, err
}
}
if !GetBot().IsOnline() {
return nil, errors.New("bot offline")
}
return bot.Instance.UploadPrivateImage(uin, bytes.NewReader(img))
}
const (
internalMsgTypeGroup = "group"
)
type internalMsg struct {
Type string `json:"type"`
MsgInfo string `json:"msg_info"`
ElementString string `json:"element_string"`
}
func SerializationGroupMsg(m *message.GroupMessage) (string, error) {
elems := m.Elements
m.Elements = nil
defer func() {
m.Elements = elems
}()
mString, err := json.MarshalToString(m)
if err != nil {
return "", err
}
elemString, err := SerializationElement(elems)
if err != nil {
return "", err
}
imsg := &internalMsg{
Type: internalMsgTypeGroup,
MsgInfo: mString,
ElementString: elemString,
}
return json.MarshalToString(imsg)
}
func DeserializationGroupMsg(r string) (*message.GroupMessage, error) {
var imsg *internalMsg
err := json.UnmarshalFromString(r, &imsg)
if err != nil {
return nil, err
}
var m *message.GroupMessage
err = json.UnmarshalFromString(imsg.MsgInfo, &m)
if err != nil {
return nil, err
}
elems, err := DeserializationElement(imsg.ElementString)
if err != nil {
return nil, err
}
m.Elements = elems
return m, nil
}
type internalElem struct {
Type string `json:"type"`
Content string `json:"content"`
}
const (
internalTypeText = "text"
internalTypeGroupImage = "group_image"
internalTypeFriendImage = "friend_image"
)
// SerializationElement 序列化消息,只支持图片,文字
func SerializationElement(e []message.IMessageElement) (string, error) {
var tmp []*internalElem
for _, elem := range e {
switch o := elem.(type) {
case *message.TextElement:
b, _ := json.Marshal(o)
tmp = append(tmp, &internalElem{
Type: internalTypeText,
Content: string(b),
})
case *message.GroupImageElement:
b, _ := json.Marshal(o)
tmp = append(tmp, &internalElem{
Type: internalTypeGroupImage,
Content: string(b),
})
case *message.FriendImageElement:
b, _ := json.Marshal(o)
tmp = append(tmp, &internalElem{
Type: internalTypeFriendImage,
Content: string(b),
})
default:
panic("unsupported element type")
}
}
s, err := json.MarshalToString(tmp)
return s, err
}
// DeserializationElement 反序列化消息,只支持图片,文字
func DeserializationElement(r string) ([]message.IMessageElement, error) {
var tmp []*internalElem
err := json.Unmarshal([]byte(r), &tmp)
if err != nil {
return nil, err
}
var result []message.IMessageElement
for _, e := range tmp {
switch e.Type {
case internalTypeGroupImage:
var elem *message.GroupImageElement
json.UnmarshalFromString(e.Content, &elem)
if elem != nil {
result = append(result, elem)
}
case internalTypeText:
var elem *message.TextElement
json.UnmarshalFromString(e.Content, &elem)
if elem != nil {
result = append(result, elem)
}
case internalTypeFriendImage:
var elem *message.FriendImageElement
json.UnmarshalFromString(e.Content, &elem)
if elem != nil {
result = append(result, elem)
}
default:
panic("unsupported element type")
}
}
return result, nil
}