diff --git a/crates/teloxide/examples/dispatching_features.rs b/crates/teloxide/examples/dispatching_features.rs index f69dc9de..05c5a98d 100644 --- a/crates/teloxide/examples/dispatching_features.rs +++ b/crates/teloxide/examples/dispatching_features.rs @@ -4,6 +4,7 @@ use rand::Rng; use teloxide::{ + dispatching::HandlerExt, prelude::*, types::{Dice, ReplyParameters}, utils::command::BotCommands, @@ -52,12 +53,31 @@ async fn main() { .branch( // Filtering allow you to filter updates by some condition. dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup()) - // An endpoint is the last update handler. - .endpoint(|msg: Message, bot: Bot| async move { - log::info!("Received a message from a group chat."); - bot.send_message(msg.chat.id, "This is a group chat.").await?; - respond(()) - }), + .branch( + // Filtering by mention allows to filter only `/repeat@my_bot` commands. + // Use if you want to make sure that users refer specifically to your bot. + // Same as filter_command, the next handlers will receive a parsed + // `GroupCommand`. + dptree::entry().filter_mention_command::().endpoint( + |bot: Bot, msg: Message, cmd: GroupCommand| async move { + match cmd { + GroupCommand::Repeat { text } => { + bot.send_message(msg.chat.id, "You said: ".to_owned() + &text) + .await?; + Ok(()) + } + } + }, + ), + ) + .branch( + // An endpoint is the last update handler. + dptree::endpoint(|msg: Message, bot: Bot| async move { + log::info!("Received a message from a group chat."); + bot.send_message(msg.chat.id, "This is a group chat.").await?; + respond(()) + }), + ), ) .branch( // There are some extension filtering functions on `Message`. The following filter will @@ -116,6 +136,14 @@ enum MaintainerCommands { Rand { from: u64, to: u64 }, } +/// Group commands +#[derive(BotCommands, Clone)] +#[command(rename_rule = "lowercase")] +enum GroupCommand { + /// Repeats a message + Repeat { text: String }, +} + async fn simple_commands_handler( cfg: ConfigParameters, bot: Bot, diff --git a/crates/teloxide/src/dispatching/handler_ext.rs b/crates/teloxide/src/dispatching/handler_ext.rs index b1717a80..ecda2406 100644 --- a/crates/teloxide/src/dispatching/handler_ext.rs +++ b/crates/teloxide/src/dispatching/handler_ext.rs @@ -149,6 +149,7 @@ where } #[cfg(test)] +#[cfg(feature = "macros")] mod tests { use crate::{self as teloxide, dispatching::UpdateFilterExt, utils::command::BotCommands}; use chrono::DateTime; @@ -161,7 +162,6 @@ mod tests { use super::HandlerExt; - #[cfg(feature = "macros")] #[derive(BotCommands, Clone)] #[command(rename_rule = "lowercase")] enum Cmd { @@ -255,11 +255,9 @@ mod tests { } #[tokio::test] - #[cfg(feature = "macros")] async fn test_filter_command() { - let h = dptree::entry().branch( - Update::filter_message().filter_command::().endpoint(|| async {}), - ); + let h = dptree::entry() + .branch(Update::filter_message().filter_command::().endpoint(|| async {})); let me = make_me(); let update = make_update("/test@".to_owned() + me.username()); @@ -276,11 +274,9 @@ mod tests { } #[tokio::test] - #[cfg(feature = "macros")] async fn test_filter_mention_command() { - let h = dptree::entry().branch( - Update::filter_message().filter_mention_command::().endpoint(|| async {}), - ); + let h = dptree::entry() + .branch(Update::filter_message().filter_mention_command::().endpoint(|| async {})); let me = make_me(); let update = make_update("/test@".to_owned() + me.username());