summaryrefslogtreecommitdiff
path: root/src/web.rs
blob: 8b6505bb657d46465c623a97038f95d6d0d14f06 (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
use serde_derive::{Deserialize, Serialize};
use teloxide::{prelude2::*};
use warp::{Rejection, Reply};

use crate::{Bot, database, DbPool};

#[derive(Deserialize, Serialize)]
pub struct SendMessage {
    token: String,
    message: String,
}

#[derive(Deserialize, Serialize)]
pub struct SendMessageResponse {
    success: bool,
    message: String,
}

pub async fn handler(req: SendMessage, db: DbPool, bot: Bot) -> std::result::Result<impl Reply, Rejection> {
    println!("Token: {}, Message: {}", req.token, req.message);
    let user = database::get_user_by_token(&db, req.token.as_str()).await;
    Ok(warp::reply::json(&match user {
        Ok(u) => match u {
            Some(user) => {
                log::info!("User: {} (id={}), message: {}",
                    user.name, user.id, req.message);
                // TODO send message to Telegram
                let bot = bot.auto_send();
                match bot.send_message(user.chat_id as i64, req.message).await {
                    Ok(_) => SendMessageResponse {
                        success: true,
                        message: String::new(),
                    },
                    Err(why) => {
                        println!("Failed to send message to telegram: {:?}", why);
                        SendMessageResponse {
                            success: false,
                            message: String::from("Failed to send message to telegram."),
                        }
                    }
                }
            }
            None => {
                log::warn!("Invalid token {}, message: {}", req.token, req.message);
                SendMessageResponse {
                    success: false,
                    message: String::from("Invalid token."),
                }
            }
        },
        Err(_) => {
            log::error!("Error when querying the database.");
            SendMessageResponse {
                success: false,
                message: String::from("Invalid parameter."),
            }
        }
    }))
}