Added @all handler for groups

Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
2023-02-26 20:49:03 +04:00
parent 579b1daf02
commit 8b5a588770
4 changed files with 51 additions and 13 deletions

View File

@ -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 {

View File

@ -1,4 +1,5 @@
pub mod currency_converter;
pub mod get_chat_id;
pub mod help;
pub mod notify_all;
pub mod weather_forecaster;

View File

@ -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(())
}
}

View File

@ -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;