summaryrefslogtreecommitdiff
path: root/src/callbacks.rs
blob: c3bcb0307e7ae10524fe8824b3dbd5da77e48219 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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: &str,
) -> 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?;

                        Ok(true)
                    } else {
                        Ok(false)
                    }
                }
                Err(_) => Ok(false),
            }
        },
        "space" => unsafe {
            // Select the sapce
            match browser::select_option("select2-results__option", "data-select2-id", call[2])
                .await
            {
                Ok(result) => {
                    if result {
                        let timetable = browser::get_timetable().await.unwrap();
                        let keyboard = make_inline_keyboard(
                            &timetable,
                            "timetable",
                            chat_id.parse::<i64>().unwrap(),
                        )
                        .await;

                        // Edit the previous spaces message with timetable' buttons
                        cx.requester
                            .edit_message_text(
                                chat_id.to_string(),
                                cx.update.message.clone().unwrap().id,
                                "When?",
                            )
                            .await?;

                        cx.requester
                            .edit_message_reply_markup(
                                chat_id.to_string(),
                                cx.update.message.clone().unwrap().id,
                            )
                            .reply_markup(keyboard)
                            .await?;

                        Ok(true)
                    } else {
                        Ok(false)
                    }
                }
                Err(_) => Ok(false),
            }
        },
        "timetable" => unsafe {
            match browser::select_table_row(call[2]).await {
                Ok(result) => {
                    if result {
                        cx.requester
                            .edit_message_text(
                                chat_id.to_string(),
                                cx.update.message.clone().unwrap().id,
                                "Reservation made! ✅",
                            )
                            .await?;
                        Ok(true)
                    } else {
                        cx.requester
                            .edit_message_text(
                                chat_id.to_string(),
                                cx.update.message.clone().unwrap().id,
                                "Error, try again! ⚠️",
                            )
                            .await?;
                        Ok(false)
                    }
                }
                Err(_) => Ok(false),
            }
        },
        _ => Ok(false),
    }
}