Skip to main content

telegram_rich/types/
blocks.rs

1//! Outgoing rich message blocks ([`InputRichBlock`]).
2
3use serde::{Deserialize, Serialize};
4
5use super::button::RichMessageButton;
6use super::common::Location;
7use super::media::{
8    InputMediaAnimation, InputMediaAudio, InputMediaDocument, InputMediaPhoto, InputMediaVideo,
9    InputMediaVoiceNote,
10};
11use super::rich_text::RichText;
12
13/// Horizontal alignment.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "snake_case")]
16pub enum HorizontalAlign {
17    /// Left.
18    Left,
19    /// Center.
20    Center,
21    /// Right.
22    Right,
23}
24
25/// Vertical alignment.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
27#[serde(rename_all = "snake_case")]
28pub enum VerticalAlign {
29    /// Top.
30    Top,
31    /// Middle.
32    Middle,
33    /// Bottom.
34    Bottom,
35}
36
37/// Label type of an ordered list item.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
39pub enum ListLabelType {
40    /// Lowercase letters.
41    #[serde(rename = "a")]
42    LowerAlpha,
43    /// Uppercase letters.
44    #[serde(rename = "A")]
45    UpperAlpha,
46    /// Lowercase Roman numerals.
47    #[serde(rename = "i")]
48    LowerRoman,
49    /// Uppercase Roman numerals.
50    #[serde(rename = "I")]
51    UpperRoman,
52    /// Decimal numbers.
53    #[serde(rename = "1")]
54    Decimal,
55}
56
57/// Caption of a rich formatted block.
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
59pub struct RichBlockCaption {
60    /// Block caption.
61    pub text: RichText,
62    /// Optional. Block credit (`<cite>`).
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub credit: Option<RichText>,
65}
66
67/// A cell in a table.
68#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
69pub struct RichBlockTableCell {
70    /// Optional. Text in the cell; omitted means the cell is invisible.
71    #[serde(default, skip_serializing_if = "Option::is_none")]
72    pub text: Option<RichText>,
73    /// Optional. True, if the cell is a header cell.
74    #[serde(default, skip_serializing_if = "Option::is_none")]
75    pub is_header: Option<bool>,
76    /// Optional. Number of columns the cell spans, if bigger than 1.
77    #[serde(default, skip_serializing_if = "Option::is_none")]
78    pub colspan: Option<u32>,
79    /// Optional. Number of rows the cell spans, if bigger than 1.
80    #[serde(default, skip_serializing_if = "Option::is_none")]
81    pub rowspan: Option<u32>,
82    /// Horizontal cell content alignment.
83    pub align: HorizontalAlign,
84    /// Vertical cell content alignment.
85    pub valign: VerticalAlign,
86}
87
88/// An item of a list to be sent.
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub struct InputRichBlockListItem {
91    /// The content of the item.
92    pub blocks: Vec<InputRichBlock>,
93    /// Optional. The item has a checkbox.
94    #[serde(default, skip_serializing_if = "Option::is_none")]
95    pub has_checkbox: Option<bool>,
96    /// Optional. The item has a checked checkbox.
97    #[serde(default, skip_serializing_if = "Option::is_none")]
98    pub is_checked: Option<bool>,
99    /// Optional. For ordered lists, the numeric value of the item label.
100    #[serde(default, skip_serializing_if = "Option::is_none")]
101    pub value: Option<i64>,
102    /// Optional. For ordered lists, the type of the item label.
103    #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
104    pub label_type: Option<ListLabelType>,
105}
106
107impl InputRichBlockListItem {
108    /// Creates a list item with the given content.
109    pub fn new(blocks: Vec<InputRichBlock>) -> Self {
110        Self {
111            blocks,
112            has_checkbox: None,
113            is_checked: None,
114            value: None,
115            label_type: None,
116        }
117    }
118}
119
120/// A block in a rich formatted message to be sent, tagged by the `type` field.
121#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
122#[serde(tag = "type", rename_all = "snake_case")]
123pub enum InputRichBlock {
124    /// A text paragraph (`<p>`).
125    Paragraph {
126        /// Text of the block.
127        text: RichText,
128    },
129    /// A section heading (`<h1>`–`<h6>`).
130    Heading {
131        /// Text of the block.
132        text: RichText,
133        /// Relative font size; 1-6, 1 is the largest.
134        size: u8,
135    },
136    /// A preformatted text block (`<pre><code>`).
137    Pre {
138        /// Text of the block.
139        text: RichText,
140        /// Optional. The programming language of the text.
141        #[serde(default, skip_serializing_if = "Option::is_none")]
142        language: Option<String>,
143    },
144    /// A footer (`<footer>`).
145    Footer {
146        /// Text of the block.
147        text: RichText,
148    },
149    /// A divider (`<hr/>`).
150    Divider,
151    /// A block formula (`<tg-math-block>`).
152    MathematicalExpression {
153        /// The expression in LaTeX format.
154        expression: String,
155    },
156    /// An anchor (`<a name="…">`).
157    Anchor {
158        /// The name of the anchor.
159        name: String,
160    },
161    /// A list (`<ul>` / `<ol>`).
162    List {
163        /// Items of the list.
164        items: Vec<InputRichBlockListItem>,
165    },
166    /// A block quotation (`<blockquote>`).
167    #[serde(rename = "blockquote")]
168    BlockQuotation {
169        /// Content of the block.
170        blocks: Vec<InputRichBlock>,
171        /// Optional. Credit of the block.
172        #[serde(default, skip_serializing_if = "Option::is_none")]
173        credit: Option<RichText>,
174    },
175    /// An expandable block quotation (`<blockquote expandable>`, Bot API 10.3).
176    #[serde(rename = "expandable_blockquote")]
177    ExpandableBlockQuotation {
178        /// Content of the block.
179        text: RichText,
180        /// Optional. Credit of the block.
181        #[serde(default, skip_serializing_if = "Option::is_none")]
182        credit: Option<RichText>,
183    },
184    /// A quotation with centered text (`<aside>`).
185    #[serde(rename = "pullquote")]
186    PullQuotation {
187        /// Text of the block.
188        text: RichText,
189        /// Optional. Credit of the block.
190        #[serde(default, skip_serializing_if = "Option::is_none")]
191        credit: Option<RichText>,
192    },
193    /// A collage (`<tg-collage>`).
194    Collage {
195        /// Elements of the collage.
196        blocks: Vec<InputRichBlock>,
197        /// Optional. Caption of the block.
198        #[serde(default, skip_serializing_if = "Option::is_none")]
199        caption: Option<RichBlockCaption>,
200    },
201    /// A slideshow (`<tg-slideshow>`).
202    Slideshow {
203        /// Elements of the slideshow.
204        blocks: Vec<InputRichBlock>,
205        /// Optional. Caption of the block.
206        #[serde(default, skip_serializing_if = "Option::is_none")]
207        caption: Option<RichBlockCaption>,
208    },
209    /// A table (`<table>`).
210    Table {
211        /// Cells of the table.
212        cells: Vec<Vec<RichBlockTableCell>>,
213        /// Optional. The table has borders.
214        #[serde(default, skip_serializing_if = "Option::is_none")]
215        is_bordered: Option<bool>,
216        /// Optional. The table is striped.
217        #[serde(default, skip_serializing_if = "Option::is_none")]
218        is_striped: Option<bool>,
219        /// Optional. Cells have smaller indents (Bot API 10.3).
220        #[serde(default, skip_serializing_if = "Option::is_none")]
221        is_compact: Option<bool>,
222        /// Optional. Caption of the table.
223        #[serde(default, skip_serializing_if = "Option::is_none")]
224        caption: Option<RichText>,
225    },
226    /// An expandable details block (`<details>`).
227    Details {
228        /// Always shown summary of the block.
229        summary: RichText,
230        /// Content of the block.
231        blocks: Vec<InputRichBlock>,
232        /// Optional. The content is visible by default.
233        #[serde(default, skip_serializing_if = "Option::is_none")]
234        is_open: Option<bool>,
235    },
236    /// A map (`<tg-map>`).
237    Map {
238        /// Location of the center of the map.
239        location: Location,
240        /// Optional. Zoom level; 0-24.
241        #[serde(default, skip_serializing_if = "Option::is_none")]
242        zoom: Option<u8>,
243        /// Optional. Width; 0-10000.
244        #[serde(default, skip_serializing_if = "Option::is_none")]
245        width: Option<u32>,
246        /// Optional. Height; 0-10000.
247        #[serde(default, skip_serializing_if = "Option::is_none")]
248        height: Option<u32>,
249        /// Optional. Caption of the block.
250        #[serde(default, skip_serializing_if = "Option::is_none")]
251        caption: Option<RichBlockCaption>,
252    },
253    /// A row of 1-8 buttons (`<tg-button-row>`, Bot API 10.3).
254    Buttons {
255        /// List of 1-8 buttons.
256        buttons: Vec<RichMessageButton>,
257        /// Optional. Horizontal alignment.
258        #[serde(default, skip_serializing_if = "Option::is_none")]
259        align: Option<HorizontalAlign>,
260    },
261    /// An animation (`<video>`).
262    Animation {
263        /// The animation; caption is ignored.
264        animation: InputMediaAnimation,
265        /// Optional. Caption of the block.
266        #[serde(default, skip_serializing_if = "Option::is_none")]
267        caption: Option<RichBlockCaption>,
268    },
269    /// A music file (`<audio>`).
270    Audio {
271        /// The audio; caption is ignored.
272        audio: InputMediaAudio,
273        /// Optional. Caption of the block.
274        #[serde(default, skip_serializing_if = "Option::is_none")]
275        caption: Option<RichBlockCaption>,
276    },
277    /// A general file (`<tg-document>`, Bot API 10.3).
278    Document {
279        /// The document; caption is ignored.
280        document: InputMediaDocument,
281        /// Optional. Caption of the block.
282        #[serde(default, skip_serializing_if = "Option::is_none")]
283        caption: Option<RichBlockCaption>,
284    },
285    /// A photo (`<img>`).
286    Photo {
287        /// The photo; caption is ignored.
288        photo: InputMediaPhoto,
289        /// Optional. Caption of the block.
290        #[serde(default, skip_serializing_if = "Option::is_none")]
291        caption: Option<RichBlockCaption>,
292    },
293    /// A video (`<video>`).
294    Video {
295        /// The video; caption is ignored.
296        video: InputMediaVideo,
297        /// Optional. Caption of the block.
298        #[serde(default, skip_serializing_if = "Option::is_none")]
299        caption: Option<RichBlockCaption>,
300    },
301    /// A voice note (`<audio>`).
302    VoiceNote {
303        /// The voice note; caption is ignored.
304        voice_note: InputMediaVoiceNote,
305        /// Optional. Caption of the block.
306        #[serde(default, skip_serializing_if = "Option::is_none")]
307        caption: Option<RichBlockCaption>,
308    },
309    /// A “Thinking…” placeholder; only valid in `sendRichMessageDraft`.
310    Thinking {
311        /// Text of the block.
312        text: RichText,
313    },
314}