Updated delete_message.rs to send a message if the deletion fails and added a function to just get a handler to chain with the schema

This commit is contained in:
StNicolay 2023-05-10 18:47:46 +03:00
parent 9365c75e6e
commit 8307bee4cf
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
2 changed files with 19 additions and 11 deletions

View File

@ -1,17 +1,26 @@
use teloxide::{adaptors::Throttle, prelude::*};
use crate::handlers::markups::deletion_markup;
use teloxide::{adaptors::Throttle, dispatching::DpHandlerDescription, prelude::*};
/// Deletes the message from the callback
pub async fn run(bot: Throttle<Bot>, q: CallbackQuery) -> crate::Result<()> {
match q.message {
Some(msg) => {
bot.delete_message(msg.chat.id, msg.id).await?;
Ok(())
async fn run(bot: Throttle<Bot>, q: CallbackQuery) -> crate::Result<()> {
if let Some(msg) = q.message {
if let Err(err) = bot.delete_message(msg.chat.id, msg.id).await {
let _ = bot
.send_message(msg.chat.id, "Error deleting the message")
.reply_markup(deletion_markup())
.await;
return Err(err.into());
}
None => Ok(()),
}
Ok(())
}
/// Filters the delete_message callbacks
pub fn filter(q: CallbackQuery) -> bool {
fn filter(q: CallbackQuery) -> bool {
matches!(q.data.as_deref(), Some("delete_message"))
}
/// Gets a handler for deleting the message
pub fn get_handler() -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> {
dptree::filter(filter).endpoint(run)
}

View File

@ -115,9 +115,8 @@ pub fn get_dispatcher(
.branch(command_handler)
.branch(endpoint(commands::default));
let callback_handler = Update::filter_callback_query()
.filter(callbacks::delete_message::filter)
.endpoint(callbacks::delete_message::run);
let callback_handler =
Update::filter_callback_query().chain(callbacks::delete_message::get_handler());
let handler = dptree::entry()
.branch(message_handler)