Reworked dispatcher, added testing "Thanks" endpoint

This commit is contained in:
Nefrace 2022-10-11 19:13:43 +03:00
parent 07138bb5e2
commit 9ebd181161
2 changed files with 41 additions and 10 deletions

View File

@ -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<Message> {
bot.send_message(msg.chat.id, Command::descriptions().to_string())
.await

View File

@ -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::<commands::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;
}