Added bot_mention_command filter

This commit is contained in:
LasterAlex 2024-08-17 22:45:27 +03:00
parent 0d63d3a8ce
commit 94c4955c49
No known key found for this signature in database
4 changed files with 57 additions and 2 deletions

View file

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Add `filter_boost_added` and `filter_reply_to_story` filters to `MessageFilterExt` trait - Add `filter_boost_added` and `filter_reply_to_story` filters to `MessageFilterExt` trait
- Add `filter_mention_command` filter to `HandlerExt` trait ([issue #494](https://github.com/teloxide/teloxide/issues/494))
## 0.13.0 - 2024-08-16 ## 0.13.0 - 2024-08-16

View file

@ -227,4 +227,4 @@ pub use dispatcher::{Dispatcher, DispatcherBuilder, UpdateHandler};
pub use distribution::DefaultKey; pub use distribution::DefaultKey;
pub use filter_ext::{MessageFilterExt, UpdateFilterExt}; pub use filter_ext::{MessageFilterExt, UpdateFilterExt};
pub use handler_description::DpHandlerDescription; pub use handler_description::DpHandlerDescription;
pub use handler_ext::{filter_command, HandlerExt}; pub use handler_ext::{filter_command, filter_mention_command, HandlerExt};

View file

@ -23,6 +23,18 @@ pub trait HandlerExt<Output> {
where where
C: BotCommands + Send + Sync + 'static; C: BotCommands + Send + Sync + 'static;
/// Returns a handler that accepts a parsed command `C` if the command
/// contains a bot mention, for example `/start@my_bot`.
///
/// ## Dependency requirements
///
/// - [`crate::types::Message`]
/// - [`crate::types::Me`]
#[must_use]
fn filter_mention_command<C>(self) -> Self
where
C: BotCommands + Send + Sync + 'static;
/// Passes [`Dialogue<D, S>`] and `D` as handler dependencies. /// Passes [`Dialogue<D, S>`] and `D` as handler dependencies.
/// ///
/// It does so by the following steps: /// It does so by the following steps:
@ -61,6 +73,13 @@ where
self.chain(filter_command::<C, Output>()) self.chain(filter_command::<C, Output>())
} }
fn filter_mention_command<C>(self) -> Self
where
C: BotCommands + Send + Sync + 'static,
{
self.chain(filter_mention_command::<C, Output>())
}
fn enter_dialogue<Upd, S, D>(self) -> Self fn enter_dialogue<Upd, S, D>(self) -> Self
where where
S: Storage<D> + ?Sized + Send + Sync + 'static, S: Storage<D> + ?Sized + Send + Sync + 'static,
@ -93,3 +112,38 @@ where
message.text().and_then(|text| C::parse(text, &bot_name).ok()) message.text().and_then(|text| C::parse(text, &bot_name).ok())
}) })
} }
/// Returns a handler that accepts a parsed command `C` if the command
/// contains a bot mention, for example `/start@my_bot`.
///
/// A call to this function is the same as
/// `dptree::entry().filter_mention_command()`.
///
/// See [`HandlerExt::filter_mention_command`].
///
/// ## Dependency requirements
///
/// - [`crate::types::Message`]
/// - [`crate::types::Me`]
#[must_use]
pub fn filter_mention_command<C, Output>(
) -> Handler<'static, DependencyMap, Output, DpHandlerDescription>
where
C: BotCommands + Send + Sync + 'static,
Output: Send + Sync + 'static,
{
dptree::filter_map(move |message: Message, me: Me| {
let bot_name = me.user.username.expect("Bots must have a username");
let command = message.text().and_then(|text| C::parse(text, &bot_name).ok());
// If the parsing succeeds with a bot_name,
// but fails without - there is a mention
let is_username_required =
message.text().and_then(|text| C::parse(text, "").ok()).is_none();
if !is_username_required {
return None;
}
command
})
}

View file

@ -149,7 +149,7 @@ pub use teloxide_core::*;
#[cfg(feature = "macros")] #[cfg(feature = "macros")]
pub use teloxide_macros as macros; pub use teloxide_macros as macros;
pub use dispatching::filter_command; pub use dispatching::{filter_command, filter_mention_command};
pub use dptree::{self, case as handler}; pub use dptree::{self, case as handler};
#[cfg(all(feature = "nightly", doctest))] #[cfg(all(feature = "nightly", doctest))]