Skip to main content

telegram_rich/types/
message.rs

1//! [`InputRichMessage`] and [`InputRichMessageMedia`].
2
3use serde::{Deserialize, Serialize};
4
5use super::blocks::InputRichBlock;
6use super::media::InputMedia;
7
8/// Describes a rich message to be sent.
9///
10/// Exactly one of `html`, `markdown` or `blocks` must be set; use the
11/// constructors to guarantee this.
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct InputRichMessage {
14    /// Optional. Content described as a list of blocks.
15    #[serde(default, skip_serializing_if = "Option::is_none")]
16    pub blocks: Option<Vec<InputRichBlock>>,
17    /// Optional. Content described using Rich HTML formatting.
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    pub html: Option<String>,
20    /// Optional. Content described using Rich Markdown formatting.
21    #[serde(default, skip_serializing_if = "Option::is_none")]
22    pub markdown: Option<String>,
23    /// Optional. Media referenced from `html`/`markdown` via `tg://photo?id=`,
24    /// `tg://video?id=`, `tg://document?id=` or `tg://audio?id=` links.
25    #[serde(default, skip_serializing_if = "Vec::is_empty")]
26    pub media: Vec<InputRichMessageMedia>,
27    /// Optional. Show the message right-to-left.
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub is_rtl: Option<bool>,
30    /// Optional. Skip automatic detection of URLs, mentions, hashtags, etc.
31    #[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    /// Creates a message from Rich HTML.
48    pub fn html(html: impl Into<String>) -> Self {
49        Self {
50            html: Some(html.into()),
51            ..Self::empty()
52        }
53    }
54
55    /// Creates a message from Rich Markdown.
56    pub fn markdown(markdown: impl Into<String>) -> Self {
57        Self {
58            markdown: Some(markdown.into()),
59            ..Self::empty()
60        }
61    }
62
63    /// Creates a message from explicit blocks.
64    pub fn blocks(blocks: Vec<InputRichBlock>) -> Self {
65        Self {
66            blocks: Some(blocks),
67            ..Self::empty()
68        }
69    }
70
71    /// Adds an explicitly specified media element (HTML/Markdown modes only).
72    pub fn with_media(mut self, media: InputRichMessageMedia) -> Self {
73        self.media.push(media);
74        self
75    }
76
77    /// Sets `skip_entity_detection`.
78    pub fn with_skip_entity_detection(mut self, skip: bool) -> Self {
79        self.skip_entity_detection = Some(skip);
80        self
81    }
82
83    /// Sets `is_rtl`.
84    pub fn with_rtl(mut self, rtl: bool) -> Self {
85        self.is_rtl = Some(rtl);
86        self
87    }
88}
89
90/// A media element embedded in an outgoing rich message.
91#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
92pub struct InputRichMessageMedia {
93    /// Identifier used in `tg://…?id=` links; 1-64 characters of `A-Za-z0-9_-`.
94    pub id: String,
95    /// The media to be sent; only the media itself and its properties are used.
96    pub media: InputMedia,
97}
98
99impl InputRichMessageMedia {
100    /// Creates a media element.
101    pub fn new(id: impl Into<String>, media: InputMedia) -> Self {
102        Self {
103            id: id.into(),
104            media,
105        }
106    }
107
108    /// Returns `true` if `id` satisfies the Bot API constraints.
109    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}