forked from huoxue1/leafbot
/
message.go
322 lines (292 loc) · 8.24 KB
/
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
package message
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
jsoniter "github.com/json-iterator/go"
)
// Message impl the array form of message
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/array.md#%E6%95%B0%E7%BB%84%E6%A0%BC%E5%BC%8F
type Message []MessageSegment
// MessageSegment impl the single message
// MessageSegment 消息数组
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/array.md#%E6%95%B0%E7%BB%84%E6%A0%BC%E5%BC%8F
type MessageSegment struct {
Type string `json:"type"`
Data map[string]string `json:"data"`
}
// EscapeCQText escapes special characters in a non-media plain message.\
//
// CQ码字符转换
func EscapeCQText(str string) string {
str = strings.ReplaceAll(str, "&", "&")
str = strings.ReplaceAll(str, "[", "[")
str = strings.ReplaceAll(str, "]", "]")
return str
}
// UnescapeCQText unescapes special characters in a non-media plain message.
//
// CQ码反解析
func UnescapeCQText(str string) string {
str = strings.ReplaceAll(str, "]", "]")
str = strings.ReplaceAll(str, "[", "[")
str = strings.ReplaceAll(str, "&", "&")
return str
}
// EscapeCQCodeText escapes special characters in a cqcode value.
//
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/string.md#%E8%BD%AC%E4%B9%89
//
// cq码字符转换
func EscapeCQCodeText(str string) string {
str = strings.ReplaceAll(str, "&", "&")
str = strings.ReplaceAll(str, "[", "[")
str = strings.ReplaceAll(str, "]", "]")
str = strings.ReplaceAll(str, ",", ",")
return str
}
// UnescapeCQCodeText unescapes special characters in a cqcode value.
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/string.md#%E8%BD%AC%E4%B9%89
//
// cq码反解析
func UnescapeCQCodeText(str string) string {
str = strings.ReplaceAll(str, ",", ",")
str = strings.ReplaceAll(str, "]", "]")
str = strings.ReplaceAll(str, "[", "[")
str = strings.ReplaceAll(str, "&", "&")
return str
}
// CQCode 将数组消息转换为CQ码
func (m MessageSegment) CQCode() string {
cqcode := "[CQ:" + m.Type // 消息类型
for k, v := range m.Data { // 消息参数
cqcode = cqcode + "," + k + "=" + EscapeCQCodeText(v)
}
return cqcode + "]"
}
// Text 纯文本
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E7%BA%AF%E6%96%87%E6%9C%AC
func Text(text ...interface{}) MessageSegment {
return MessageSegment{
Type: "text",
Data: map[string]string{
"text": fmt.Sprint(text...),
},
}
}
// Face QQ表情
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#qq-%E8%A1%A8%E6%83%85
func Face(id int) MessageSegment {
return MessageSegment{
Type: "face",
Data: map[string]string{
"id": strconv.Itoa(id),
},
}
}
// ImageC 普通图片
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E5%9B%BE%E7%89%87
func ImageC(file interface{}) MessageSegment {
switch file.(type) {
case string:
if strings.HasPrefix(file.(string), "http://") || strings.HasPrefix(file.(string), "https://") {
return MessageSegment{
Type: "image",
Data: map[string]string{
"file": file.(string),
},
}
} else {
return MessageSegment{
Type: "image",
Data: map[string]string{
"file": "file:///" + file.(string),
},
}
}
case []byte:
return MessageSegment{
Type: "image",
Data: map[string]string{
"file": base64.StdEncoding.EncodeToString(file.([]byte)),
},
}
default:
return MessageSegment{
Type: "image",
Data: map[string]string{
"file": file.(string),
},
}
}
}
// Image 普通图片
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E5%9B%BE%E7%89%87
func Image(file string) MessageSegment {
return MessageSegment{
Type: "image",
Data: map[string]string{
"file": file,
},
}
}
// Record 语音
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E8%AF%AD%E9%9F%B3
func Record(file string) MessageSegment {
return MessageSegment{
Type: "record",
Data: map[string]string{
"file": file,
},
}
}
// At @某人
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E6%9F%90%E4%BA%BA
func At(qq int64) MessageSegment {
return MessageSegment{
Type: "at",
Data: map[string]string{
"qq": strconv.FormatInt(qq, 10),
},
}
}
// Music 音乐分享
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E9%9F%B3%E4%B9%90%E5%88%86%E4%BA%AB-
func Music(mType string, id int64) MessageSegment {
return MessageSegment{
Type: "music",
Data: map[string]string{
"type": mType,
"id": strconv.FormatInt(id, 10),
},
}
}
// CustomMusic 音乐自定义分享
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E9%9F%B3%E4%B9%90%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%86%E4%BA%AB-
func CustomMusic(url, audio, title string) MessageSegment {
return MessageSegment{
Type: "music",
Data: map[string]string{
"type": "custom",
"url": url,
"audio": audio,
"title": title,
},
}
}
// Reply 回复
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E5%9B%9E%E5%A4%8D
func Reply(id int64) MessageSegment {
return MessageSegment{
Type: "reply",
Data: map[string]string{
"id": strconv.FormatInt(id, 10),
},
}
}
// Forward 合并转发
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91-
func Forward(id string) MessageSegment {
return MessageSegment{
Type: "forward",
Data: map[string]string{
"id": id,
},
}
}
// Node 合并转发节点
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91%E8%8A%82%E7%82%B9-
func Node(id int64) MessageSegment {
return MessageSegment{
Type: "node",
Data: map[string]string{
"id": strconv.FormatInt(id, 10),
},
}
}
// CustomNode 自定义合并转发节点
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8A%82%E7%82%B9
func CustomNode(nickname string, userID int64, content interface{}) MessageSegment {
var str string
if s, ok := content.(string); ok {
str = s
} else {
str, _ = jsoniter.MarshalToString(content)
}
return MessageSegment{
Type: "node",
Data: map[string]string{
"uin": strconv.FormatInt(userID, 10),
"name": nickname,
"content": str,
},
}
}
// XML 消息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#xml-%E6%B6%88%E6%81%AF
func XML(data string) MessageSegment {
return MessageSegment{
Type: "xml",
Data: map[string]string{
"data": data,
},
}
}
// JSON 消息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#xml-%E6%B6%88%E6%81%AF
func JSON(data string) MessageSegment {
return MessageSegment{
Type: "json",
Data: map[string]string{
"data": data,
},
}
}
// Expand CQCode
// Gift 群礼物
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E7%A4%BC%E7%89%A9
//
// Deprecated: 群礼物改版
func Gift(userID string, giftID string) MessageSegment {
return MessageSegment{
Type: "gift",
Data: map[string]string{
"qq": userID,
"id": giftID,
},
}
}
// Poke 戳一戳
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E6%88%B3%E4%B8%80%E6%88%B3
func Poke(userID int64) MessageSegment {
return MessageSegment{
Type: "poke",
Data: map[string]string{
"qq": strconv.FormatInt(userID, 10),
},
}
}
// TTS 文本转语音
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E6%96%87%E6%9C%AC%E8%BD%AC%E8%AF%AD%E9%9F%B3
func TTS(text string) MessageSegment {
return MessageSegment{
Type: "tts",
Data: map[string]string{
"text": text,
},
}
}
// Add 为 MessageSegment 的 Data 增加一个字段
func (m MessageSegment) Add(key string, val interface{}) MessageSegment {
m.Data[key] = fmt.Sprint(val)
return m
}
func (m MessageSegment) Delete(key string) MessageSegment {
delete(m.Data, key)
return m
}
// ReplyWithMessage returns a reply message
func ReplyWithMessage(messageID int64, m ...MessageSegment) Message {
return append(Message{Reply(messageID)}, m...)
}