Skip to main content

botcat_capoo/api/
send_message.rs

1use serde_json::json;
2use tracing::{error, info};
3
4use crate::{api::napcat::napcat_request, config::AppConfig, model::message::Message};
5
6pub async fn send_group_message(config: &AppConfig, group_id: u64, message: Message) {
7    let body = json!({
8        "group_id": group_id,
9        "message": message,
10    });
11
12    let res = napcat_request(config, "/send_group_msg", &body).await;
13    match res {
14        Ok(response) => {
15            info!("Group message sent successfully: {response:?}");
16        }
17        Err(err) => {
18            error!("Error sending group message: {err:?}");
19        }
20    }
21}
22
23pub async fn send_private_message(config: &AppConfig, to: u64, message: Message) {
24    let body = json!({
25        "user_id": to,
26        "message": message,
27    });
28
29    let res = napcat_request(config, "/send_private_msg", &body).await;
30    match res {
31        Ok(response) => {
32            info!("Private message sent successfully: {response:?}");
33        }
34        Err(err) => {
35            error!("Error sending private message: {err:?}");
36        }
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    use crate::model::message::segment::MessageSegment;
45
46    #[tokio::test]
47    #[ignore = "Requires an API server in internal network"]
48    async fn test_send_group_message() {
49        let config = AppConfig::init().expect("Failed to load config");
50        println!("Loaded config");
51        let message = vec![
52            MessageSegment::At {
53                qq: "46595749".to_string(),
54            },
55            MessageSegment::Text {
56                text: " Ich habe keine Ahnung.".to_string(),
57            },
58        ];
59        send_group_message(&config, 738943282, message).await;
60    }
61
62    #[tokio::test]
63    #[ignore = "Requires an API server in internal network"]
64    async fn test_send_private_message() {
65        let config = AppConfig::init().expect("Failed to load config");
66        println!("Loaded config");
67        let message = vec![MessageSegment::Text {
68            text: "Hello, user!".to_string(),
69        }];
70        send_private_message(&config, 46595749, message).await;
71    }
72}