summaryrefslogtreecommitdiff
path: root/src/keyboard.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-08-05 19:16:49 +0200
committerSanto Cariotti <santo@dcariotti.me>2021-08-05 19:16:49 +0200
commiteaafe83dae4c12d44c9c7672a3b02c04b110cf6b (patch)
treef6f34c03338bdfb6bfdec915b0992a50db2276c0 /src/keyboard.rs
parent429489349688ce452f124a691c7542d2ca2177d1 (diff)
feat: create a module to manage the keyboard response
Diffstat (limited to 'src/keyboard.rs')
-rw-r--r--src/keyboard.rs35
1 files changed, 35 insertions, 0 deletions
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);
+}