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
use serde::{Deserialize, Serialize};
/// Onebot 协议消息定义
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(tag = "type", content = "data")]
pub enum Message {
/// 纯文本
#[serde(rename = "text")]
Text {
/// 纯文本内容
text: String,
},
/// QQ 表情
#[serde(rename = "face")]
Face {
/// QQ 表情 ID
id: String,
},
/// 图片
#[serde(rename = "image")]
Image {
/// 图片文件名
file: String,
/// 图片类型 flash 闪照
#[serde(rename = "type")]
type_: Option<String>,
/// 图片 URL
url: Option<String>,
/// 是否使用缓存文件 1|0
cache: Option<u8>,
/// 是否使用代理 1|0
proxy: Option<u8>,
/// 网络文件下载超时 单位秒
timeout: Option<i64>,
},
/// 语音
#[serde(rename = "record")]
Record {
/// 语音文件名
file: String,
/// 是否变声 1|0
magic: Option<u8>,
/// 语音 URL
url: Option<String>,
/// 是否使用缓存文件 1|0
cache: Option<u8>,
/// 是否使用代理 1|0
proxy: Option<u8>,
/// 网络文件下载超时 单位秒
timeout: Option<i64>,
},
/// 短视频
#[serde(rename = "video")]
Video {
/// 视频文件名
file: String,
/// 视频 URL
url: Option<String>,
/// 是否使用缓存文件 1|0
cache: Option<u8>,
/// 是否使用代理 1|0
proxy: Option<u8>,
/// 网络文件下载超时 单位秒
timeout: Option<i64>,
},
/// @某人
#[serde(rename = "at")]
At {
/// @QQ ID all 表示全体
qq: String,
},
/// 猜拳魔法表情
#[serde(rename = "rps")]
Rps,
/// 掷骰子魔法表情
#[serde(rename = "dice")]
Dice,
/// 窗口抖动(戳一戳)
#[serde(rename = "shake")]
Shake,
/// 戳一戳
#[serde(rename = "poke")]
Poke {
/// 类型
#[serde(rename = "type")]
type_: String,
/// ID
id: String,
/// 表情名
name: Option<String>,
},
/// 匿名发消息
#[serde(rename = "anonymous")]
Anonymous,
/// 链接分享
#[serde(rename = "share")]
Share {
/// URL
url: String,
/// 标题
title: String,
/// 内容描述
content: Option<String>,
/// 图片 URl
image: Option<String>,
},
/// 推荐好友|群
#[serde(rename = "contact")]
Contact {
/// 类型 qq|group
#[serde(rename = "type")]
type_: String,
/// QQ号|群号
id: String,
},
/// 位置
#[serde(rename = "location")]
Lacation {
/// 纬度
lat: String,
/// 经度
lon: String,
/// 标题
title: Option<String>,
/// 内容描述
content: Option<String>,
},
/// 音乐分享
#[serde(rename = "music")]
Music {
/// 类型 qq|163|xm|custom
#[serde(rename = "type")]
type_: String,
/// 歌曲 ID
id: Option<String>,
/// 点击后跳转 URL
url: Option<String>,
/// 歌曲 URL
audio: Option<String>,
/// 标题
title: Option<String>,
/// 内容描述
content: Option<String>,
/// 图片 URl
image: Option<String>,
},
/// 回复
#[serde(rename = "reply")]
Reply {
/// 回复的消息 ID
id: String,
},
/// 合并转发
#[serde(rename = "forward")]
Forward {
/// 合并转发 ID
id: String,
},
/// 合并转发节点
#[serde(rename = "node")]
Node {
/// 转发的消息 ID
id: Option<String>,
/// 发送者 QQ 号
user_id: Option<String>,
/// 发送者昵称
nickname: Option<String>,
/// 消息内容
content: Option<Vec<Message>>,
},
/// XML 消息
#[serde(rename = "xml")]
Xml {
/// 合并转发 ID
data: String,
},
/// JSON 消息
#[serde(rename = "json")]
Json {
/// 合并转发 ID
data: String,
},
}
macro_rules! message_builder {
($fn_name: ident, $message_type: tt) => {
pub fn $fn_name() -> Message {
Message::$message_type
}
};
($fn_name: ident, $message_type: tt, $param: ident: $param_ty: ty) => {
pub fn $fn_name($param: $param_ty) -> Message {
Message::$message_type { $param: $param }
}
};
($fn_name: ident, $message_type: tt, $($param: ident: $param_ty: ty),*) => {
pub fn $fn_name($($param: $param_ty,)*) -> Message {
Message::$message_type { $($param: $param,)* }
}
};
}
impl Message {
// pub fn text(text: &str) -> Message {
// Message::Text {
// text: text.to_string(),
// }
// }
message_builder!(text, Text, text: String);
message_builder!(face, Face, id: String);
message_builder!(
image,
Image,
file: String,
type_: Option<String>,
url: Option<String>,
cache: Option<u8>,
proxy: Option<u8>,
timeout: Option<i64>
);
message_builder!(
record,
Record,
file: String,
magic: Option<u8>,
url: Option<String>,
cache: Option<u8>,
proxy: Option<u8>,
timeout: Option<i64>
);
message_builder!(
video,
Video,
file: String,
url: Option<String>,
cache: Option<u8>,
proxy: Option<u8>,
timeout: Option<i64>
);
message_builder!(at, At, qq: String);
message_builder!(rps, Rps);
message_builder!(dice, Dice);
message_builder!(shake, Shake);
message_builder!(poke, Poke, type_: String, id: String, name: Option<String>);
message_builder!(anonymous, Anonymous);
message_builder!(
share,
Share,
url: String,
title: String,
content: Option<String>,
image: Option<String>
);
message_builder!(contact, Contact, type_: String, id: String);
message_builder!(
location,
Lacation,
lat: String,
lon: String,
title: Option<String>,
content: Option<String>
);
message_builder!(
music,
Music,
type_: String,
id: Option<String>,
url: Option<String>,
audio: Option<String>,
title: Option<String>,
content: Option<String>,
image: Option<String>
);
message_builder!(reply, Reply, id: String);
message_builder!(forward, Forward, id: String);
message_builder!(
node,
Node,
id: Option<String>,
user_id: Option<String>,
nickname: Option<String>,
content: Option<Vec<Message>>
);
message_builder!(xml, Xml, data: String);
message_builder!(json, Json, data: String);
}