telegram_rich/types/
message.rs1use serde::{Deserialize, Serialize};
4
5use super::blocks::InputRichBlock;
6use super::media::InputMedia;
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct InputRichMessage {
14 #[serde(default, skip_serializing_if = "Option::is_none")]
16 pub blocks: Option<Vec<InputRichBlock>>,
17 #[serde(default, skip_serializing_if = "Option::is_none")]
19 pub html: Option<String>,
20 #[serde(default, skip_serializing_if = "Option::is_none")]
22 pub markdown: Option<String>,
23 #[serde(default, skip_serializing_if = "Vec::is_empty")]
26 pub media: Vec<InputRichMessageMedia>,
27 #[serde(default, skip_serializing_if = "Option::is_none")]
29 pub is_rtl: Option<bool>,
30 #[serde(default, skip_serializing_if = "Option::is_none")]
32 pub skip_entity_detection: Option<bool>,
33}
34
35impl InputRichMessage {
36 fn empty() -> Self {
37 Self {
38 blocks: None,
39 html: None,
40 markdown: None,
41 media: Vec::new(),
42 is_rtl: None,
43 skip_entity_detection: None,
44 }
45 }
46
47 pub fn html(html: impl Into<String>) -> Self {
49 Self {
50 html: Some(html.into()),
51 ..Self::empty()
52 }
53 }
54
55 pub fn markdown(markdown: impl Into<String>) -> Self {
57 Self {
58 markdown: Some(markdown.into()),
59 ..Self::empty()
60 }
61 }
62
63 pub fn blocks(blocks: Vec<InputRichBlock>) -> Self {
65 Self {
66 blocks: Some(blocks),
67 ..Self::empty()
68 }
69 }
70
71 pub fn with_media(mut self, media: InputRichMessageMedia) -> Self {
73 self.media.push(media);
74 self
75 }
76
77 pub fn with_skip_entity_detection(mut self, skip: bool) -> Self {
79 self.skip_entity_detection = Some(skip);
80 self
81 }
82
83 pub fn with_rtl(mut self, rtl: bool) -> Self {
85 self.is_rtl = Some(rtl);
86 self
87 }
88}
89
90#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
92pub struct InputRichMessageMedia {
93 pub id: String,
95 pub media: InputMedia,
97}
98
99impl InputRichMessageMedia {
100 pub fn new(id: impl Into<String>, media: InputMedia) -> Self {
102 Self {
103 id: id.into(),
104 media,
105 }
106 }
107
108 pub fn is_valid_id(id: &str) -> bool {
110 (1..=64).contains(&id.len())
111 && id
112 .bytes()
113 .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-')
114 }
115}