tgbot 0.47.0

A Telegram Bot library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use serde::{Deserialize, Serialize};

use super::text::RichText;
use crate::types::{Animation, Audio, Integer, Location, PhotoSize, Video, Voice};

/// Represents a block in a rich formatted message.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(from = "RawRichBlock", into = "RawRichBlock")]
pub enum RichBlock {
    /// An anchor (`<a>` with the `name` attribute).
    Anchor(String),
    /// An animation (`<video>`).
    Animation(RichBlockAnimation),
    /// An audio (`<audio>`).
    Audio(RichBlockAudio),
    /// A block quotation (`<blockquote>`).
    BlockQuotation(RichBlockBlockQuotation),
    /// A collage (`<tg-collage>`).
    Collage(RichBlockCollage),
    /// An expandable block for details disclosure (`<details>`).
    Details(RichBlockDetails),
    /// A divider (`<hr />`).
    Divider,
    /// A footer (`<footer>`).
    Footer(RichText),
    /// A list (`<ul>` or `<ol>`).
    List(Vec<RichBlockListItem>),
    /// A map (`<tg-map>`).
    Map(RichBlockMap),
    /// A mathematical expression (`<tg-math-block>`).
    MathematicalExpression(String),
    /// A paragraph (`<p>`).
    Paragraph(RichText),
    /// A Photo (`<photo>`).
    Photo(RichBlockPhoto),
    /// A preformatted text (`<pre>` or `<code>`).
    Preformatted(RichBlockPreformatted),
    /// A quotation with centered text (`<aside>`).
    PullQuotation(RichBlockPullQuotation),
    /// A section heading (`<h[1-6]>` text, size).
    SectionHeading(RichText, Integer),
    /// A slideshow (`<tg-slideshow>`).
    Slideshow(RichBlockSlideshow),
    /// A table (`<table>`).
    Table(RichBlockTable),
    /// A "Thinking ..." placeholder (`<tg-thinking>`).
    ///
    /// The block may be used only in `[crate::types::SendRichMessageDraft]`.
    Thinking(RichText),
    /// A video (`<video>`).
    Video(RichBlockVideo),
    /// A voice note (`<audio>`).
    VoiceNote(RichBlockVoiceNote),
}

/// A block with an animation (`<video>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockAnimation {
    /// The animation.
    pub animation: Animation,
    /// Caption of the block.
    pub caption: Option<RichBlockCaption>,
    /// Whether the media preview is covered by a spoiler animation.
    pub has_spoiler: Option<bool>,
}

/// A block with a music file (`<audio>`)
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockAudio {
    /// The audio.
    pub audio: Audio,
    /// Caption of the block.
    pub caption: Option<RichBlockCaption>,
}

/// A block quotation (`<blockquote>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockBlockQuotation {
    /// Content of the block.
    pub blocks: Vec<RichBlock>,
    /// Credit of the block.
    pub credit: Option<RichText>,
}

/// Caption of a rich formatted block.
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockCaption {
    /// Block caption.
    pub text: RichText,
    /// Block credit which corresponds to the HTML tag `<cite>`.
    pub credit: Option<RichText>,
}

impl<T> From<T> for RichBlockCaption
where
    T: Into<RichText>,
{
    fn from(value: T) -> Self {
        Self {
            text: value.into(),
            credit: None,
        }
    }
}

impl<A, B> From<(A, B)> for RichBlockCaption
where
    A: Into<RichText>,
    B: Into<RichText>,
{
    fn from((text, credit): (A, B)) -> Self {
        Self::from(text).with_credit(credit)
    }
}

impl RichBlockCaption {
    /// Sets a new credit.
    ///
    /// # Arguments
    ///
    /// * `value` - Block credit.
    pub fn with_credit<T>(mut self, value: T) -> Self
    where
        T: Into<RichText>,
    {
        self.credit = Some(value.into());
        self
    }
}

/// A collage (`<tg-collage>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockCollage {
    /// Elements of the collage.
    pub blocks: Vec<RichBlock>,
    /// Caption of the block.
    pub caption: Option<RichBlockCaption>,
}

/// An expandable block for details disclosure (`<details>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockDetails {
    /// Content of the block.
    pub blocks: Vec<RichBlock>,
    /// Always shown summary of the block.
    pub summary: RichText,
    /// Whether the content of the block is visible by default.
    pub is_open: Option<bool>,
}

/// An item of a list.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RichBlockListItem {
    /// The content of the item.
    pub blocks: Vec<RichBlock>,
    /// Label of the item.
    pub label: String,
    /// Whether the item has a checkbox.
    pub has_checkbox: Option<bool>,
    /// Whether the item has a checked checkbox.
    pub is_checked: Option<bool>,
    /// For ordered lists, the type of the item label.
    #[serde(rename = "type")]
    pub item_type: Option<RichBlockListItemType>,
    /// For ordered lists, the numberic value of the item label.
    pub value: Option<Integer>,
}

/// Represents the type of the item label.
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum RichBlockListItemType {
    /// Decimal numbers.
    #[serde(rename = "1")]
    Decimal,
    /// Lowercase letters.
    #[serde(rename = "a")]
    LowercaseLetters,
    /// Lowercase roman numerals.
    #[serde(rename = "i")]
    LowercaseRoman,
    /// Uppercase letters.
    #[serde(rename = "A")]
    UppercaseLetters,
    /// Uppercase roman numerals.
    #[serde(rename = "I")]
    UppercaseRoman,
}

/// Ablock with a map (`<tg-map>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockMap {
    /// Location of the center of the map.
    pub location: Location,
    /// Expected height of the map.
    pub height: Integer,
    /// Expected width of the map.
    pub width: Integer,
    /// Map zoom level; 13-20.
    pub zoom: Integer,
    /// Caption of the block.
    pub caption: Option<RichBlockCaption>,
}

/// A block with a photo (`<photo>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockPhoto {
    /// Available sizes of the photo.
    pub photo: Vec<PhotoSize>,
    /// Caption of the block.
    pub caption: Option<RichBlockCaption>,
    /// Whether the media preview is covered by a spoiler animation.
    pub has_spoiler: Option<bool>,
}

/// A preformatted text block (`<pre>` or `<code>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockPreformatted {
    /// Text of the block.
    pub text: RichText,
    /// The programming language of the text.
    pub language: Option<String>,
}

/// A quotation with centered text (`<aside>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockPullQuotation {
    /// Text of the block.
    pub text: RichText,
    /// Credit of the block.
    pub credit: Option<RichText>,
}

/// A slideshow (`<tg-slideshow>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockSlideshow {
    /// Elements of the slideshow.
    pub blocks: Vec<RichBlock>,
    /// Caption of the block.
    pub caption: Option<RichBlockCaption>,
}

/// A table (`<table>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockTable {
    /// Cells of the table.
    pub cells: Vec<Vec<RichBlockTableCell>>,
    /// Caption of the table.
    pub caption: Option<RichText>,
    /// Whether the table has borders.
    pub is_bordered: Option<bool>,
    /// Whether the table is striped.
    pub is_striped: Option<bool>,
}

/// Cell in a table.
#[serde_with::skip_serializing_none]
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct RichBlockTableCell {
    /// Horizontal cell content alignment.
    pub align: RichBlockTableCellAlign,
    /// Vertical cell content alignment.
    pub valign: RichBlockTableCellValign,
    /// The number of columns the cell spans if it is bigger than 1.
    pub colspan: Option<Integer>,
    /// Whether the cell is a header cell.
    pub is_header: Option<bool>,
    /// The number of rows the cell spans if it is bigger than 1.
    pub rowspan: Option<Integer>,
    /// Text in the cell.
    ///
    /// If omitted, the the cell is invisible.
    pub text: Option<RichText>,
}

impl<T> From<T> for RichBlockTableCell
where
    T: Into<RichText>,
{
    fn from(value: T) -> Self {
        Self::default().with_text(value)
    }
}

impl RichBlockTableCell {
    /// Sets a new horizontal alignment.
    ///
    /// # Arguments
    ///
    /// * `value` - The horizontal alignment.
    pub fn with_align(mut self, value: RichBlockTableCellAlign) -> Self {
        self.align = value;
        self
    }

    /// Sets a new colspan.
    ///
    /// # Arguments
    ///
    /// * `value` - The number of columns the cell span.
    pub fn with_colspan(mut self, value: Integer) -> Self {
        self.colspan = Some(value);
        self
    }

    /// Sets a new value for the `is_header` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the cell is a header cell.
    pub fn with_is_header(mut self, value: bool) -> Self {
        self.is_header = Some(value);
        self
    }

    /// Sets a new rowspan.
    ///
    /// # Arguments
    ///
    /// * `value` - The number of rows the cell spans.
    pub fn with_rowspan(mut self, value: Integer) -> Self {
        self.rowspan = Some(value);
        self
    }

    /// Sets a new text.
    ///
    /// # Arguments
    ///
    /// * `value` - The text.
    pub fn with_text<T>(mut self, value: T) -> Self
    where
        T: Into<RichText>,
    {
        self.text = Some(value.into());
        self
    }

    /// Sets a new vertical alignment.
    ///
    /// # Arguments
    ///
    /// * `value` - The vertical alignment.
    pub fn with_valign(mut self, value: RichBlockTableCellValign) -> Self {
        self.valign = value;
        self
    }
}

/// Horizontall cell content alignment.
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RichBlockTableCellAlign {
    /// Left.
    #[default]
    Left,
    /// Center.
    Center,
    /// Right.
    Right,
}

/// Vertical cell content alignment.
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RichBlockTableCellValign {
    /// Top.
    Top,
    /// Middle.
    #[default]
    Middle,
    /// Bottom.
    Bottom,
}

/// A block with a video (`<video`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockVideo {
    /// The video.
    pub video: Video,
    /// Caption of the block.
    pub caption: Option<RichBlockCaption>,
    /// Whether the media preview is covered by a spoiler animation.
    pub has_spoiler: Option<bool>,
}

/// A block with a voice note (`<audio>`).
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RichBlockVoiceNote {
    /// The voice note.
    pub voice_note: Voice,
    /// Caption of the block.
    pub caption: Option<RichBlockCaption>,
}

#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case", tag = "type")]
enum RawRichBlock {
    Anchor {
        name: String,
    },
    Animation(RichBlockAnimation),
    Audio(RichBlockAudio),
    #[serde(rename = "blockquote")]
    BlockQuotation(RichBlockBlockQuotation),
    Collage(RichBlockCollage),
    Details(RichBlockDetails),
    Divider,
    Footer {
        text: RichText,
    },
    List {
        items: Vec<RichBlockListItem>,
    },
    Map(RichBlockMap),
    MathematicalExpression {
        expression: String,
    },
    Paragraph {
        text: RichText,
    },
    Photo(RichBlockPhoto),
    #[serde(rename = "pre")]
    Preformatted(RichBlockPreformatted),
    #[serde(rename = "pullquote")]
    PullQuotation(RichBlockPullQuotation),
    #[serde(rename = "heading")]
    SectionHeading {
        text: RichText,
        size: Integer,
    },
    Slideshow(RichBlockSlideshow),
    Table(RichBlockTable),
    Thinking {
        text: RichText,
    },
    Video(RichBlockVideo),
    VoiceNote(RichBlockVoiceNote),
}

impl From<RichBlock> for RawRichBlock {
    fn from(value: RichBlock) -> Self {
        match value {
            RichBlock::Anchor(name) => Self::Anchor { name },
            RichBlock::Animation(value) => Self::Animation(value),
            RichBlock::Audio(value) => Self::Audio(value),
            RichBlock::BlockQuotation(value) => Self::BlockQuotation(value),
            RichBlock::Collage(value) => Self::Collage(value),
            RichBlock::Details(value) => Self::Details(value),
            RichBlock::Divider => Self::Divider,
            RichBlock::Footer(text) => Self::Footer { text },
            RichBlock::List(items) => Self::List { items },
            RichBlock::Map(value) => Self::Map(value),
            RichBlock::MathematicalExpression(expression) => Self::MathematicalExpression { expression },
            RichBlock::Paragraph(text) => Self::Paragraph { text },
            RichBlock::Photo(value) => Self::Photo(value),
            RichBlock::Preformatted(value) => Self::Preformatted(value),
            RichBlock::PullQuotation(value) => Self::PullQuotation(value),
            RichBlock::SectionHeading(text, size) => Self::SectionHeading { text, size },
            RichBlock::Slideshow(value) => Self::Slideshow(value),
            RichBlock::Table(value) => Self::Table(value),
            RichBlock::Thinking(text) => Self::Thinking { text },
            RichBlock::Video(value) => Self::Video(value),
            RichBlock::VoiceNote(value) => Self::VoiceNote(value),
        }
    }
}

impl From<RawRichBlock> for RichBlock {
    fn from(value: RawRichBlock) -> Self {
        match value {
            RawRichBlock::Anchor { name } => Self::Anchor(name),
            RawRichBlock::Animation(value) => Self::Animation(value),
            RawRichBlock::Audio(value) => Self::Audio(value),
            RawRichBlock::BlockQuotation(value) => Self::BlockQuotation(value),
            RawRichBlock::Collage(value) => Self::Collage(value),
            RawRichBlock::Details(value) => Self::Details(value),
            RawRichBlock::Divider => Self::Divider,
            RawRichBlock::Footer { text } => Self::Footer(text),
            RawRichBlock::List { items } => Self::List(items),
            RawRichBlock::Map(value) => Self::Map(value),
            RawRichBlock::MathematicalExpression { expression } => Self::MathematicalExpression(expression),
            RawRichBlock::Paragraph { text } => Self::Paragraph(text),
            RawRichBlock::Photo(value) => Self::Photo(value),
            RawRichBlock::Preformatted(value) => Self::Preformatted(value),
            RawRichBlock::PullQuotation(value) => Self::PullQuotation(value),
            RawRichBlock::SectionHeading { text, size } => Self::SectionHeading(text, size),
            RawRichBlock::Slideshow(value) => Self::Slideshow(value),
            RawRichBlock::Table(value) => Self::Table(value),
            RawRichBlock::Thinking { text } => Self::Thinking(text),
            RawRichBlock::Video(value) => Self::Video(value),
            RawRichBlock::VoiceNote(value) => Self::VoiceNote(value),
        }
    }
}