Added new handle.

Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
2024-04-04 03:02:57 +02:00
parent 07c6ff69e0
commit 90ac6d77aa
10 changed files with 77 additions and 32 deletions

View File

@ -74,7 +74,7 @@ lazy_static::lazy_static! {
);
static ref CUR_REGEX: Regex = {
#[allow(clippy::clone_double_ref)]
#[allow(suspicious_double_ref_op)]
let a = CONVERTION_ALIASES.keys()
.copied()
.chain(SUPPORTED_CURS.iter().copied())
@ -115,7 +115,9 @@ impl Filter for CurrencyTextFilter {
#[async_trait::async_trait]
impl Handler for CurrencyConverter {
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
let Some(message) = get_message(update) else{ return Ok(())};
let Some(message) = get_message(update) else {
return Ok(());
};
let response = self
.client
.get("https://www.cbr-xml-daily.ru/daily_json.js")
@ -127,7 +129,8 @@ impl Handler for CurrencyConverter {
let Some(valutes) = response
.get("Valute")
.and_then(serde_json::Value::as_object) else{
.and_then(serde_json::Value::as_object)
else {
log::warn!("Can't get valutes fom response.");
return Ok(());
};
@ -142,7 +145,8 @@ impl Handler for CurrencyConverter {
// Convert match to string.
.map(|mtch| mtch.as_str())
// Parse it.
.and_then(|val| val.parse::<f64>().ok()) else{
.and_then(|val| val.parse::<f64>().ok())
else {
continue;
};
let cur_name = capture.name("cur_name").map(|mtch| mtch.as_str());
@ -150,7 +154,8 @@ impl Handler for CurrencyConverter {
// We check if the value is an alias.
.and_then(|val| CONVERTION_ALIASES.get(val).copied())
// get previous value if not.
.or(cur_name) else{
.or(cur_name)
else {
continue;
};
let fingerprint = format!("{num_value:.5} {cur_name}");

View File

@ -0,0 +1,24 @@
use grammers_client::{types::Chat, Client, InputMessage, Update};
use crate::{bot::handlers::Handler, utils::messages::get_message};
#[derive(Clone)]
pub struct GetUserId;
#[async_trait::async_trait]
impl Handler for GetUserId {
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
let message = get_message(update);
let Some(msg) = message else {
return Ok(());
};
let Chat::User(user) = msg.chat() else {
return Ok(());
};
let username = user.username().unwrap_or("Unknown username");
let user_id = user.id();
msg.reply(InputMessage::text(format!("{username}: {user_id}")).silent(true))
.await?;
Ok(())
}
}

View File

@ -8,7 +8,9 @@ pub struct Help;
#[async_trait::async_trait]
impl Handler for Help {
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
let Update::NewMessage(message) = update else {return Ok(())};
let Update::NewMessage(message) = update else {
return Ok(());
};
message.reply("Я больше не рассказываю что я умею.").await?;

View File

@ -1,5 +1,6 @@
pub mod currency_converter;
pub mod get_chat_id;
pub mod get_user_id;
pub mod help;
pub mod notify_all;
pub mod time_converter;

View File

@ -8,7 +8,9 @@ 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 Some(message) = get_message(update) else {
return Ok(());
};
let Chat::Group(group) = message.chat() else {
return Ok(());
};

View File

@ -24,7 +24,7 @@ pub fn convert_time(offsets: &[FixedOffset], times: &[NaiveTime]) -> Vec<String>
let mut replies = Vec::new();
let now = Utc::now();
let Some(main_offset) = offsets.get(0) else {
let Some(main_offset) = offsets.first() else {
return vec![];
};
@ -56,7 +56,9 @@ pub fn convert_time(offsets: &[FixedOffset], times: &[NaiveTime]) -> Vec<String>
#[async_trait::async_trait]
impl Handler for TimeConverter {
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
let Some(message) = get_message(update) else{return Ok(());};
let Some(message) = get_message(update) else {
return Ok(());
};
let mut offsets = Vec::new();
let mut times = Vec::new();
@ -97,7 +99,7 @@ impl Handler for TimeConverter {
if times.is_empty() {
offsets = [FixedOffset::east_opt(0).unwrap()]
.into_iter()
.chain(offsets.into_iter())
.chain(offsets)
.collect::<Vec<_>>();
times.push(Utc::now().time());
}

View File

@ -21,7 +21,9 @@ pub struct Greeter;
#[async_trait]
impl Handler for Greeter {
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
let Update::NewMessage(message) = update else {return Ok(())};
let Update::NewMessage(message) = update else {
return Ok(());
};
// Choose random greeting from the list of greetings.
let reply_text = GREETINGS.iter().choose(&mut rand::rngs::OsRng).copied();

View File

@ -8,7 +8,9 @@ pub struct Repeator;
#[async_trait::async_trait]
impl Handler for Repeator {
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
let Some(message) = get_message(update) else { return Ok(()) };
let Some(message) = get_message(update) else {
return Ok(());
};
message
.respond(InputMessage::from(message.text()).silent(true))
.await?;

View File

@ -19,6 +19,7 @@ use super::{
basic::{
currency_converter::{CurrencyConverter, CurrencyTextFilter},
get_chat_id::GetChatId,
get_user_id::GetUserId,
help::Help,
notify_all::NotifyAll,
time_converter::TimeConverter,
@ -97,6 +98,7 @@ async fn handle_with_log(handler: Box<dyn Handler>, client: Client, update_data:
/// and spawns correcsponding handlers.
///
/// Also, every available handler is defined here.
#[allow(clippy::too_many_lines)]
async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
let me = client.get_me().await?;
let handlers: Vec<FilteredHandler> = vec![
@ -118,6 +120,11 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(TextFilter(&[".cid"], TextMatchMethod::Matches))
.add_filter(OnlyFromId(me.id())),
// Get user id.
FilteredHandler::new(GetUserId)
.add_filter(SilentFilter)
.add_filter(MessageDirectionFilter(MessageDirection::Outgoing))
.add_filter(TextFilter(&[".uid"], TextMatchMethod::Matches)),
// Make бля fun again.
FilteredHandler::new(Blyaficator)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
@ -168,8 +175,6 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
&[".cid"],
TextMatchMethod::StartsWith,
))),
// .add_filter(TextFilter(&[".cid"], TextMatchMethod::)),
// Notify all.
FilteredHandler::new(NotifyAll)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
@ -265,7 +270,7 @@ pub async fn start(args: BotConfig, web_code: Arc<RwLock<Option<String>>>) -> an
Err(err) => {
log::error!("{err}");
}
Ok(_) => {
Ok(()) => {
log::info!("Lol, messages are ended.");
}
}