From 9ebd181161f8a53ad3d02ed641adb4ea3d4808dc Mon Sep 17 00:00:00 2001 From: Nefrace Date: Tue, 11 Oct 2022 19:13:43 +0300 Subject: [PATCH] Reworked dispatcher, added testing "Thanks" endpoint --- src/godette.rs | 12 ++++++++++++ src/main.rs | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/godette.rs b/src/godette.rs index 4f599f1..5bcd8d8 100644 --- a/src/godette.rs +++ b/src/godette.rs @@ -25,6 +25,18 @@ impl Godette { Ok(()) } + pub async fn message_dispatcher(bot: Bot, msg: Message) -> ResponseResult<()> { + let text = msg + .text() + .unwrap_or(msg.caption().unwrap_or_default()) + .to_string(); + match text.to_lowercase().find("спасибо") { + Some(_id) => bot.send_message(msg.chat.id, "Не за что!").await?, + None => todo!(), + }; + Ok(()) + } + async fn show_help(bot: Bot, msg: Message) -> ResponseResult { bot.send_message(msg.chat.id, Command::descriptions().to_string()) .await diff --git a/src/main.rs b/src/main.rs index aa1ed89..5505365 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,38 @@ mod commands; mod godette; -use teloxide::{ - prelude::*, types::ParseMode::MarkdownV2, utils::command::BotCommands, utils::markdown, -}; +use teloxide::prelude::*; + +use godette::Godette; #[tokio::main] async fn main() { pretty_env_logger::init(); - let bot = godette::Godette::new(); - teloxide::commands_repl( - bot.bot, - godette::Godette::commands_dispatcher, - commands::Command::ty(), - ) - .await; + let bot = Godette::new(); + let handler = Update::filter_message() + .branch( + dptree::entry() + .filter_command::() + .endpoint(Godette::commands_dispatcher), + ) + .branch( + dptree::filter(|msg: Message| { + msg.from() + .map(|user| user.id == UserId(60441930)) + .unwrap_or_default() + }) + .endpoint(Godette::message_dispatcher), + ); + Dispatcher::builder(bot.bot, handler) + .default_handler(|upd| async move { + log::warn!("Unhandled update: {:?}", upd); + }) + .error_handler(LoggingErrorHandler::with_custom_text( + "An error has occurred in the dispatcher", + )) + .enable_ctrlc_handler() + .build() + .dispatch() + .await; }