Added @all handler for groups
Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
@ -14,8 +14,9 @@ pub enum MessageDirection {
|
|||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
pub enum TextMatchMethod {
|
pub enum TextMatchMethod {
|
||||||
IStartsWith,
|
StartsWith,
|
||||||
IMatches,
|
Matches,
|
||||||
|
Contains,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -86,10 +87,13 @@ impl<'a> Filter for TextFilter<'a> {
|
|||||||
};
|
};
|
||||||
for text in self.0 {
|
for text in self.0 {
|
||||||
let matches = match self.1 {
|
let matches = match self.1 {
|
||||||
TextMatchMethod::IStartsWith => message_text
|
TextMatchMethod::StartsWith => message_text
|
||||||
.to_lowercase()
|
.to_lowercase()
|
||||||
.starts_with(text.to_lowercase().as_str()),
|
.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 {
|
if matches {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
pub mod currency_converter;
|
pub mod currency_converter;
|
||||||
pub mod get_chat_id;
|
pub mod get_chat_id;
|
||||||
pub mod help;
|
pub mod help;
|
||||||
|
pub mod notify_all;
|
||||||
pub mod weather_forecaster;
|
pub mod weather_forecaster;
|
||||||
|
28
src/bot/handlers/basic/notify_all.rs
Normal file
28
src/bot/handlers/basic/notify_all.rs
Normal 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(())
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,7 @@ use super::{
|
|||||||
currency_converter::{CurrencyConverter, CurrencyTextFilter},
|
currency_converter::{CurrencyConverter, CurrencyTextFilter},
|
||||||
get_chat_id::GetChatId,
|
get_chat_id::GetChatId,
|
||||||
help::Help,
|
help::Help,
|
||||||
|
notify_all::NotifyAll,
|
||||||
weather_forecaster::WeatherForecaster,
|
weather_forecaster::WeatherForecaster,
|
||||||
},
|
},
|
||||||
fun::{
|
fun::{
|
||||||
@ -99,24 +100,23 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
|
|||||||
let me = client.get_me().await?;
|
let me = client.get_me().await?;
|
||||||
let handlers: Vec<FilteredHandler> = vec![
|
let handlers: Vec<FilteredHandler> = vec![
|
||||||
// Printing help.
|
// Printing help.
|
||||||
FilteredHandler::new(Help).add_filter(TextFilter(&[".h"], TextMatchMethod::IMatches)),
|
FilteredHandler::new(Help).add_filter(TextFilter(&[".h"], TextMatchMethod::Matches)),
|
||||||
// Greeting my fellow humans.
|
// Greeting my fellow humans.
|
||||||
FilteredHandler::new(Greeter)
|
FilteredHandler::new(Greeter)
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(ExcludedChatsFilter(vec![me.id()]))
|
.add_filter(ExcludedChatsFilter(vec![me.id()]))
|
||||||
.add_filter(TextFilter(&["привет"], TextMatchMethod::IStartsWith))
|
.add_filter(TextFilter(&["привет"], TextMatchMethod::StartsWith))
|
||||||
.add_filter(ExcludedChatsFilter(args.excluded_chats))
|
.add_filter(ExcludedChatsFilter(args.excluded_chats))
|
||||||
.add_middleware::<MembersCount<100>>(),
|
.add_middleware::<MembersCount<100>>(),
|
||||||
// Getting chat id.
|
// Getting chat id.
|
||||||
FilteredHandler::new(GetChatId)
|
FilteredHandler::new(GetChatId).add_filter(TextFilter(&[".cid"], TextMatchMethod::Matches)),
|
||||||
.add_filter(TextFilter(&[".cid"], TextMatchMethod::IMatches)),
|
|
||||||
// Make бля fun again.
|
// Make бля fun again.
|
||||||
FilteredHandler::new(Blyaficator)
|
FilteredHandler::new(Blyaficator)
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".bl"], TextMatchMethod::IStartsWith)),
|
.add_filter(TextFilter(&[".bl"], TextMatchMethod::StartsWith)),
|
||||||
// Handler for converting currecies.
|
// Handler for converting currecies.
|
||||||
FilteredHandler::new(CurrencyConverter::new()?)
|
FilteredHandler::new(CurrencyConverter::new()?)
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
@ -127,12 +127,12 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
|
|||||||
FilteredHandler::new(Rotator)
|
FilteredHandler::new(Rotator)
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".rl"], TextMatchMethod::IStartsWith)),
|
.add_filter(TextFilter(&[".rl"], TextMatchMethod::StartsWith)),
|
||||||
// Weather forecast.
|
// Weather forecast.
|
||||||
FilteredHandler::new(WeatherForecaster::new()?)
|
FilteredHandler::new(WeatherForecaster::new()?)
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".w"], TextMatchMethod::IStartsWith)),
|
.add_filter(TextFilter(&[".w"], TextMatchMethod::StartsWith)),
|
||||||
// Smiley repeator.
|
// Smiley repeator.
|
||||||
FilteredHandler::new(Repeator)
|
FilteredHandler::new(Repeator)
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
@ -145,12 +145,17 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
|
|||||||
FilteredHandler::new(MagicBall)
|
FilteredHandler::new(MagicBall)
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".mb"], TextMatchMethod::IStartsWith)),
|
.add_filter(TextFilter(&[".mb"], TextMatchMethod::StartsWith)),
|
||||||
// Random chooser.
|
// Random chooser.
|
||||||
FilteredHandler::new(Chooser)
|
FilteredHandler::new(Chooser)
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.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;
|
let mut errors_count = 0;
|
||||||
|
Reference in New Issue
Block a user