1use layer_tl_types as tl;
17
18#[derive(Clone)]
22pub struct Button {
23 inner: tl::enums::KeyboardButton,
24}
25
26impl Button {
27 pub fn callback(text: impl Into<String>, data: impl Into<Vec<u8>>) -> Self {
29 Self {
30 inner: tl::enums::KeyboardButton::Callback(
31 tl::types::KeyboardButtonCallback {
32 requires_password: false,
33 text: text.into(),
34 data: data.into(),
35 style: None,
36 },
37 ),
38 }
39 }
40
41 pub fn url(text: impl Into<String>, url: impl Into<String>) -> Self {
43 Self {
44 inner: tl::enums::KeyboardButton::Url(
45 tl::types::KeyboardButtonUrl {
46 text: text.into(),
47 url: url.into(),
48 style: None,
49 },
50 ),
51 }
52 }
53
54 pub fn url_auth(text: impl Into<String>, url: impl Into<String>, fwd_text: Option<String>, bot: tl::enums::InputUser) -> Self {
56 Self {
57 inner: tl::enums::KeyboardButton::InputKeyboardButtonUrlAuth(
58 tl::types::InputKeyboardButtonUrlAuth {
59 request_write_access: false,
60 text: text.into(),
61 fwd_text,
62 url: url.into(),
63 bot,
64 style: None,
65 },
66 ),
67 }
68 }
69
70 pub fn switch_inline(text: impl Into<String>, query: impl Into<String>) -> Self {
72 Self {
73 inner: tl::enums::KeyboardButton::SwitchInline(
74 tl::types::KeyboardButtonSwitchInline {
75 same_peer: true,
76 peer_types: None,
77 text: text.into(),
78 query: query.into(),
79 style: None,
80 },
81 ),
82 }
83 }
84
85 pub fn text(label: impl Into<String>) -> Self {
87 Self {
88 inner: tl::enums::KeyboardButton::KeyboardButton(
89 tl::types::KeyboardButton { text: label.into(), style: None },
90 ),
91 }
92 }
93
94 pub fn switch_elsewhere(text: impl Into<String>, query: impl Into<String>) -> Self {
96 Self {
97 inner: tl::enums::KeyboardButton::SwitchInline(
98 tl::types::KeyboardButtonSwitchInline {
99 same_peer: false,
100 peer_types: None,
101 text: text.into(),
102 query: query.into(),
103 style: None,
104 },
105 ),
106 }
107 }
108
109 pub fn webview(text: impl Into<String>, url: impl Into<String>) -> Self {
111 Self {
112 inner: tl::enums::KeyboardButton::WebView(
113 tl::types::KeyboardButtonWebView {
114 text: text.into(),
115 url: url.into(),
116 style: None,
117 },
118 ),
119 }
120 }
121
122 pub fn simple_webview(text: impl Into<String>, url: impl Into<String>) -> Self {
124 Self {
125 inner: tl::enums::KeyboardButton::SimpleWebView(
126 tl::types::KeyboardButtonSimpleWebView {
127 text: text.into(),
128 url: url.into(),
129 style: None,
130 },
131 ),
132 }
133 }
134
135 pub fn request_phone(text: impl Into<String>) -> Self {
137 Self {
138 inner: tl::enums::KeyboardButton::RequestPhone(
139 tl::types::KeyboardButtonRequestPhone {
140 text: text.into(),
141 style: None,
142 },
143 ),
144 }
145 }
146
147 pub fn request_geo(text: impl Into<String>) -> Self {
149 Self {
150 inner: tl::enums::KeyboardButton::RequestGeoLocation(
151 tl::types::KeyboardButtonRequestGeoLocation {
152 text: text.into(),
153 style: None,
154 },
155 ),
156 }
157 }
158
159 pub fn request_poll(text: impl Into<String>) -> Self {
161 Self {
162 inner: tl::enums::KeyboardButton::RequestPoll(
163 tl::types::KeyboardButtonRequestPoll {
164 quiz: None,
165 text: text.into(),
166 style: None,
167 },
168 ),
169 }
170 }
171
172 pub fn request_quiz(text: impl Into<String>) -> Self {
174 Self {
175 inner: tl::enums::KeyboardButton::RequestPoll(
176 tl::types::KeyboardButtonRequestPoll {
177 quiz: Some(true),
178 text: text.into(),
179 style: None,
180 },
181 ),
182 }
183 }
184
185 pub fn game(text: impl Into<String>) -> Self {
187 Self {
188 inner: tl::enums::KeyboardButton::Game(
189 tl::types::KeyboardButtonGame {
190 text: text.into(),
191 style: None,
192 },
193 ),
194 }
195 }
196
197 pub fn buy(text: impl Into<String>) -> Self {
199 Self {
200 inner: tl::enums::KeyboardButton::Buy(
201 tl::types::KeyboardButtonBuy {
202 text: text.into(),
203 style: None,
204 },
205 ),
206 }
207 }
208
209 pub fn copy_text(text: impl Into<String>, copy_text: impl Into<String>) -> Self {
211 Self {
212 inner: tl::enums::KeyboardButton::Copy(
213 tl::types::KeyboardButtonCopy {
214 text: text.into(),
215 copy_text: copy_text.into(),
216 style: None,
217 },
218 ),
219 }
220 }
221
222 pub fn into_raw(self) -> tl::enums::KeyboardButton {
224 self.inner
225 }
226}
227
228#[derive(Clone, Default)]
245pub struct InlineKeyboard {
246 rows: Vec<Vec<Button>>,
247}
248
249impl InlineKeyboard {
250 pub fn new() -> Self {
252 Self::default()
253 }
254
255 pub fn row(mut self, buttons: impl IntoIterator<Item = Button>) -> Self {
257 self.rows.push(buttons.into_iter().collect());
258 self
259 }
260
261 pub fn into_markup(self) -> tl::enums::ReplyMarkup {
263 let rows = self.rows.into_iter().map(|row| {
264 tl::enums::KeyboardButtonRow::KeyboardButtonRow(
265 tl::types::KeyboardButtonRow {
266 buttons: row.into_iter().map(Button::into_raw).collect(),
267 },
268 )
269 }).collect();
270
271 tl::enums::ReplyMarkup::ReplyInlineMarkup(
272 tl::types::ReplyInlineMarkup { rows }
273 )
274 }
275}
276
277impl From<InlineKeyboard> for tl::enums::ReplyMarkup {
278 fn from(kb: InlineKeyboard) -> Self {
279 kb.into_markup()
280 }
281}
282
283#[derive(Clone, Default)]
287pub struct ReplyKeyboard {
288 rows: Vec<Vec<Button>>,
289 resize: bool,
290 single_use: bool,
291 selective: bool,
292}
293
294impl ReplyKeyboard {
295 pub fn new() -> Self { Self::default() }
297
298 pub fn row(mut self, buttons: impl IntoIterator<Item = Button>) -> Self {
300 self.rows.push(buttons.into_iter().collect());
301 self
302 }
303
304 pub fn resize(mut self) -> Self { self.resize = true; self }
306
307 pub fn single_use(mut self) -> Self { self.single_use = true; self }
309
310 pub fn selective(mut self) -> Self { self.selective = true; self }
312
313 pub fn into_markup(self) -> tl::enums::ReplyMarkup {
315 let rows = self.rows.into_iter().map(|row| {
316 tl::enums::KeyboardButtonRow::KeyboardButtonRow(
317 tl::types::KeyboardButtonRow {
318 buttons: row.into_iter().map(Button::into_raw).collect(),
319 },
320 )
321 }).collect();
322
323 tl::enums::ReplyMarkup::ReplyKeyboardMarkup(
324 tl::types::ReplyKeyboardMarkup {
325 resize: self.resize,
326 single_use: self.single_use,
327 selective: self.selective,
328 persistent: false,
329 rows,
330 placeholder: None,
331 }
332 )
333 }
334}
335
336impl From<ReplyKeyboard> for tl::enums::ReplyMarkup {
337 fn from(kb: ReplyKeyboard) -> Self {
338 kb.into_markup()
339 }
340}