From f1ecd0d1db3322f783cf10391375dd8fe6a564ed Mon Sep 17 00:00:00 2001 From: Akulij Date: Fri, 2 May 2025 17:15:44 +0300 Subject: [PATCH] create send_application_to_chat function --- src/main.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5035291..e65ddd8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ pub mod db; pub mod mongodb_storage; pub mod utils; +use db::application::Application; use db::callback_info::CallbackInfo; use log::{error, info, warn}; use std::time::Duration; @@ -368,6 +369,55 @@ async fn callback_handler(bot: Bot, mut db: DB, q: CallbackQuery) -> BotResult<( Ok(()) } +async fn send_application_to_chat( + bot: &Bot, + db: &mut DB, + app: &Application, +) -> BotResult<()> { + let chat_id: i64 = match db.get_literal_value("support_chat_id").await? { + Some(strcid) => match strcid.parse() { + Ok(cid) => cid, + Err(err) => { + notify_admin(&format!( + "Support chat_id should be a number. Got: {strcid}, err: {err}.\n\ + Anyways, applied user: {:?}", + app.from + )) + .await; + return Ok(()); + } + }, + None => { + notify_admin(&format!( + "support_chat_id is not set!!!\nAnyways, applied user: {:?}", + app.from + )) + .await; + return Ok(()); + } + }; + let msg = match db.get_literal_value("application_format").await? { + Some(msg) => msg + .replace("{user_id}", app.from.id.0.to_string().as_str()) + .replace( + "{username}", + app.from + .username + .clone() + .unwrap_or("Username not set".to_string()) + .as_str(), + ), + None => { + notify_admin("format for support_chat_id is not set").await; + return Ok(()); + } + }; + + bot.send_message(ChatId(chat_id), msg).await?; + + Ok(()) +} + /// This is an emergent situation function, so it should not return any Result, but handle Results /// on its own async fn notify_admin(text: &str) {