diff --git a/src/main.rs b/src/main.rs index 90de0ff..5234494 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,31 @@ -use teloxide::{payloads::SendMessageSetters, prelude::*, utils::render::RenderMessageTextHelper}; +pub mod db; +use crate::db::DB; + +use teloxide::{dispatching::dialogue::GetChatId, payloads::SendMessageSetters, prelude::*, utils::{command::BotCommands, render::RenderMessageTextHelper}}; use envconfig::Envconfig; #[derive(Envconfig)] struct Config { #[envconfig(from = "BOT_TOKEN")] pub bot_token: String, + #[envconfig(from = "DATABASE_URL")] + pub db_url: String, +} + +#[derive(BotCommands, Clone)] +#[command(rename_rule = "lowercase")] +enum UserCommands { + /// The first message of user + Start, + /// Shows this message. + Help, +} + +#[derive(BotCommands, Clone)] +#[command(rename_rule = "lowercase")] +enum AdminCommands { + /// Shows your ID. + MyId, } #[tokio::main] @@ -12,18 +33,85 @@ async fn main() -> Result<(), Box>{ dotenvy::dotenv()?; let config = Config::init_from_env()?; - let bot = Bot::new(config.bot_token); + let bot = Bot::new(&config.bot_token); + let db = DB::new(&config.db_url); - teloxide::repl(bot, |bot: Bot, msg: Message| async move { - let msgtext = msg.html_text().unwrap(); - println!("Message HTML Text: {}", msgtext); + let handler = dptree::entry() + .inspect(|u: Update| { + //eprintln!("{u:#?}"); // Print the update to the console with inspect + }) + .branch( + Update::filter_message() + .branch( + dptree::entry().filter_command::().endpoint(user_command_handler) + ) + .branch( + dptree::entry().filter(|msg: Message, mut db: DB| { + let user = db.get_or_init_user(msg.from.unwrap().id.0 as i64); + user.is_admin + }).filter_command::().endpoint(admin_command_handler) + ) + ) + .branch( + Update::filter_message().endpoint(echo) + ) + ; - let mut smsg = bot.send_message(msg.chat.id, msgtext); - smsg = smsg.parse_mode(teloxide::types::ParseMode::Html); - smsg.await?; - Ok(()) - }) - .await; + Dispatcher::builder(bot, handler) + .dependencies(dptree::deps![db]) + .enable_ctrlc_handler() + .build() + .dispatch() + .await; Ok(()) } + +async fn user_command_handler( + mut db: DB, + bot: Bot, + msg: Message, + cmd: UserCommands, +) -> Result<(), teloxide::RequestError> { + let user = db.get_or_init_user(msg.from.clone().unwrap().id.0 as i64); + println!("MSG: {}", msg.html_text().unwrap()); + match cmd { + UserCommands::Help => { + bot.send_message(msg.chat.id, UserCommands::descriptions().to_string()).await?; + Ok(()) + }, + _ => { + bot.send_message(msg.chat.id, "Not yet implemented").await?; + Ok(()) + } + } +} + +async fn admin_command_handler( + mut db: DB, + bot: Bot, + msg: Message, + cmd: AdminCommands, +) -> Result<(), teloxide::RequestError> { + let tguser = msg.from.clone().unwrap(); + let user = db.get_or_init_user(tguser.id.0 as i64); + println!("MSG: {}", msg.html_text().unwrap()); + match cmd { + AdminCommands::MyId => { + bot.send_message(msg.chat.id, format!("Your ID is: {}", tguser.id)).await?; + Ok(()) + } + _ => { + bot.send_message(msg.chat.id, "Not yet implemented").await?; + Ok(()) + } + } +} + +async fn echo( + bot: Bot, + msg: Message, +) -> Result<(), teloxide::RequestError> { + bot.send_message(msg.chat.id, msg.html_text().unwrap()).parse_mode(teloxide::types::ParseMode::Html).await?; + Ok(()) +}