summaryrefslogtreecommitdiff
path: root/src/keyboard.rs
blob: 8cee93d342a331115e004ff5a67448e28e2ac8e8 (plain)
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
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,
    chat_id: i64,
) -> 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!("{}_{}_{}", chat_id, 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);
}