mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Add teloxide::filter_command
This commit is contained in:
parent
e529be0120
commit
df0d13c42b
6 changed files with 37 additions and 9 deletions
|
@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## unreleased
|
## unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- The `dispatching::filter_command` function (also accessible as `teloxide::filter_command`) as a shortcut for `dptree::entry().filter_command()`.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Update teloxide-core to v0.6.0 with [Bot API 6.0] support [**BC**].
|
- Update teloxide-core to v0.6.0 with [Bot API 6.0] support [**BC**].
|
||||||
|
|
|
@ -49,12 +49,11 @@ enum Command {
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting dialogue_bot...");
|
log::info!("Starting purchase bot...");
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env().auto_send();
|
||||||
|
|
||||||
let command_handler = dptree::entry()
|
let command_handler = teloxide::filter_command::<Command, _>()
|
||||||
.filter_command::<Command>()
|
|
||||||
.branch(
|
.branch(
|
||||||
teloxide::handler![State::Start]
|
teloxide::handler![State::Start]
|
||||||
.branch(teloxide::handler![Command::Help].endpoint(help))
|
.branch(teloxide::handler![Command::Help].endpoint(help))
|
||||||
|
|
|
@ -189,6 +189,11 @@ where
|
||||||
///
|
///
|
||||||
/// See [`HandlerExt::enter_dialogue`].
|
/// See [`HandlerExt::enter_dialogue`].
|
||||||
///
|
///
|
||||||
|
/// ## Dependency requirements
|
||||||
|
///
|
||||||
|
/// - `Arc<S>`
|
||||||
|
/// - `Upd`
|
||||||
|
///
|
||||||
/// [`HandlerExt::enter_dialogue`]: super::HandlerExt::enter_dialogue
|
/// [`HandlerExt::enter_dialogue`]: super::HandlerExt::enter_dialogue
|
||||||
pub fn enter<Upd, S, D, Output>() -> Handler<'static, DependencyMap, Output, DpHandlerDescription>
|
pub fn enter<Upd, S, D, Output>() -> Handler<'static, DependencyMap, Output, DpHandlerDescription>
|
||||||
where
|
where
|
||||||
|
|
|
@ -42,7 +42,8 @@ pub trait HandlerExt<Output> {
|
||||||
/// - `Arc<S>`
|
/// - `Arc<S>`
|
||||||
/// - `Upd`
|
/// - `Upd`
|
||||||
///
|
///
|
||||||
/// [`Dialogue<D, S>`]: Dialogue
|
/// [`Dialogue<D, S>`]: super::dialogue::Dialogue
|
||||||
|
/// [`Dialogue::get_or_default`]: super::dialogue::Dialogue::get_or_default
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn enter_dialogue<Upd, S, D>(self) -> Self
|
fn enter_dialogue<Upd, S, D>(self) -> Self
|
||||||
where
|
where
|
||||||
|
@ -67,10 +68,7 @@ where
|
||||||
where
|
where
|
||||||
C: BotCommands + Send + Sync + 'static,
|
C: BotCommands + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
self.chain(dptree::filter_map(move |message: Message, me: Me| {
|
self.chain(filter_command::<C, Output>())
|
||||||
let bot_name = me.user.username.expect("Bots must have a username");
|
|
||||||
message.text().and_then(|text| C::parse(text, bot_name).ok())
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter_dialogue<Upd, S, D>(self) -> Self
|
fn enter_dialogue<Upd, S, D>(self) -> Self
|
||||||
|
@ -91,3 +89,24 @@ where
|
||||||
self.chain(F::handler())
|
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())
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
|
@ -113,6 +113,6 @@ 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::HandlerExt;
|
pub use handler_ext::{filter_command, HandlerExt};
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
pub use handler_factory::HandlerFactory;
|
pub use handler_factory::HandlerFactory;
|
||||||
|
|
|
@ -79,6 +79,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 dptree;
|
pub use dptree;
|
||||||
|
|
||||||
#[cfg(all(feature = "nightly", doctest))]
|
#[cfg(all(feature = "nightly", doctest))]
|
||||||
|
|
Loading…
Reference in a new issue