summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 6b855590a362a8f2311e1053d7fc3f62c85cec40 (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
use std::error::Error;
use teloxide::prelude::*;
mod browser;
mod callbacks;
mod commands;
mod config;
mod keyboard;

use crate::config::Config;

use tokio_stream::wrappers::UnboundedReceiverStream;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    teloxide::enable_logging!();
    log::info!("Starting bot...");

    let bot = Bot::from_env().auto_send();
    let config = Config::from_env().unwrap();

    unsafe {
        // Open the browser
        browser::init(&config.driver_url).await;

        // Login using the credentials inside the `config`
        match browser::login(&config).await {
            Ok(_) => {
                log::info!("Logged in Smartedu");
            }
            Err(e) => {
                // Using the bot when the user is not logged in, is simply useless.
                panic!("You can't connect: `{}`, credentials are {:?}", e, config);
            }
        }
    }

    Dispatcher::new(bot)
        .messages_handler(|rx: DispatcherHandlerRx<AutoSend<Bot>, Message>| {
            UnboundedReceiverStream::new(rx).for_each_concurrent(None, |cx| async move {
                commands::handler(cx).await.log_on_error().await;
            })
        })
        .callback_queries_handler(|rx: DispatcherHandlerRx<AutoSend<Bot>, CallbackQuery>| {
            UnboundedReceiverStream::new(rx).for_each_concurrent(None, |cx| async move {
                let data = &cx.update.data;
                if let Some(text) = data {
                    let _ = callbacks::handler(&cx, text).await;
                }
            })
        })
        .dispatch()
        .await;

    log::info!("Closing bot... Goodbye!");

    Ok(())
}