Add teloxide::filter_command

This commit is contained in:
Hirrolot 2022-04-25 19:16:05 +06:00
parent e529be0120
commit df0d13c42b
6 changed files with 37 additions and 9 deletions

View file

@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased
### Added
- The `dispatching::filter_command` function (also accessible as `teloxide::filter_command`) as a shortcut for `dptree::entry().filter_command()`.
### Changed
- Update teloxide-core to v0.6.0 with [Bot API 6.0] support [**BC**].

View file

@ -49,12 +49,11 @@ enum Command {
#[tokio::main]
async fn main() {
pretty_env_logger::init();
log::info!("Starting dialogue_bot...");
log::info!("Starting purchase bot...");
let bot = Bot::from_env().auto_send();
let command_handler = dptree::entry()
.filter_command::<Command>()
let command_handler = teloxide::filter_command::<Command, _>()
.branch(
teloxide::handler![State::Start]
.branch(teloxide::handler![Command::Help].endpoint(help))

View file

@ -189,6 +189,11 @@ where
///
/// See [`HandlerExt::enter_dialogue`].
///
/// ## Dependency requirements
///
/// - `Arc<S>`
/// - `Upd`
///
/// [`HandlerExt::enter_dialogue`]: super::HandlerExt::enter_dialogue
pub fn enter<Upd, S, D, Output>() -> Handler<'static, DependencyMap, Output, DpHandlerDescription>
where

View file

@ -42,7 +42,8 @@ pub trait HandlerExt<Output> {
/// - `Arc<S>`
/// - `Upd`
///
/// [`Dialogue<D, S>`]: Dialogue
/// [`Dialogue<D, S>`]: super::dialogue::Dialogue
/// [`Dialogue::get_or_default`]: super::dialogue::Dialogue::get_or_default
#[must_use]
fn enter_dialogue<Upd, S, D>(self) -> Self
where
@ -67,10 +68,7 @@ where
where
C: BotCommands + Send + Sync + 'static,
{
self.chain(dptree::filter_map(move |message: Message, me: Me| {
let bot_name = me.user.username.expect("Bots must have a username");
message.text().and_then(|text| C::parse(text, bot_name).ok())
}))
self.chain(filter_command::<C, Output>())
}
fn enter_dialogue<Upd, S, D>(self) -> Self
@ -91,3 +89,24 @@ where
self.chain(F::handler())
}
}
/// Returns a handler that accepts a parsed command `C`.
///
/// A call to this function is the same as `dptree::entry().filter_command()`.
///
/// See [`HandlerExt::filter_command`].
///
/// ## Dependency requirements
///
/// - [`crate::types::Message`]
/// - [`crate::types::Me`]
pub fn filter_command<C, Output>() -> Handler<'static, DependencyMap, Output, DpHandlerDescription>
where
C: BotCommands + Send + Sync + 'static,
Output: Send + Sync + 'static,
{
dptree::entry().chain(dptree::filter_map(move |message: Message, me: Me| {
let bot_name = me.user.username.expect("Bots must have a username");
message.text().and_then(|text| C::parse(text, bot_name).ok())
}))
}

View file

@ -113,6 +113,6 @@ pub use dispatcher::{Dispatcher, DispatcherBuilder, UpdateHandler};
pub use distribution::DefaultKey;
pub use filter_ext::{MessageFilterExt, UpdateFilterExt};
pub use handler_description::DpHandlerDescription;
pub use handler_ext::HandlerExt;
pub use handler_ext::{filter_command, HandlerExt};
#[allow(deprecated)]
pub use handler_factory::HandlerFactory;

View file

@ -79,6 +79,7 @@ pub use teloxide_core::*;
#[cfg(feature = "macros")]
pub use teloxide_macros as macros;
pub use dispatching::filter_command;
pub use dptree;
#[cfg(all(feature = "nightly", doctest))]