added a filter for messages without user information

This commit is contained in:
StNicolay 2023-08-12 17:37:27 +03:00
parent df7a78a1c7
commit 072596a797
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
3 changed files with 55 additions and 1 deletions

51
src/filter_user_info.rs Normal file
View File

@ -0,0 +1,51 @@
use crate::prelude::*;
use teloxide::{dispatching::DpHandlerDescription, dptree::Handler};
#[inline]
fn has_no_user_info(msg: Message) -> bool {
msg.from().is_none()
}
#[inline]
async fn notify_about_no_user_info(
bot: Throttle<Bot>,
msg: Message,
state: State,
) -> crate::Result<()> {
use State::*;
const TEXT: &str = "Invalid message. Couldn't get the user information. Send the message again";
match state {
Start => {
bot.send_message(msg.chat.id, TEXT)
.reply_markup(deletion_markup())
.await?;
}
GetNewName(handler)
| GetMasterPass(handler)
| GetNewMasterPass(handler)
| GetLogin(handler)
| GetPassword(handler) => {
let mut handler = handler.lock().await;
handler
.previous
.alter_message(&bot, TEXT, None, None)
.await?;
}
GetUser(handler) => {
let mut handler = handler.lock().await;
handler
.previous
.alter_message(&bot, TEXT, None, None)
.await?;
}
};
Ok(())
}
/// Gets a handler that filters out the messages without user information
#[inline]
pub fn get_handler() -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> {
dptree::filter(has_no_user_info).endpoint(notify_about_no_user_info)
}

View File

@ -2,6 +2,7 @@ mod callbacks;
mod commands;
mod default;
mod errors;
mod filter_user_info;
mod macros;
mod markups;
mod master_password_check;
@ -47,6 +48,8 @@ fn get_dispatcher(
let message_handler = Update::filter_message()
.map_async(utils::delete_message)
// Filters out the messages without user information
.branch(filter_user_info::get_handler())
.branch(case![State::GetNewName(next)].endpoint(state::get_new_name))
.branch(case![State::GetMasterPass(next)].endpoint(state::get_master_pass))
.branch(case![State::GetNewMasterPass(next)].endpoint(state::get_new_master_pass))

View File

@ -70,7 +70,7 @@ type DynHanlder<T> = Box<
+ Send,
>;
pub struct Handler<T> {
pub struct Handler<T: ?Sized> {
pub func: Option<DynHanlder<T>>,
pub previous: MessageIds,