From d1c1b7500d749075dbf50ea75ec768b85550c522 Mon Sep 17 00:00:00 2001 From: Akulij Date: Mon, 26 May 2025 20:06:07 +0500 Subject: [PATCH] create script_handler function that creates teloxide's handler for botscript dispatch --- src/bot_handler.rs | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/bot_handler.rs diff --git a/src/bot_handler.rs b/src/bot_handler.rs new file mode 100644 index 0000000..8a670ec --- /dev/null +++ b/src/bot_handler.rs @@ -0,0 +1,86 @@ +use log::info; +use std::{ + str::FromStr, + sync::{Arc, RwLock}, +}; +use teloxide::{ + dispatching::UpdateFilterExt, + dptree::{self, Handler}, + prelude::DependencyMap, + types::{InlineKeyboardButton, InlineKeyboardMarkup, Message, Update}, + Bot, +}; + +use crate::{ + botscript::{self, BotMessage, RunnerConfig}, + commands::BotCommand, + db::{CallDB, DB}, + message_answerer::MessageAnswerer, + update_user_tg, BotResult, +}; + +pub fn script_handler( + rc: Arc>, +) -> Handler<'static, DependencyMap, BotResult<()>, teloxide::dispatching::DpHandlerDescription> { + dptree::entry() + .inspect(|u: Update| { + info!("{u:#?}"); // Print the update to the console with inspect + }) + .branch( + Update::filter_message() + .filter_map(|m: Message| m.text().and_then(|t| BotCommand::from_str(t).ok())) + .filter_map(move |bc: BotCommand| { + let rc = std::sync::Arc::clone(&rc); + let command = bc.command(); + + let rc = rc.read().expect("RwLock lock on commands map failed"); + + rc.get_command_message(command) + }) + .endpoint(botscript_command_handler), + ) +} + +async fn botscript_command_handler( + bot: Bot, + mut db: DB, + bm: BotMessage, + msg: Message, +) -> BotResult<()> { + info!("Eval BM: {:?}", bm); + let tguser = match msg.from.clone() { + Some(user) => user, + None => return Ok(()), // do nothing, cause its not usecase of function + }; + let user = db + .get_or_init_user(tguser.id.0 as i64, &tguser.first_name) + .await?; + let user = update_user_tg(user, &tguser); + user.update_user(&mut db).await?; + + let buttons = bm + .resolve_buttons(&mut db) + .await? + .map(|buttons| InlineKeyboardMarkup { + inline_keyboard: buttons + .iter() + .map(|r| { + r.iter() + .map(|b| match b { + botscript::ButtonLayout::Callback { + name, + literal: _, + callback, + } => InlineKeyboardButton::callback(name, callback), + }) + .collect() + }) + .collect(), + }); + let literal = bm.literal().map_or("", |s| s.as_str()); + + let ma = MessageAnswerer::new(&bot, &mut db, msg.chat.id.0); + ma.answer(literal, None, buttons).await?; + + Ok(()) +}