summaryrefslogtreecommitdiff
path: root/src/commands.rs
blob: 7ee8d83fe21232efed27ac9cb32d2e74314e7be9 (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
use crate::browser;
use crate::config::Config;
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)]
#[command(rename = "lowercase", description = "These commands are supported:")]
pub enum Command {
    #[command(description = "Display this text")]
    Help,
    #[command(description = "Reserve a classroom for tomorrow", parse_with = "split")]
    Room,
}

/// This is the handler for the commands.
/// It's called by `teloxide` every time someone write `/<text>`.
///
/// First it checks if the author is authorized to do the commands. Then match that text thanks to
/// the `parse` method of `BotCommand`.
pub async fn handler(
    cx: UpdateWithCx<AutoSend<Bot>, Message>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
    // In the config file there's a key for the Telegram username authorized to use this bot.
    let username = Config::from_env().unwrap().username;

    // Compare the author of the message with the Telegram username from the config
    if let Some(author) = &cx.update.from().unwrap().username {
        if *author != username {
            cx.reply_to("You are not allowed to do this action!")
                .await?;

            return Ok(());
        }
    }

    let txt = cx.update.text();
    if txt.is_none() {
        return Ok(());
    }

    if let Ok(command) = BotCommand::parse(txt.unwrap(), "unict-reservation") {
        match command {
            Command::Help => {
                // Just send the descriptions of all commands
                cx.answer(Command::descriptions()).await?;
            }
            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);
                cx.answer("Where?").reply_markup(keyboard).await?;
            }
        }
    } else {
        cx.reply_to("Command not found!").await?;
    }

    Ok(())
}