botrs 0.2.8

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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# API 客户端使用指南

BotRS 提供了完整的 QQ 频道 REST API 客户端,允许您直接与 QQ 频道的各种端点进行交互。本指南将详细介绍如何使用 `BotApi` 客户端来执行各种操作。

## 概述

`BotApi` 是一个完整的 HTTP 客户端,封装了所有 QQ 频道机器人 API 端点。它提供了类型安全的接口,自动处理身份验证、序列化和错误处理。

```rust
use botrs::{BotApi, Token};

let api = BotApi::new();
let token = Token::new("应用ID", "密钥");
```

## 基础设置

### 创建 API 客户端

```rust
use botrs::{BotApi, Token};

// 创建 API 客户端
let api = BotApi::new();

// 创建身份验证令牌
let token = Token::new("你的应用ID", "你的密钥");

// 验证令牌
token.validate()?;
```

### 自定义配置

```rust
use botrs::{BotApi, HttpClient};
use std::time::Duration;

// 创建自定义 HTTP 客户端
let http_client = HttpClient::builder()
    .timeout(Duration::from_secs(30))
    .user_agent("MyBot/1.0")
    .build()?;

let api = BotApi::with_http_client(http_client);
```

## 消息 API

### 发送文本消息

```rust
use botrs::{MessageParams, BotApi, Token};

async fn send_text_message(
    api: &BotApi,
    token: &Token,
    channel_id: &str,
    content: &str
) -> Result<Message, BotError> {
    let params = MessageParams::new_text(content);
    api.post_message_with_params(token, channel_id, params).await
}

// 使用示例
let message = send_text_message(
    &api,
    &token,
    "channel_id_123",
    "你好,这是一条测试消息!"
).await?;

println!("消息发送成功,ID: {}", message.id);
```

### 发送富文本消息

```rust
use botrs::{Embed, MessageParams};

async fn send_embed_message(
    api: &BotApi,
    token: &Token,
    channel_id: &str
) -> Result<Message, BotError> {
    let embed = Embed::new()
        .title("机器人状态")
        .description("当前机器人运行正常")
        .color(0x00ff00)
        .field("服务器", "在线", true)
        .field("延迟", "25ms", true)
        .timestamp(chrono::Utc::now());

    let params = MessageParams::new_embed(embed);
    api.post_message_with_params(token, channel_id, params).await
}
```

### 发送回复消息

```rust
async fn send_reply(
    api: &BotApi,
    token: &Token,
    channel_id: &str,
    original_message_id: &str,
    reply_content: &str
) -> Result<Message, BotError> {
    let params = MessageParams::new_text(reply_content)
        .with_reply(original_message_id);
    
    api.post_message_with_params(token, channel_id, params).await
}
```

### 发送 Markdown 消息

```rust
async fn send_markdown_message(
    api: &BotApi,
    token: &Token,
    channel_id: &str
) -> Result<Message, BotError> {
    let markdown_content = r#"
# 欢迎使用机器人

这是一条 **Markdown** 格式的消息。

## 功能列表

- 发送消息
- 管理频道
- 处理事件

[点击访问官网](https://example.com)
"#;

    let params = MessageParams::new_markdown(markdown_content);
    api.post_message_with_params(token, channel_id, params).await
}
```

## 文件上传

### 上传图片

```rust
async fn upload_image(
    api: &BotApi,
    token: &Token,
    channel_id: &str,
    image_path: &str
) -> Result<Message, BotError> {
    // 读取图片文件
    let image_data = tokio::fs::read(image_path).await?;
    
    // 提取文件名
    let filename = std::path::Path::new(image_path)
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("image.png");
    
    // 发送带文件的消息
    api.post_message_with_file(
        token,
        channel_id,
        filename,
        &image_data,
        "png"
    ).await
}

// 使用示例
let message = upload_image(&api, &token, "channel_123", "assets/logo.png").await?;
```

### 上传多种文件类型

```rust
async fn upload_file_by_type(
    api: &BotApi,
    token: &Token,
    channel_id: &str,
    file_path: &str
) -> Result<Message, BotError> {
    let file_data = tokio::fs::read(file_path).await?;
    let filename = std::path::Path::new(file_path)
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("file");
    
    // 根据文件扩展名确定类型
    let file_type = match std::path::Path::new(file_path)
        .extension()
        .and_then(|s| s.to_str()) {
        Some("png") | Some("jpg") | Some("jpeg") | Some("gif") => "image",
        Some("mp4") | Some("avi") | Some("mov") => "video",
        Some("mp3") | Some("wav") | Some("ogg") => "audio",
        _ => "file",
    };
    
    api.post_message_with_file(token, channel_id, filename, &file_data, file_type).await
}
```

## 频道管理

### 获取频道信息

```rust
async fn get_guild_info(
    api: &BotApi,
    token: &Token,
    guild_id: &str
) -> Result<Guild, BotError> {
    api.get_guild(token, guild_id).await
}

// 使用示例
let guild = get_guild_info(&api, &token, "guild_123").await?;
println!("频道名称: {}", guild.name.unwrap_or_default());
println!("成员数量: {}", guild.member_count.unwrap_or_default());
```

### 获取子频道列表

```rust
async fn list_channels(
    api: &BotApi,
    token: &Token,
    guild_id: &str
) -> Result<Vec<Channel>, BotError> {
    api.get_guild_channels(token, guild_id).await
}

// 使用示例
let channels = list_channels(&api, &token, "guild_123").await?;
for channel in channels {
    println!("频道: {} ({})", 
             channel.name.unwrap_or_default(), 
             channel.id);
}
```

### 创建子频道

```rust
use botrs::{ChannelType, ChannelSubType};

async fn create_text_channel(
    api: &BotApi,
    token: &Token,
    guild_id: &str,
    channel_name: &str
) -> Result<Channel, BotError> {
    let channel_data = serde_json::json!({
        "name": channel_name,
        "type": ChannelType::Text as u8,
        "sub_type": ChannelSubType::Chat as u8,
        "position": 1
    });
    
    api.create_guild_channel(token, guild_id, &channel_data).await
}
```

### 修改子频道

```rust
async fn modify_channel(
    api: &BotApi,
    token: &Token,
    channel_id: &str,
    new_name: &str
) -> Result<Channel, BotError> {
    let update_data = serde_json::json!({
        "name": new_name
    });
    
    api.modify_guild_channel(token, channel_id, &update_data).await
}
```

## 成员管理

### 获取成员信息

```rust
async fn get_member_info(
    api: &BotApi,
    token: &Token,
    guild_id: &str,
    user_id: &str
) -> Result<Member, BotError> {
    api.get_guild_member(token, guild_id, user_id).await
}

// 使用示例
let member = get_member_info(&api, &token, "guild_123", "user_456").await?;
println!("成员昵称: {}", member.nick.unwrap_or_default());
```

### 获取成员列表

```rust
async fn list_guild_members(
    api: &BotApi,
    token: &Token,
    guild_id: &str,
    limit: Option<u32>
) -> Result<Vec<Member>, BotError> {
    api.get_guild_members(token, guild_id, None, limit).await
}

// 分页获取所有成员
async fn get_all_members(
    api: &BotApi,
    token: &Token,
    guild_id: &str
) -> Result<Vec<Member>, BotError> {
    let mut all_members = Vec::new();
    let mut after = None;
    let limit = 400; // QQ API 的最大限制
    
    loop {
        let members = api.get_guild_members(token, guild_id, after.as_deref(), Some(limit)).await?;
        
        if members.is_empty() {
            break;
        }
        
        after = members.last().map(|m| m.user.id.clone());
        all_members.extend(members);
        
        // 避免速率限制
        tokio::time::sleep(Duration::from_millis(100)).await;
    }
    
    Ok(all_members)
}
```

### 管理成员权限

```rust
async fn add_member_role(
    api: &BotApi,
    token: &Token,
    guild_id: &str,
    user_id: &str,
    role_id: &str
) -> Result<(), BotError> {
    api.add_guild_member_role(token, guild_id, user_id, role_id).await
}

async fn remove_member_role(
    api: &BotApi,
    token: &Token,
    guild_id: &str,
    user_id: &str,
    role_id: &str
) -> Result<(), BotError> {
    api.remove_guild_member_role(token, guild_id, user_id, role_id).await
}
```

## 私信 API

### 发送私信

```rust
async fn send_direct_message(
    api: &BotApi,
    token: &Token,
    guild_id: &str,
    user_id: &str,
    content: &str
) -> Result<DirectMessage, BotError> {
    // 首先创建私信会话
    let dm_session = api.create_direct_message_session(token, guild_id, user_id).await?;
    
    // 然后发送消息
    let params = MessageParams::new_text(content);
    api.post_direct_message_with_params(token, guild_id, &dm_session.channel_id, params).await
}
```

### 获取私信历史

```rust
async fn get_dm_history(
    api: &BotApi,
    token: &Token,
    guild_id: &str,
    channel_id: &str,
    limit: u32
) -> Result<Vec<DirectMessage>, BotError> {
    api.get_direct_messages(token, guild_id, channel_id, Some(limit)).await
}
```

## 群组 API

### 发送群组消息

```rust
async fn send_group_message(
    api: &BotApi,
    token: &Token,
    group_id: &str,
    content: &str
) -> Result<GroupMessage, BotError> {
    let params = MessageParams::new_text(content);
    api.post_group_message_with_params(token, group_id, params).await
}
```

### 发送 C2C 消息

```rust
async fn send_c2c_message(
    api: &BotApi,
    token: &Token,
    user_id: &str,
    content: &str
) -> Result<C2CMessage, BotError> {
    let params = MessageParams::new_text(content);
    api.post_c2c_message_with_params(token, user_id, params).await
}
```

## 公告 API

### 创建公告

```rust
use botrs::Announce;

async fn create_announcement(
    api: &BotApi,
    token: &Token,
    guild_id: &str,
    channel_id: &str,
    content: &str
) -> Result<Announce, BotError> {
    let announce_data = serde_json::json!({
        "message": content,
        "channel_id": channel_id
    });
    
    api.create_guild_announce(token, guild_id, &announce_data).await
}
```

### 删除公告

```rust
async fn delete_announcement(
    api: &BotApi,
    token: &Token,
    guild_id: &str,
    announce_id: &str
) -> Result<(), BotError> {
    api.delete_guild_announce(token, guild_id, announce_id).await
}
```

## 表情回应 API

### 添加表情回应

```rust
async fn add_reaction(
    api: &BotApi,
    token: &Token,
    channel_id: &str,
    message_id: &str,
    emoji: &str
) -> Result<(), BotError> {
    api.put_message_reaction(token, channel_id, message_id, emoji).await
}
```

### 删除表情回应

```rust
async fn remove_reaction(
    api: &BotApi,
    token: &Token,
    channel_id: &str,
    message_id: &str,
    emoji: &str
) -> Result<(), BotError> {
    api.delete_message_reaction(token, channel_id, message_id, emoji).await
}
```

### 获取表情回应用户列表

```rust
async fn get_reaction_users(
    api: &BotApi,
    token: &Token,
    channel_id: &str,
    message_id: &str,
    emoji: &str
) -> Result<Vec<User>, BotError> {
    let reaction_users = api.get_message_reaction_users(
        token, 
        channel_id, 
        message_id, 
        emoji,
        None, // cookie
        None  // limit
    ).await?;
    
    Ok(reaction_users.users)
}
```

## 错误处理

### 基础错误处理

```rust
use botrs::BotError;

async fn handle_api_errors(api: &BotApi, token: &Token) {
    let result = api.get_guild(token, "invalid_guild_id").await;
    
    match result {
        Ok(guild) => println!("获取频道成功: {}", guild.id),
        Err(BotError::NotFound) => eprintln!("频道不存在"),
        Err(BotError::Forbidden) => eprintln!("权限不足"),
        Err(BotError::RateLimited(retry_after)) => {
            eprintln!("速率限制,{}秒后重试", retry_after);
        }
        Err(BotError::Authentication(_)) => eprintln!("身份验证失败"),
        Err(BotError::Network(_)) => eprintln!("网络连接错误"),
        Err(e) => eprintln!("其他错误: {}", e),
    }
}
```

### 自动重试机制

```rust
use tokio::time::{sleep, Duration};

async fn api_call_with_retry<T, F, Fut>(
    operation: F,
    max_retries: usize,
) -> Result<T, BotError>
where
    F: Fn() -> Fut,
    Fut: std::future::Future<Output = Result<T, BotError>>,
{
    let mut last_error = None;
    
    for attempt in 0..=max_retries {
        match operation().await {
            Ok(result) => return Ok(result),
            Err(BotError::RateLimited(retry_after)) => {
                if attempt < max_retries {
                    sleep(Duration::from_secs(retry_after)).await;
                    continue;
                }
                last_error = Some(BotError::RateLimited(retry_after));
            }
            Err(BotError::Network(_)) => {
                if attempt < max_retries {
                    sleep(Duration::from_secs(2_u64.pow(attempt as u32))).await;
                    continue;
                }
                last_error = Some(BotError::Network("重试失败".to_string()));
            }
            Err(e) => return Err(e),
        }
    }
    
    Err(last_error.unwrap_or_else(|| BotError::Custom("重试次数用尽".to_string())))
}

// 使用示例
let result = api_call_with_retry(
    || api.get_guild(&token, "guild_123"),
    3
).await?;
```

## 批量操作

### 批量发送消息

```rust
async fn send_messages_to_multiple_channels(
    api: &BotApi,
    token: &Token,
    channel_ids: &[String],
    content: &str
) -> Result<Vec<Message>, BotError> {
    let mut results = Vec::new();
    
    for channel_id in channel_ids {
        let params = MessageParams::new_text(content);
        
        match api.post_message_with_params(token, channel_id, params).await {
            Ok(message) => {
                results.push(message);
                println!("消息发送到频道 {} 成功", channel_id);
            }
            Err(e) => {
                eprintln!("向频道 {} 发送消息失败: {}", channel_id, e);
            }
        }
        
        // 避免速率限制
        sleep(Duration::from_millis(500)).await;
    }
    
    Ok(results)
}
```

### 并发 API 调用

```rust
use futures::future::try_join_all;

async fn concurrent_guild_info(
    api: &BotApi,
    token: &Token,
    guild_ids: &[String]
) -> Result<Vec<Guild>, BotError> {
    let futures: Vec<_> = guild_ids.iter()
        .map(|guild_id| api.get_guild(token, guild_id))
        .collect();
    
    try_join_all(futures).await
}
```

## 数据缓存

### 简单缓存实现

```rust
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use std::time::{Duration, Instant};

struct CachedApiClient {
    api: BotApi,
    guild_cache: Arc<RwLock<HashMap<String, (Guild, Instant)>>>,
    cache_duration: Duration,
}

impl CachedApiClient {
    pub fn new(api: BotApi, cache_duration: Duration) -> Self {
        Self {
            api,
            guild_cache: Arc::new(RwLock::new(HashMap::new())),
            cache_duration,
        }
    }
    
    pub async fn get_guild_cached(
        &self,
        token: &Token,
        guild_id: &str
    ) -> Result<Guild, BotError> {
        // 检查缓存
        {
            let cache = self.guild_cache.read().await;
            if let Some((guild, cached_at)) = cache.get(guild_id) {
                if cached_at.elapsed() < self.cache_duration {
                    return Ok(guild.clone());
                }
            }
        }
        
        // 缓存未命中,从 API 获取
        let guild = self.api.get_guild(token, guild_id).await?;
        
        // 更新缓存
        {
            let mut cache = self.guild_cache.write().await;
            cache.insert(guild_id.to_string(), (guild.clone(), Instant::now()));
        }
        
        Ok(guild)
    }
}
```

## 最佳实践

### API 客户端封装

```rust
pub struct BotApiWrapper {
    api: BotApi,
    token: Token,
}

impl BotApiWrapper {
    pub fn new(token: Token) -> Self {
        Self {
            api: BotApi::new(),
            token,
        }
    }
    
    pub async fn send_message(&self, channel_id: &str, content: &str) -> Result<Message, BotError> {
        let params = MessageParams::new_text(content);
        self.api.post_message_with_params(&self.token, channel_id, params).await
    }
    
    pub async fn send_embed(&self, channel_id: &str, embed: Embed) -> Result<Message, BotError> {
        let params = MessageParams::new_embed(embed);
        self.api.post_message_with_params(&self.token, channel_id, params).await
    }
    
    pub async fn get_guild(&self, guild_id: &str) -> Result<Guild, BotError> {
        self.api.get_guild(&self.token, guild_id).await
    }
}
```

### 配置管理

```rust
#[derive(Clone)]
pub struct ApiConfig {
    pub timeout: Duration,
    pub retry_attempts: usize,
    pub rate_limit_delay: Duration,
}

impl Default for ApiConfig {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(30),
            retry_attempts: 3,
            rate_limit_delay: Duration::from_millis(500),
        }
    }
}
```

### 日志记录

```rust
use tracing::{info, warn, error};

async fn logged_api_call<T>(
    operation_name: &str,
    operation: impl std::future::Future<Output = Result<T, BotError>>
) -> Result<T, BotError> {
    info!("开始执行 API 操作: {}", operation_name);
    
    match operation.await {
        Ok(result) => {
            info!("API 操作成功: {}", operation_name);
            Ok(result)
        }
        Err(e) => {
            error!("API 操作失败: {} - {}", operation_name, e);
            Err(e)
        }
    }
}

// 使用示例
let guild = logged_api_call(
    "获取频道信息",
    api.get_guild(&token, "guild_123")
).await?;
```

API 客户端是 BotRS 的核心组件之一,提供了与 QQ 频道平台交互的所有必要功能。通过合理使用错误处理、重试机制和缓存策略,您可以构建出健壮且高性能的机器人应用程序。

## 另请参阅

- [消息处理指南]/zh/guide/messages.md - 详细的消息发送和处理
- [错误处理指南]/zh/guide/error-handling.md - API 错误处理策略
- [`BotApi` API 参考]/zh/api/bot-api.md - 完整的 API 参考文档
- [`Token` API 参考]/zh/api/token.md - 身份验证令牌管理