use layer_tl_types as tl;
#[derive(Clone)]
pub struct Button {
inner: tl::enums::KeyboardButton,
}
impl Button {
pub fn callback(text: impl Into<String>, data: impl Into<Vec<u8>>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Callback(tl::types::KeyboardButtonCallback {
requires_password: false,
text: text.into(),
data: data.into(),
style: None,
}),
}
}
pub fn url(text: impl Into<String>, url: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Url(tl::types::KeyboardButtonUrl {
text: text.into(),
url: url.into(),
style: None,
}),
}
}
pub fn url_auth(
text: impl Into<String>,
url: impl Into<String>,
fwd_text: Option<String>,
bot: tl::enums::InputUser,
) -> Self {
Self {
inner: tl::enums::KeyboardButton::InputKeyboardButtonUrlAuth(
tl::types::InputKeyboardButtonUrlAuth {
request_write_access: false,
text: text.into(),
fwd_text,
url: url.into(),
bot,
style: None,
},
),
}
}
pub fn switch_inline(text: impl Into<String>, query: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::SwitchInline(tl::types::KeyboardButtonSwitchInline {
same_peer: true,
peer_types: None,
text: text.into(),
query: query.into(),
style: None,
}),
}
}
pub fn text(label: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::KeyboardButton(tl::types::KeyboardButton {
text: label.into(),
style: None,
}),
}
}
pub fn switch_elsewhere(text: impl Into<String>, query: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::SwitchInline(tl::types::KeyboardButtonSwitchInline {
same_peer: false,
peer_types: None,
text: text.into(),
query: query.into(),
style: None,
}),
}
}
pub fn webview(text: impl Into<String>, url: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::WebView(tl::types::KeyboardButtonWebView {
text: text.into(),
url: url.into(),
style: None,
}),
}
}
pub fn simple_webview(text: impl Into<String>, url: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::SimpleWebView(
tl::types::KeyboardButtonSimpleWebView {
text: text.into(),
url: url.into(),
style: None,
},
),
}
}
pub fn request_phone(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::RequestPhone(tl::types::KeyboardButtonRequestPhone {
text: text.into(),
style: None,
}),
}
}
pub fn request_geo(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::RequestGeoLocation(
tl::types::KeyboardButtonRequestGeoLocation {
text: text.into(),
style: None,
},
),
}
}
pub fn request_poll(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::RequestPoll(tl::types::KeyboardButtonRequestPoll {
quiz: None,
text: text.into(),
style: None,
}),
}
}
pub fn request_quiz(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::RequestPoll(tl::types::KeyboardButtonRequestPoll {
quiz: Some(true),
text: text.into(),
style: None,
}),
}
}
pub fn game(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Game(tl::types::KeyboardButtonGame {
text: text.into(),
style: None,
}),
}
}
pub fn buy(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Buy(tl::types::KeyboardButtonBuy {
text: text.into(),
style: None,
}),
}
}
pub fn copy_text(text: impl Into<String>, copy_text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Copy(tl::types::KeyboardButtonCopy {
text: text.into(),
copy_text: copy_text.into(),
style: None,
}),
}
}
pub fn into_raw(self) -> tl::enums::KeyboardButton {
self.inner
}
}
#[derive(Clone, Default)]
pub struct InlineKeyboard {
rows: Vec<Vec<Button>>,
}
impl InlineKeyboard {
pub fn new() -> Self {
Self::default()
}
pub fn row(mut self, buttons: impl IntoIterator<Item = Button>) -> Self {
self.rows.push(buttons.into_iter().collect());
self
}
pub fn into_markup(self) -> tl::enums::ReplyMarkup {
let rows = self
.rows
.into_iter()
.map(|row| {
tl::enums::KeyboardButtonRow::KeyboardButtonRow(tl::types::KeyboardButtonRow {
buttons: row.into_iter().map(Button::into_raw).collect(),
})
})
.collect();
tl::enums::ReplyMarkup::ReplyInlineMarkup(tl::types::ReplyInlineMarkup { rows })
}
}
impl From<InlineKeyboard> for tl::enums::ReplyMarkup {
fn from(kb: InlineKeyboard) -> Self {
kb.into_markup()
}
}
#[derive(Clone, Default)]
pub struct ReplyKeyboard {
rows: Vec<Vec<Button>>,
resize: bool,
single_use: bool,
selective: bool,
}
impl ReplyKeyboard {
pub fn new() -> Self {
Self::default()
}
pub fn row(mut self, buttons: impl IntoIterator<Item = Button>) -> Self {
self.rows.push(buttons.into_iter().collect());
self
}
pub fn resize(mut self) -> Self {
self.resize = true;
self
}
pub fn single_use(mut self) -> Self {
self.single_use = true;
self
}
pub fn selective(mut self) -> Self {
self.selective = true;
self
}
pub fn into_markup(self) -> tl::enums::ReplyMarkup {
let rows = self
.rows
.into_iter()
.map(|row| {
tl::enums::KeyboardButtonRow::KeyboardButtonRow(tl::types::KeyboardButtonRow {
buttons: row.into_iter().map(Button::into_raw).collect(),
})
})
.collect();
tl::enums::ReplyMarkup::ReplyKeyboardMarkup(tl::types::ReplyKeyboardMarkup {
resize: self.resize,
single_use: self.single_use,
selective: self.selective,
persistent: false,
rows,
placeholder: None,
})
}
}
impl From<ReplyKeyboard> for tl::enums::ReplyMarkup {
fn from(kb: ReplyKeyboard) -> Self {
kb.into_markup()
}
}