mobot/api/
reply_markup.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Default, Debug, Serialize, Deserialize, Clone)]
4pub struct KeyboardButton {
5    /// Text of the button. If none of the optional fields are used, it will be sent as a message when the button is pressed
6    pub text: String,
7    // Optional fields omitted
8}
9
10impl<T: Into<String>> From<T> for KeyboardButton {
11    fn from(text: T) -> Self {
12        Self { text: text.into() }
13    }
14}
15
16#[derive(Default, Debug, Serialize, Deserialize, Clone)]
17pub struct InlineKeyboardButton {
18    /// Label text on the button
19    pub text: String,
20
21    /// HTTP or tg:// url to be opened when button is pressed
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub url: Option<String>,
24
25    /// Callback data to be sent in a callback query to the bot when button is pressed, 1-64 bytes.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub callback_data: Option<String>,
28}
29
30impl<T: Into<String>> From<T> for InlineKeyboardButton {
31    fn from(text: T) -> Self {
32        Self {
33            text: text.into(),
34            ..Default::default()
35        }
36    }
37}
38
39impl InlineKeyboardButton {
40    pub fn with_callback_data<T: Into<String>>(mut self, callback_data: T) -> Self {
41        self.callback_data = Some(callback_data.into());
42        self
43    }
44}
45
46#[derive(Debug, Serialize, Deserialize, Clone)]
47#[serde(untagged)]
48pub enum ReplyMarkup {
49    InlineKeyboardMarkup {
50        /// Array of button rows, each represented by an Array of KeyboardButton objects
51        inline_keyboard: Vec<Vec<InlineKeyboardButton>>,
52
53        /// Requests clients to resize the keyboard vertically for optimal fit
54        resize_keyboard: bool,
55
56        /// Requests clients to hide the keyboard as soon as it's been used
57        one_time_keyboard: bool,
58
59        /// Use this parameter if you want to show the keyboard to specific users only
60        selective: bool,
61
62        /// The placeholder to be shown in the input field when the keyboard is active; 1-64 characters
63        #[serde(skip_serializing_if = "Option::is_none")]
64        input_field_placeholder: Option<String>,
65
66        /// Requests clients to always show the keyboard in the chat (users may not otherwise see the keyboard)
67        is_persistent: bool,
68    },
69    ReplyKeyboardMarkup {
70        /// Array of button rows, each represented by an Array of KeyboardButton objects
71        keyboard: Vec<Vec<KeyboardButton>>,
72
73        /// Requests clients to resize the keyboard vertically for optimal fit
74        resize_keyboard: bool,
75
76        /// Requests clients to hide the keyboard as soon as it's been used
77        one_time_keyboard: bool,
78
79        /// Use this parameter if you want to show the keyboard to specific users only
80        selective: bool,
81
82        /// The placeholder to be shown in the input field when the keyboard is active; 1-64 characters
83        #[serde(skip_serializing_if = "Option::is_none")]
84        input_field_placeholder: Option<String>,
85
86        /// Requests clients to always show the keyboard in the chat (users may not otherwise see the keyboard)
87        is_persistent: bool,
88    },
89    ReplyKeyboardRemove {
90        /// Requests clients to remove the custom keyboard (user will not be
91        /// able to summon this keyboard; if you want to hide the keyboard from
92        /// sight but keep it accessible, use one_time_keyboard in ReplyKeyboardMarkup)
93        remove_keyboard: bool,
94
95        /// Use this parameter if you want to remove the keyboard for specific users only
96        selective: bool,
97    },
98    ForceReply {
99        /// Shows reply interface to the user, as if they manually selected the bot's message and tapped 'Reply'
100        force_reply: bool,
101
102        /// The placeholder to be shown in the input field when the keyboard is active; 1-64 characters
103        #[serde(skip_serializing_if = "Option::is_none")]
104        input_field_placeholder: Option<String>,
105
106        /// Use this parameter if you want to force reply from specific users only
107        selective: bool,
108    },
109}
110
111impl ReplyMarkup {
112    pub fn inline_keyboard_markup(inline_keyboard: Vec<Vec<InlineKeyboardButton>>) -> ReplyMarkup {
113        ReplyMarkup::InlineKeyboardMarkup {
114            inline_keyboard,
115            resize_keyboard: false,
116            one_time_keyboard: false,
117            selective: false,
118            input_field_placeholder: None,
119            is_persistent: false,
120        }
121    }
122
123    pub fn reply_keyboard_markup(keyboard: Vec<Vec<KeyboardButton>>) -> ReplyMarkup {
124        ReplyMarkup::ReplyKeyboardMarkup {
125            keyboard,
126            resize_keyboard: false,
127            one_time_keyboard: true,
128            selective: false,
129            input_field_placeholder: None,
130            is_persistent: false,
131        }
132    }
133
134    pub fn reply_keyboard_remove() -> ReplyMarkup {
135        ReplyMarkup::ReplyKeyboardRemove {
136            remove_keyboard: true,
137            selective: false,
138        }
139    }
140
141    pub fn force_reply() -> ReplyMarkup {
142        ReplyMarkup::ForceReply {
143            force_reply: true,
144            input_field_placeholder: None,
145            selective: false,
146        }
147    }
148}
149
150impl<T: Into<String>> From<T> for ReplyMarkup {
151    fn from(text: T) -> Self {
152        serde_json::from_str(&text.into()).unwrap()
153    }
154}