From aae1f386456a605c12263e2ba5f09dc36bb180b3 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 27 Mar 2022 17:40:18 +0800 Subject: Finally, it works. Further cleanup work is needed. --- src/web.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/web.rs (limited to 'src/web.rs') diff --git a/src/web.rs b/src/web.rs new file mode 100644 index 0000000..8b6505b --- /dev/null +++ b/src/web.rs @@ -0,0 +1,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 { + 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."), + } + } + })) +} -- cgit v1.2.3