Skip to main content

mirai/message/
single.rs

1//! Single Message is the element of MessageChain, when you want to send a message, you need to construct them.
2//!
3//! # SingleMessage
4//!
5//! [`SingleMessage`] is the element of [`MessageChain`], it has many variants:
6//!
7//! * Source: It contains a message-id and timestamp, but in common you don't need to use it, it only returns from the server.
8//! * Plain: It contains plain text, [`Plain`] message is common, and most frequently uses.
9//! * Quote: It is similar to [`Source`] variant, only returns from the server. It means this message quoted another message.
10//! * At: You can use [`At`] variant when you want this message notice somebody, the [`display`] property is how this [`At`] message displays.
11//! * AtAll: This message can only be received from a group.
12//! * Face: A face (aka expression) message element, if you want to construct it, you need provide at least one of [face_id] or [name].
13//! * Image | FlashImage: [`Image`] and [`FlashImage`] are similar, they both send an image message, but [`FlashImage`] has a time limitation.
14//!                       Both of them have three property: [`image_id`], [`url`] and [`path`],
15//!                       [`image_id`] is the id of an image which saved in Tencent server,
16//!                       [`url`] is a url that points to an image,
17//!                       [`path`] is a path that points to an image in the server.
18//!                       They also have priority, [`image_id`] > [`url`] > [`path`].
19//! * Xml | Json | App | Poke: These message are not very commonly used, you can see [this](https://github.com/mamoe/mirai-api-http/blob/master/MessageType.md) for more information.
20
21use serde::{Serialize, Deserialize};
22
23use crate::message::{MessageID, TimeStamp, MessageChain};
24use crate::Target;
25
26#[serde(tag = "type")]
27#[derive(Debug, Clone, Deserialize, Serialize)]
28pub enum SingleMessage {
29    Source {
30        id: MessageID,
31        time: TimeStamp,
32    },
33    Plain {
34        text: String
35    },
36    #[serde(rename_all = "camelCase")]
37    Quote {
38        id: MessageID,
39        group_id: Target,
40        sender_id: Target,
41        target_id: Target,
42        origin: MessageChain,
43    },
44    At {
45        target: Target,
46        display: String,
47    },
48    AtAll,
49    #[serde(rename_all = "camelCase")]
50    Face {
51        face_id: Option<i32>,
52        name: Option<String>,
53    },
54    #[serde(rename_all = "camelCase")]
55    Image {
56        image_id: Option<String>,
57        url: Option<String>,
58        path: Option<String>,
59    },
60    #[serde(rename_all = "camelCase")]
61    FlashImage {
62        image_id: Option<String>,
63        url: Option<String>,
64        path: Option<String>,
65    },
66    Xml {
67        xml: String
68    },
69    Json {
70        json: String
71    },
72    App {
73        content: String
74    },
75    Poke {
76        name: String
77    },
78
79    #[serde(other)]
80    Unsupported,
81}
82
83impl From<String> for SingleMessage {
84    fn from(str: String) -> Self {
85        SingleMessage::Plain {
86            text: str
87        }
88    }
89}
90
91impl From<&str> for SingleMessage {
92    fn from(str: &str) -> Self {
93        SingleMessage::from(str.to_string())
94    }
95}
96
97impl ToString for SingleMessage {
98    fn to_string(&self) -> String {
99        match self {
100            SingleMessage::Source { id, time: _ } => format!("[mirai:source:{}]", id),
101            SingleMessage::Plain { text } => text.clone(),
102            SingleMessage::Quote { id, group_id: _, sender_id: _, target_id: _, origin: _ } => format!("[mirai:quote:{}]", id),
103            SingleMessage::At { target, display: _ } => format!("[mirai:at:{}]", target),
104            SingleMessage::Image { .. } => "[Image]".to_string(),
105            SingleMessage::FlashImage { .. } => "[FlashImage]".to_string(),
106            SingleMessage::Xml { xml } => format!("[mirai:xml:{}]", xml),
107            SingleMessage::Json { json } => format!("[mirai:json:{}]", json),
108            SingleMessage::App { content } => format!("[mirai:app:{}]", content),
109            SingleMessage::Poke { name } => format!("[mirai:poke:{}]", name),
110            SingleMessage::Unsupported => format!("{:?}", SingleMessage::Unsupported),
111            SingleMessage::AtAll => "[mirai:atall]".to_string(),
112            SingleMessage::Face { face_id, name } => {
113                let s = if let Some(id) = face_id {
114                    id.to_string()
115                } else if let Some(name) = name {
116                    name.clone()
117                } else {
118                    panic!("id == None && name == None");
119                };
120
121                format!("[mirai:face:{}]", s)
122            }
123        }
124    }
125}