diff --git a/src/bot/filters/message_fitlers.rs b/src/bot/filters/message_fitlers.rs index e947d4e4758d524b5ad4a7371e8c5025229b1dc6..f60aac48c486d2e444c5bf436c85d545a1d40a52 100644 --- a/src/bot/filters/message_fitlers.rs +++ b/src/bot/filters/message_fitlers.rs @@ -14,8 +14,9 @@ pub enum MessageDirection { #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum TextMatchMethod { - IStartsWith, - IMatches, + StartsWith, + Matches, + Contains, } #[allow(dead_code)] @@ -86,10 +87,13 @@ impl<'a> Filter for TextFilter<'a> { }; for text in self.0 { let matches = match self.1 { - TextMatchMethod::IStartsWith => message_text + TextMatchMethod::StartsWith => message_text .to_lowercase() .starts_with(text.to_lowercase().as_str()), - TextMatchMethod::IMatches => message_text.to_lowercase() == text.to_lowercase(), + TextMatchMethod::Matches => message_text.to_lowercase() == text.to_lowercase(), + TextMatchMethod::Contains => message_text + .to_lowercase() + .contains(text.to_lowercase().as_str()), }; if matches { diff --git a/src/bot/handlers/basic/mod.rs b/src/bot/handlers/basic/mod.rs index acc4306c2c07cd0186f2ccdb38ce2a1b09f9ecde..c3bfd1aa86d0d8f55d3ecef16fabad86689ac595 100644 --- a/src/bot/handlers/basic/mod.rs +++ b/src/bot/handlers/basic/mod.rs @@ -1,4 +1,5 @@ pub mod currency_converter; pub mod get_chat_id; pub mod help; +pub mod notify_all; pub mod weather_forecaster; diff --git a/src/bot/handlers/basic/notify_all.rs b/src/bot/handlers/basic/notify_all.rs new file mode 100644 index 0000000000000000000000000000000000000000..11555910db127d9783397b5dd2688723271882b2 --- /dev/null +++ b/src/bot/handlers/basic/notify_all.rs @@ -0,0 +1,28 @@ +use grammers_client::{types::Chat, Client, Update}; + +use crate::{bot::handlers::Handler, utils::messages::get_message}; + +#[derive(Clone)] +pub struct NotifyAll; + +#[async_trait::async_trait] +impl Handler for NotifyAll { + async fn react(&self, client: &Client, update: &Update) -> anyhow::Result<()> { + let Some(message) = get_message(update) else {return Ok(());}; + let Chat::Group(group) = message.chat() else { + return Ok(()); + }; + let mut participats_iter = client.iter_participants(group); + let mut response = String::new(); + while let Some(participant) = participats_iter.next().await? { + if participant.user.is_bot() { + continue; + } + if let Some(username) = participant.user.username() { + response.push_str(format!("@{username} ").as_str()); + } + } + message.reply(response).await?; + Ok(()) + } +} diff --git a/src/bot/main.rs b/src/bot/main.rs index 0182b64997c5300c503bc299ebe6bf361310ee2a..f1762681c55e0febef044da1ba9e344f6da1b1df 100644 --- a/src/bot/main.rs +++ b/src/bot/main.rs @@ -20,6 +20,7 @@ use super::{ currency_converter::{CurrencyConverter, CurrencyTextFilter}, get_chat_id::GetChatId, help::Help, + notify_all::NotifyAll, weather_forecaster::WeatherForecaster, }, fun::{ @@ -99,24 +100,23 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> { let me = client.get_me().await?; let handlers: Vec<FilteredHandler> = vec![ // Printing help. - FilteredHandler::new(Help).add_filter(TextFilter(&[".h"], TextMatchMethod::IMatches)), + FilteredHandler::new(Help).add_filter(TextFilter(&[".h"], TextMatchMethod::Matches)), // Greeting my fellow humans. FilteredHandler::new(Greeter) .add_filter(UpdateTypeFilter(&[UpdateType::New])) .add_filter(MessageDirectionFilter(MessageDirection::Incoming)) .add_filter(SilentFilter) .add_filter(ExcludedChatsFilter(vec![me.id()])) - .add_filter(TextFilter(&["привет"], TextMatchMethod::IStartsWith)) + .add_filter(TextFilter(&["привет"], TextMatchMethod::StartsWith)) .add_filter(ExcludedChatsFilter(args.excluded_chats)) .add_middleware::<MembersCount<100>>(), // Getting chat id. - FilteredHandler::new(GetChatId) - .add_filter(TextFilter(&[".cid"], TextMatchMethod::IMatches)), + FilteredHandler::new(GetChatId).add_filter(TextFilter(&[".cid"], TextMatchMethod::Matches)), // Make Ð±Ð»Ñ fun again. FilteredHandler::new(Blyaficator) .add_filter(UpdateTypeFilter(&[UpdateType::New])) .add_filter(SilentFilter) - .add_filter(TextFilter(&[".bl"], TextMatchMethod::IStartsWith)), + .add_filter(TextFilter(&[".bl"], TextMatchMethod::StartsWith)), // Handler for converting currecies. FilteredHandler::new(CurrencyConverter::new()?) .add_filter(UpdateTypeFilter(&[UpdateType::New])) @@ -127,12 +127,12 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> { FilteredHandler::new(Rotator) .add_filter(UpdateTypeFilter(&[UpdateType::New])) .add_filter(SilentFilter) - .add_filter(TextFilter(&[".rl"], TextMatchMethod::IStartsWith)), + .add_filter(TextFilter(&[".rl"], TextMatchMethod::StartsWith)), // Weather forecast. FilteredHandler::new(WeatherForecaster::new()?) .add_filter(UpdateTypeFilter(&[UpdateType::New])) .add_filter(SilentFilter) - .add_filter(TextFilter(&[".w"], TextMatchMethod::IStartsWith)), + .add_filter(TextFilter(&[".w"], TextMatchMethod::StartsWith)), // Smiley repeator. FilteredHandler::new(Repeator) .add_filter(UpdateTypeFilter(&[UpdateType::New])) @@ -145,12 +145,17 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> { FilteredHandler::new(MagicBall) .add_filter(UpdateTypeFilter(&[UpdateType::New])) .add_filter(SilentFilter) - .add_filter(TextFilter(&[".mb"], TextMatchMethod::IStartsWith)), + .add_filter(TextFilter(&[".mb"], TextMatchMethod::StartsWith)), // Random chooser. FilteredHandler::new(Chooser) .add_filter(UpdateTypeFilter(&[UpdateType::New])) .add_filter(SilentFilter) - .add_filter(TextFilter(&[".c"], TextMatchMethod::IStartsWith)), + .add_filter(TextFilter(&[".c"], TextMatchMethod::StartsWith)), + // Notify all. + FilteredHandler::new(NotifyAll) + .add_filter(UpdateTypeFilter(&[UpdateType::New])) + .add_filter(SilentFilter) + .add_filter(TextFilter(&["@all"], TextMatchMethod::Contains)), ]; let mut errors_count = 0;