mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
add builders
This commit is contained in:
parent
b95b1c15aa
commit
73fd76997d
5 changed files with 134 additions and 1 deletions
|
@ -9,7 +9,7 @@ pub use self::{
|
|||
chat_photo::ChatPhoto,
|
||||
document::Document,
|
||||
force_reply::ForceReply,
|
||||
inline_keyboard_button::InlineKeyboardButton,
|
||||
inline_keyboard_button::{InlineKeyboardButton, InlineKeyboardButtonKind},
|
||||
inline_keyboard_markup::InlineKeyboardMarkup,
|
||||
input_file::InputFile,
|
||||
input_media::InputMedia,
|
||||
|
|
55
src/keyboards/inline_keyboard_button.rs
Normal file
55
src/keyboards/inline_keyboard_button.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
use crate::core::types::{InlineKeyboardButton, InlineKeyboardButtonKind};
|
||||
|
||||
pub struct InlineKeyboardButtonBuilder;
|
||||
|
||||
/// Build buttons
|
||||
///
|
||||
/// Example:
|
||||
/// ```edition2018
|
||||
/// use async_telegram_bot::keyboards::InlineKeyboardButtonBuilder;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let url_button = InlineKeyboardButtonBuilder::url(
|
||||
/// "Text".to_string(),
|
||||
/// "http://url.com".to_string(),
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
impl InlineKeyboardButtonBuilder {
|
||||
pub fn url(text: String, url: String) -> InlineKeyboardButton {
|
||||
InlineKeyboardButton {
|
||||
text,
|
||||
kind: InlineKeyboardButtonKind::Url(url),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn callback(text: String, callback_data: String)
|
||||
-> InlineKeyboardButton {
|
||||
InlineKeyboardButton {
|
||||
text,
|
||||
kind: InlineKeyboardButtonKind::CallbackData(callback_data),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn switch_inline_query(text: String, switch_inline_query: String)
|
||||
-> InlineKeyboardButton {
|
||||
InlineKeyboardButton {
|
||||
text,
|
||||
kind: InlineKeyboardButtonKind::SwitchInlineQuery(switch_inline_query)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn switch_inline_query_current_chat(
|
||||
text: String,
|
||||
switch_inline_query_current_chat: String
|
||||
) -> InlineKeyboardButton {
|
||||
|
||||
InlineKeyboardButton {
|
||||
text,
|
||||
kind: InlineKeyboardButtonKind::SwitchInlineQueryCurrentChat(
|
||||
switch_inline_query_current_chat
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
70
src/keyboards/inline_keyboard_markup.rs
Normal file
70
src/keyboards/inline_keyboard_markup.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
use crate::core::types::{InlineKeyboardMarkup, InlineKeyboardButton};
|
||||
|
||||
pub struct InlineKeyboardMarkupBuilder {
|
||||
keyboard: InlineKeyboardMarkup,
|
||||
}
|
||||
|
||||
/// Builder for [`InlineKeyboardMarkup`]
|
||||
///
|
||||
/// Example:
|
||||
/// ```edition2018
|
||||
/// use async_telegram_bot::keyboards;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let url_button = keyboards::InlineKeyboardButtonBuilder::url(
|
||||
/// "text".to_string(),
|
||||
/// "http://url.com".to_string()
|
||||
/// );
|
||||
/// let keyboard = keyboards::InlineKeyboardMarkupBuilder::new()
|
||||
/// .row(vec![url_button])
|
||||
/// .build();
|
||||
/// }
|
||||
/// ```
|
||||
impl InlineKeyboardMarkupBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
keyboard: InlineKeyboardMarkup {
|
||||
inline_keyboard: vec![]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn row(mut self, buttons: Vec<InlineKeyboardButton>) -> Self {
|
||||
self.keyboard.inline_keyboard.push(buttons);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn append_to_row(mut self, button: InlineKeyboardButton, index: usize)
|
||||
-> Self {
|
||||
match self.keyboard.inline_keyboard.get_mut(index) {
|
||||
Some(&mut mut buttons) => buttons.push(button),
|
||||
None => self.keyboard.inline_keyboard.push(vec![button])
|
||||
};
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> InlineKeyboardMarkup {
|
||||
self.keyboard
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::keyboards::InlineKeyboardButtonBuilder;
|
||||
|
||||
#[test]
|
||||
fn test_row() {
|
||||
let btn = InlineKeyboardButtonBuilder::url(
|
||||
"text".to_string(),
|
||||
"http://url".to_string(),
|
||||
);
|
||||
let kb = InlineKeyboardMarkupBuilder::new()
|
||||
.row(vec![btn.clone()])
|
||||
.build();
|
||||
let expected = InlineKeyboardMarkup {
|
||||
inline_keyboard: vec![vec![btn.clone()]],
|
||||
};
|
||||
assert_eq!(kb, expected);
|
||||
}
|
||||
}
|
7
src/keyboards/mod.rs
Normal file
7
src/keyboards/mod.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
mod inline_keyboard_button;
|
||||
mod inline_keyboard_markup;
|
||||
|
||||
pub use self::{
|
||||
inline_keyboard_button::InlineKeyboardButtonBuilder,
|
||||
inline_keyboard_markup::InlineKeyboardMarkupBuilder,
|
||||
};
|
|
@ -5,3 +5,4 @@ extern crate serde;
|
|||
|
||||
pub mod bot;
|
||||
pub mod core;
|
||||
pub mod keyboards;
|
||||
|
|
Loading…
Reference in a new issue