diff options
-rw-r--r-- | src/commands.rs | 28 | ||||
-rw-r--r-- | src/keyboard.rs | 35 | ||||
-rw-r--r-- | src/main.rs | 1 |
3 files changed, 38 insertions, 26 deletions
diff --git a/src/commands.rs b/src/commands.rs index 7ee8d83..512e3c1 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,9 +1,9 @@ use crate::browser; use crate::config::Config; +use crate::keyboard::make_inline_keyboard; use std::error::Error; use teloxide::payloads::SendMessageSetters; use teloxide::prelude::{AutoSend, Bot, Message, UpdateWithCx}; -use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup}; use teloxide::utils::command::BotCommand; #[derive(BotCommand)] @@ -50,35 +50,11 @@ pub async fn handler( Command::Room => { let faculties; - // This is an array of array because the `InlineKeyboardMarkup` - // considers each array as a row. - // So, using this format Vec<Vec<..>> will print a button - // in `n` different rows in only 1 column. - let mut faculties_array: Vec<Vec<InlineKeyboardButton>> = vec![]; unsafe { faculties = browser::get_faculties().await.unwrap(); } - if let Some(faculties_texts) = faculties { - for (key, value) in faculties_texts { - faculties_array.push(vec![InlineKeyboardButton::callback( - value, - format!("faculty_{}", key), - )]); - } - } else { - // If the response of the Option `faculties` is None, just answer - // an useless button with a text. - // I still don't know if it's a good idea to use a callback instead of - // a normal text button, but I could handle any such kind of callback - faculties_array.push(vec![InlineKeyboardButton::callback( - "No such element".to_string(), - "".into(), - )]); - } - - // The `new` method accepts an interator - let keyboard = InlineKeyboardMarkup::new(faculties_array); + let keyboard = make_inline_keyboard(&faculties, "faculty").await; cx.answer("Where?").reply_markup(keyboard).await?; } } diff --git a/src/keyboard.rs b/src/keyboard.rs new file mode 100644 index 0000000..ea57d66 --- /dev/null +++ b/src/keyboard.rs @@ -0,0 +1,35 @@ +use std::collections::HashMap; +use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup}; + +/// Create a new InlineKeyboardMarkup from an `hashmap` defined as `<id>:<text>` +pub async fn make_inline_keyboard( + hashmap: &Option<HashMap<String, String>>, + callback_type: &str, +) -> InlineKeyboardMarkup { + // This is an array of array because the `InlineKeyboardMarkup` + // considers each array as a row. + // So, using this format Vec<Vec<..>> will print a button + // in `n` different rows in only 1 column. + let mut keyboard_array: Vec<Vec<InlineKeyboardButton>> = vec![]; + + if let Some(options) = hashmap { + for (key, value) in options { + keyboard_array.push(vec![InlineKeyboardButton::callback( + value.clone(), + format!("{}_{}", callback_type, key), + )]); + } + } else { + // If the response of the Option ``callback_type`` is None, just answer + // an useless button with a text. + // I still don't know if it's a good idea to use a callback instead of + // a normal text button, but I could handle any such kind of callback + keyboard_array.push(vec![InlineKeyboardButton::callback( + "No such element".to_string(), + "".into(), + )]); + } + + // The `new` method accepts an interator + return InlineKeyboardMarkup::new(keyboard_array); +} diff --git a/src/main.rs b/src/main.rs index 79b6e6e..92e661e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod browser; mod callbacks; mod commands; mod config; +mod keyboard; use crate::config::Config; |