Skip to main content

telegram_rich/types/
button.rs

1//! Buttons embedded in rich messages (Bot API 10.3).
2
3use serde::{Deserialize, Serialize};
4
5use super::common::{
6    CopyTextButton, DisabledButton, LoginUrl, SwitchInlineQueryChosenChat, WebAppInfo,
7};
8use super::rich_text::RichText;
9
10/// Style of a [`RichMessageButton`].
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(rename_all = "snake_case")]
13pub enum ButtonStyle {
14    /// Danger (usually red).
15    Danger,
16    /// Success (usually green).
17    Success,
18    /// Primary (accent color).
19    Primary,
20    /// Shown as a regular link without borders; callback buttons only.
21    Link,
22}
23
24/// A button in a rich message.
25///
26/// Exactly one of the fields other than `text` and `style` must be set.
27#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct RichMessageButton {
29    /// Text of the button; only plain text, custom emoji and date-time entities.
30    pub text: RichText,
31    /// Optional. Style of the button.
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub style: Option<ButtonStyle>,
34    /// Optional. HTTP or `tg://` URL to be opened when the button is pressed.
35    #[serde(default, skip_serializing_if = "Option::is_none")]
36    pub url: Option<String>,
37    /// Optional. Data sent in a callback query, 1-64 bytes.
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    pub callback_data: Option<String>,
40    /// Optional. Web App launched when the button is pressed (private chats only).
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub web_app: Option<WebAppInfo>,
43    /// Optional. HTTPS URL used to automatically authorize the user.
44    #[serde(default, skip_serializing_if = "Option::is_none")]
45    pub login_url: Option<LoginUrl>,
46    /// Optional. Switch to inline mode in a chosen chat.
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub switch_inline_query: Option<String>,
49    /// Optional. Switch to inline mode in the current chat.
50    #[serde(default, skip_serializing_if = "Option::is_none")]
51    pub switch_inline_query_current_chat: Option<String>,
52    /// Optional. Switch to inline mode in a chat of the specified type.
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub switch_inline_query_chosen_chat: Option<SwitchInlineQueryChosenChat>,
55    /// Optional. Copy the specified text to the clipboard.
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub copy_text: Option<CopyTextButton>,
58    /// Optional. The button is disabled and does nothing.
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub disabled: Option<DisabledButton>,
61}
62
63impl RichMessageButton {
64    fn empty(text: RichText) -> Self {
65        Self {
66            text,
67            style: None,
68            url: None,
69            callback_data: None,
70            web_app: None,
71            login_url: None,
72            switch_inline_query: None,
73            switch_inline_query_current_chat: None,
74            switch_inline_query_chosen_chat: None,
75            copy_text: None,
76            disabled: None,
77        }
78    }
79
80    /// Creates a URL button.
81    pub fn url(text: impl Into<RichText>, url: impl Into<String>) -> Self {
82        Self {
83            url: Some(url.into()),
84            ..Self::empty(text.into())
85        }
86    }
87
88    /// Creates a callback button.
89    pub fn callback(text: impl Into<RichText>, data: impl Into<String>) -> Self {
90        Self {
91            callback_data: Some(data.into()),
92            ..Self::empty(text.into())
93        }
94    }
95
96    /// Creates a copy-text button.
97    pub fn copy_text(text: impl Into<RichText>, copy: impl Into<String>) -> Self {
98        Self {
99            copy_text: Some(CopyTextButton { text: copy.into() }),
100            ..Self::empty(text.into())
101        }
102    }
103
104    /// Sets the button style.
105    pub fn with_style(mut self, style: ButtonStyle) -> Self {
106        self.style = Some(style);
107        self
108    }
109}