botrs 0.13.0

A Rust QQ Bot framework based on QQ Guild Bot API
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use super::{BotApi, resource};
use crate::error::Result;
use crate::models::announce::{Announce, AnnouncesType, RecommendChannel};
use crate::models::message::Media;
use serde::Serialize;
use serde_json::{Value, json};
use tracing::debug;

#[derive(Serialize)]
struct GroupFileBody<'a> {
    group_openid: &'a str,
    file_type: u32,
    url: &'a str,
    srv_send_msg: bool,
}

#[derive(Serialize)]
struct C2cFileBody<'a> {
    openid: &'a str,
    file_type: u32,
    url: &'a str,
    srv_send_msg: bool,
}

#[derive(Serialize)]
struct GuildMessageAnnounceBody<'a> {
    channel_id: &'a str,
    message_id: &'a str,
}

#[derive(Serialize)]
struct GuildRecommendAnnounceBody {
    announces_type: u32,
    recommend_channels: Vec<RecommendChannel>,
}

impl BotApi {
    /// Sends a file message to an open-platform group conversation.
    ///
    /// `file_type` follows the platform numeric values: image, video, audio,
    /// or generic file.
    pub async fn post_group_file(
        &self,
        group_openid: &str,
        file_type: u32,
        url: &str,
        srv_send_msg: Option<bool>,
    ) -> Result<Media> {
        debug!("Uploading group file to {}", group_openid);

        let body = GroupFileBody {
            group_openid,
            file_type,
            url,
            srv_send_msg: srv_send_msg.unwrap_or(false),
        };

        let path = resource::group_file(group_openid);
        let response = self
            .http
            .post(self.token(), &path, None::<&()>, Some(&body))
            .await?;
        Self::decode_json(response)
    }

    /// Sends a file message to a C2C conversation.
    ///
    /// `file_type` follows the platform numeric values: image, video, audio,
    /// or generic file.
    pub async fn post_c2c_file(
        &self,
        openid: &str,
        file_type: u32,
        url: &str,
        srv_send_msg: Option<bool>,
    ) -> Result<Media> {
        debug!("Uploading C2C file to {}", openid);

        let body = C2cFileBody {
            openid,
            file_type,
            url,
            srv_send_msg: srv_send_msg.unwrap_or(false),
        };

        let path = resource::c2c_file(openid);
        let response = self
            .http
            .post(self.token(), &path, None::<&()>, Some(&body))
            .await?;
        Self::decode_json(response)
    }

    // Announcement APIs

    /// Creates a channel announcement from an existing message.
    pub async fn create_channel_announce(
        &self,
        channel_id: &str,
        message_id: &str,
    ) -> Result<Announce> {
        debug!(
            "Creating channel announcement in channel {} for message {}",
            channel_id, message_id
        );

        let body = json!({
            "message_id": message_id
        });

        let path = resource::channel_announces(channel_id);
        let response = self
            .http
            .post(self.token(), &path, None::<&()>, Some(&body))
            .await?;
        Self::decode_json(response)
    }

    /// Deletes a channel announcement by message ID.
    pub async fn delete_channel_announce(&self, channel_id: &str, message_id: &str) -> Result<()> {
        debug!(
            "Deleting announcement {} in channel {}",
            message_id, channel_id
        );

        let path = resource::channel_announce(channel_id, message_id);
        self.http.delete(self.token(), &path, None::<&()>).await?;
        Ok(())
    }

    /// Clears all channel announcements without checking a message ID.
    pub async fn clean_channel_announces(&self, channel_id: &str) -> Result<()> {
        debug!("Clearing announcements in channel {}", channel_id);
        let path = resource::channel_announces_all(channel_id);
        self.http.delete(self.token(), &path, None::<&()>).await?;
        Ok(())
    }

    /// Creates a message-type guild announcement from an existing message.
    pub async fn create_announce(
        &self,
        guild_id: &str,
        channel_id: &str,
        message_id: &str,
    ) -> Result<Announce> {
        debug!(
            "Creating announcement in guild {} for message {}",
            guild_id, message_id
        );

        let body = GuildMessageAnnounceBody {
            channel_id,
            message_id,
        };

        let path = resource::guild_announces(guild_id);
        let response = self
            .http
            .post(self.token(), &path, None::<&()>, Some(&body))
            .await?;
        Self::decode_json(response)
    }

    /// Creates a message-type guild announcement.
    pub async fn create_guild_announce(
        &self,
        guild_id: &str,
        channel_id: &str,
        message_id: &str,
    ) -> Result<Announce> {
        self.create_announce(guild_id, channel_id, message_id).await
    }

    /// Creates a guild announcement that recommends channels.
    pub async fn create_recommend_announce(
        &self,
        guild_id: &str,
        announces_type: AnnouncesType,
        recommend_channels: Vec<RecommendChannel>,
    ) -> Result<Announce> {
        debug!("Creating recommend announcement in guild {}", guild_id);

        let body = GuildRecommendAnnounceBody {
            announces_type: u8::from(announces_type) as u32,
            recommend_channels,
        };

        let path = resource::guild_announces(guild_id);
        let response = self
            .http
            .post(self.token(), &path, None::<&()>, Some(&body))
            .await?;
        Self::decode_json(response)
    }

    /// Creates a recommended channel guild announcement.
    pub async fn create_guild_recommend_announce(
        &self,
        guild_id: &str,
        announces_type: AnnouncesType,
        recommend_channels: Vec<RecommendChannel>,
    ) -> Result<Announce> {
        self.create_recommend_announce(guild_id, announces_type, recommend_channels)
            .await
    }

    /// Deletes a guild announcement by message ID and returns the raw response.
    ///
    /// Passing `None::<&str>` deletes all guild announcements.
    pub async fn delete_announce<'a>(
        &self,
        guild_id: &str,
        message_id: impl Into<Option<&'a str>>,
    ) -> Result<Value> {
        let message_id = message_id.into().unwrap_or("all");
        debug!("Deleting announcement {} in guild {}", message_id, guild_id);

        let path = resource::guild_announce(guild_id, message_id);
        let response = self.http.delete(self.token(), &path, None::<&()>).await?;
        Ok(response)
    }

    /// Deletes a guild announcement.
    pub async fn delete_guild_announce(&self, guild_id: &str, message_id: &str) -> Result<()> {
        self.delete_announce(guild_id, message_id).await?;
        Ok(())
    }

    /// Clears all guild announcements without checking a message ID.
    pub async fn clean_guild_announces(&self, guild_id: &str) -> Result<()> {
        debug!("Clearing announcements in guild {}", guild_id);
        let path = resource::guild_announces_all(guild_id);
        self.http.delete(self.token(), &path, None::<&()>).await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::{C2cFileBody, GroupFileBody, GuildMessageAnnounceBody, GuildRecommendAnnounceBody};
    use crate::models::announce::{AnnouncesType, RecommendChannel};
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;
    use tokio::sync::oneshot;

    async fn test_api(base_url: String) -> crate::api_impl::BotApi {
        let token = crate::Token::new("APPID_XXXXXX", "SECRET_XXXXXX");
        token
            .set_cached_access_token_for_test("ACCESS_TOKEN_XXXXXX")
            .await;
        let mut http = crate::http::HttpClient::new(30, false).unwrap();
        http.base_url = base_url;
        crate::api_impl::BotApi::new(http, token)
    }

    async fn spawn_capture_server() -> (
        String,
        oneshot::Receiver<String>,
        tokio::task::JoinHandle<()>,
    ) {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let (tx, rx) = oneshot::channel();

        let handle = tokio::spawn(async move {
            let (mut stream, _) = listener.accept().await.unwrap();
            let mut request_bytes = Vec::new();
            let mut buffer = [0_u8; 4096];
            loop {
                let n = stream.read(&mut buffer).await.unwrap();
                request_bytes.extend_from_slice(&buffer[..n]);

                let request = String::from_utf8_lossy(&request_bytes);
                let Some(header_end) = request.find("\r\n\r\n") else {
                    continue;
                };
                let content_length = request
                    .lines()
                    .find_map(|line| {
                        let (name, value) = line.split_once(':')?;
                        name.eq_ignore_ascii_case("content-length")
                            .then(|| value.trim().parse::<usize>().ok())
                            .flatten()
                    })
                    .unwrap_or(0);
                let body_start = header_end + 4;
                if request_bytes.len().saturating_sub(body_start) >= content_length {
                    break;
                }
            }

            let request = String::from_utf8_lossy(&request_bytes).to_string();
            let _ = tx.send(request);

            let body = r#"{"file_uuid":"file-1","file_info":"info-1","ttl":60}"#;
            let response = format!(
                "HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{}",
                body.len(),
                body
            );
            stream.write_all(response.as_bytes()).await.unwrap();
        });

        (format!("http://{addr}"), rx, handle)
    }

    fn request_body(request: &str) -> serde_json::Value {
        let body = request.split("\r\n\r\n").nth(1).unwrap_or_default();
        serde_json::from_str(body).unwrap()
    }

    #[test]
    fn open_file_bodies_use_platform_shape() {
        let group = serde_json::to_value(GroupFileBody {
            group_openid: "group-openid-1",
            file_type: 1,
            url: "https://example.com/a.png",
            srv_send_msg: false,
        })
        .unwrap();
        assert_eq!(
            group,
            serde_json::json!({
                "group_openid": "group-openid-1",
                "file_type": 1,
                "url": "https://example.com/a.png",
                "srv_send_msg": false
            })
        );

        let c2c = serde_json::to_value(C2cFileBody {
            openid: "openid-1",
            file_type: 1,
            url: "https://example.com/a.png",
            srv_send_msg: false,
        })
        .unwrap();
        assert_eq!(
            c2c,
            serde_json::json!({
                "openid": "openid-1",
                "file_type": 1,
                "url": "https://example.com/a.png",
                "srv_send_msg": false
            })
        );
    }

    #[tokio::test]
    async fn post_group_file_uses_platform_request() {
        let (base_url, request, server) = spawn_capture_server().await;
        let api = test_api(base_url).await;
        let response = api
            .post_group_file("group-openid-1", 1, "https://example.com/a.png", None)
            .await
            .unwrap();

        assert_eq!(response.file_uuid.as_deref(), Some("file-1"));
        assert_eq!(response.file_info.as_deref(), Some("info-1"));
        assert_eq!(response.ttl, Some(60));
        let request = request.await.unwrap();
        assert!(request.starts_with("POST /v2/groups/group-openid-1/files HTTP/1.1"));
        assert_eq!(
            request_body(&request),
            serde_json::json!({
                "group_openid": "group-openid-1",
                "file_type": 1,
                "url": "https://example.com/a.png",
                "srv_send_msg": false
            })
        );
        server.await.unwrap();
    }

    #[tokio::test]
    async fn post_c2c_file_uses_platform_request() {
        let (base_url, request, server) = spawn_capture_server().await;
        let api = test_api(base_url).await;
        let response = api
            .post_c2c_file("openid-1", 1, "https://example.com/a.png", None)
            .await
            .unwrap();

        assert_eq!(response.file_uuid.as_deref(), Some("file-1"));
        assert_eq!(response.file_info.as_deref(), Some("info-1"));
        assert_eq!(response.ttl, Some(60));
        let request = request.await.unwrap();
        assert!(request.starts_with("POST /v2/users/openid-1/files HTTP/1.1"));
        assert_eq!(
            request_body(&request),
            serde_json::json!({
                "openid": "openid-1",
                "file_type": 1,
                "url": "https://example.com/a.png",
                "srv_send_msg": false
            })
        );
        server.await.unwrap();
    }

    #[tokio::test]
    async fn delete_announce_defaults_to_all() {
        let (base_url, request, server) = spawn_capture_server().await;
        let api = test_api(base_url).await;

        let response = api.delete_announce("guild-1", None::<&str>).await.unwrap();

        assert_eq!(response["file_uuid"], "file-1");
        let request = request.await.unwrap();
        assert!(request.starts_with("DELETE /guilds/guild-1/announces/all HTTP/1.1"));
        server.await.unwrap();
    }

    #[test]
    fn high_level_guild_announce_body_uses_platform_shape() {
        let message = serde_json::to_value(GuildMessageAnnounceBody {
            channel_id: "channel-1",
            message_id: "message-1",
        })
        .unwrap();
        assert_eq!(
            message,
            serde_json::json!({
                "channel_id": "channel-1",
                "message_id": "message-1"
            })
        );

        let recommend = serde_json::to_value(GuildRecommendAnnounceBody {
            announces_type: u8::from(AnnouncesType::Welcome) as u32,
            recommend_channels: vec![RecommendChannel {
                channel_id: "channel-2".to_string(),
                introduce: "intro".to_string(),
            }],
        })
        .unwrap();
        assert_eq!(recommend["announces_type"], 1);
        assert_eq!(
            recommend["recommend_channels"][0]["channel_id"],
            "channel-2"
        );
        assert!(recommend.get("channel_id").is_none());
        assert!(recommend.get("message_id").is_none());
    }
}