mobot/api/
reply_markup.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Default, Debug, Serialize, Deserialize, Clone)]
4pub struct KeyboardButton {
5 pub text: String,
7 }
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 pub text: String,
20
21 #[serde(skip_serializing_if = "Option::is_none")]
23 pub url: Option<String>,
24
25 #[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 inline_keyboard: Vec<Vec<InlineKeyboardButton>>,
52
53 resize_keyboard: bool,
55
56 one_time_keyboard: bool,
58
59 selective: bool,
61
62 #[serde(skip_serializing_if = "Option::is_none")]
64 input_field_placeholder: Option<String>,
65
66 is_persistent: bool,
68 },
69 ReplyKeyboardMarkup {
70 keyboard: Vec<Vec<KeyboardButton>>,
72
73 resize_keyboard: bool,
75
76 one_time_keyboard: bool,
78
79 selective: bool,
81
82 #[serde(skip_serializing_if = "Option::is_none")]
84 input_field_placeholder: Option<String>,
85
86 is_persistent: bool,
88 },
89 ReplyKeyboardRemove {
90 remove_keyboard: bool,
94
95 selective: bool,
97 },
98 ForceReply {
99 force_reply: bool,
101
102 #[serde(skip_serializing_if = "Option::is_none")]
104 input_field_placeholder: Option<String>,
105
106 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}