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
use serde::{Deserialize, Serialize};
use crate::entities::inline_keyboard_button::InlineKeyboardButton;
/// This object represents an [inline keyboard](https://core.telegram.org/bots/features#inline-keyboards) that appears right next to the message it belongs to.
///
/// API Reference: [link](https://core.telegram.org/bots/api/#inlinekeyboardmarkup)
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct InlineKeyboardMarkup {
/// Array of button rows, each represented by an Array of [InlineKeyboardButton](https://core.telegram.org/bots/api/#inlinekeyboardbutton) objects
pub inline_keyboard: Vec<Vec<InlineKeyboardButton>>,
}
// Divider: all content below this line will be preserved after code regen
impl InlineKeyboardMarkup {
pub fn new(inline_keyboard: impl Into<Vec<Vec<InlineKeyboardButton>>>) -> Self {
Self {
inline_keyboard: inline_keyboard.into(),
}
}
pub const fn empty() -> Self {
Self {
inline_keyboard: vec![],
}
}
// Adds empty row to the keyboard
#[must_use]
pub fn add_row(mut self) -> Self {
self.inline_keyboard.push(vec![]);
self
}
// Adds a button to the last row of the keyboard. New row will be created if the keyboard is empty
#[must_use]
pub fn add_button(mut self, button: impl Into<InlineKeyboardButton>) -> Self {
if self.inline_keyboard.is_empty() {
self.inline_keyboard.push(Vec::with_capacity(1));
}
if let Some(row) = self.inline_keyboard.last_mut() {
row.push(button.into());
}
self
}
// Adds a button to the new row
#[must_use]
pub fn add_button_row(self, button: impl Into<InlineKeyboardButton>) -> Self {
self.add_row().add_button(button)
}
}