summaryrefslogtreecommitdiff
path: root/src/callbacks.rs
blob: a82d5e9341e2a3ceed87480182359f02f386d114 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::browser;
use crate::keyboard::make_inline_keyboard;
use std::error::Error;
use teloxide::{
    payloads::EditMessageReplyMarkupSetters,
    prelude::{AutoSend, Bot, CallbackQuery, Requester, UpdateWithCx},
};

/// Handle the string of callback data
pub async fn handler(
    cx: &UpdateWithCx<AutoSend<Bot>, CallbackQuery>,
    text: &String,
) -> Result<bool, Box<dyn Error + Send + Sync>> {
    let call: Vec<&str> = text.split("_").collect();

    let chat_id = call[0];
    // First part of `_` string is the type of callback we want to handle
    let type_ = call[1];

    match type_ {
        "faculty" => unsafe {
            // Select the faculty
            match browser::select_option("select2-results__option", "data-select2-id", call[2])
                .await
            {
                Ok(result) => {
                    if result {
                        let spaces = browser::get_spaces().await.unwrap();
                        let keyboard =
                            make_inline_keyboard(&spaces, "space", chat_id.parse::<i64>().unwrap())
                                .await;

                        // Edit the previous faculties message with spaces' buttons
                        cx.requester
                            .edit_message_reply_markup(
                                chat_id.to_string(),
                                cx.update.message.clone().unwrap().id,
                            )
                            .reply_markup(keyboard)
                            .await?;

                        return Ok(true);
                    } else {
                        return Ok(false);
                    }
                }
                Err(_) => {
                    return Ok(false);
                }
            }
        },
        _ => Ok(false),
    }
}