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

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 {
        browser::init(&config.driver_url).await;

        match browser::login(&config).await {
            Ok(_) => {
                log::info!("Logged in Smartedu");
            }
            Err(e) => {
                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(d) = data {
                    println!("{}", d);
                }
            })
        })
        .dispatch()
        .await;

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

    Ok(())
}