qq_bot/restful/
send_message.rs

1use multipart::Form;
2use reqwest::{multipart, multipart::Part};
3use serde_json::Value;
4
5use super::*;
6
7/// `POST /channels/{channel_id}/messages`
8///
9/// <https://bot.q.qq.com/wiki/develop/api/openapi/message/get_message_of_id.html>
10#[derive(Debug)]
11pub struct SendMessageRequest {
12    pub content: String,
13    pub msg_id: String,
14    pub user_id: u64,
15    pub image_bytes: Vec<u8>,
16}
17#[derive(Serialize, Deserialize)]
18struct MessageReferenceR {
19    message_reference: MessageReference,
20}
21
22#[derive(Serialize, Deserialize)]
23pub struct MessageReference {
24    /// 需要引用回复的消息 id
25    message_id: String,
26    /// 是否忽略获取引用消息详情错误
27    ignore_get_message_error: bool,
28}
29
30impl SendMessageRequest {
31    pub fn end_point(channel_id: u64) -> String {
32        if cfg!(debug_assertions) {
33            format!("https://sandbox.api.sgroup.qq.com/channels/{channel_id}/messages",)
34        }
35        else {
36            format!("https://api.sgroup.qq.com/channels/{channel_id}/messages",)
37        }
38    }
39    pub async fn send_error(&self, bot: &impl QQBotProtocol, channel_id: u64) {
40        todo!()
41    }
42    pub async fn send(&self, bot: &impl QQBotProtocol, channel_id: u64) -> QQResult {
43        let url = Url::from_str(&Self::end_point(channel_id))?;
44        let image_part = Part::bytes(self.image_bytes.clone()).file_name("file_image");
45        // 必须用户 @机器人才能引用
46        let _ = MessageReferenceR {
47            message_reference: MessageReference { message_id: self.msg_id.to_string(), ignore_get_message_error: true },
48        };
49        let content = format!("<@!{}> {}", self.user_id, self.content);
50        let form = Form::new().text("content", content).text("msg_id", self.msg_id.to_string()).part("file_image", image_part);
51        let response = bot.build_request(Method::POST, url).multipart(form).send().await?;
52        if response.status().as_u16() > 300 {
53            println!("{:#?}", response.json::<Value>().await?)
54        }
55        // let value: Value = response.json().await?;
56        Ok(())
57    }
58}
59
60pub struct SendArkRequest {}
61// SendMessageItem {
62//     content: "<@!1234>hello world".to_string(),
63//     ark: Ark {
64//         ark: ArkBody {
65//             template_id: 37,
66//             kv: vec![
67//                 Kv { key: "#PROMPT#".to_string(), value: "通知提醒".to_string() },
68//                 Kv { key: "#METATITLE#".to_string(), value: "标题".to_string() },
69//                 Kv { key: "#METASUBTITLE#".to_string(), value: "子标题".to_string() },
70//                 Kv {
71//                     key: "#METACOVER#".to_string(),
72//                     value: "https://vfiles.gtimg.cn/vupload/20211029/bf0ed01635493790634.jpg".to_string(),
73//                 },
74//                 Kv {
75//                     key: "#METAURL#".to_string(),
76//                     value: "https://vfiles.gtimg.cn/vupload/20211029/bf0ed01635493790634.jpg".to_string(),
77//                 },
78//             ],
79//         },
80//     },
81// };
82#[derive(Serialize, Deserialize)]
83pub struct Ark {
84    pub ark: ArkBody,
85}
86
87#[derive(Serialize, Deserialize)]
88pub struct ArkBody {
89    pub template_id: i64,
90    pub kv: Vec<Kv>,
91}
92
93#[derive(Serialize, Deserialize)]
94pub struct Kv {
95    pub key: String,
96    pub value: String,
97}