diff --git a/assets/mails_escuelas.json b/assets/mails_escuelas.json new file mode 100644 index 0000000..c10bdb7 --- /dev/null +++ b/assets/mails_escuelas.json @@ -0,0 +1,67 @@ +[ + { + "escuela": "Informática", + "mails": [ + { + "name": "Escuela", + "mail": "escueladeinformatica@uno.edu.ar" + }, + { + "name": "Inscripciones", + "mail": "inscripcionesinformatica@uno.edu.ar" + } + ] + }, + { + "escuela": "Ingenieria", + "mails": [ + { + "name": "Escuela", + "mail": "escingeuno@uno.edu.ar" + }, + { + "name": "Alumnos", + "mail": "infoalumnosingenieria@uno.edu.ar" + } + ] + }, + { + "escuela": "Humanidades", + "mails": [ + { + "name": "Escuela", + "mail": "escueladehumanidades@uno.edu.ar" + }, + { + "name": "Alumnos", + "mail": "alumnos.humanidades@uno.edu.ar" + } + ] + }, + { + "escuela": "Administracion", + "mails": [ + { + "name": "Escuela", + "mail": "escueladeadministracion@uno.edu.ar" + }, + { + "name": "Alumnos", + "mail": "alumnos.administracion@uno.edu.ar" + } + ] + }, + { + "escuela": "Enfermería", + "mails": [ + { + "name": "Escuela", + "mail": "escueladecienciasdelasalud@uno.edu.ar" + }, + { + "name": "Alumnos", + "mail": "alumnos.salud@uno.edu.ar" + } + ] + } + ] \ No newline at end of file diff --git a/src/command.rs b/src/command.rs index c2df1d9..b98a021 100644 --- a/src/command.rs +++ b/src/command.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use teloxide::{prelude::*, types::Me, utils::command::BotCommands}; -use crate::{bot::InstanceState, commands::{calendario_academico::calendario_academico, comunidades_it::comunidades_it, get_siu_info::get_siu_info, hacer_algo::hacer_algo, links_utiles::links_utiles}, models::errors::BotErrors}; +use crate::{bot::InstanceState, commands::{calendario_academico::calendario_academico, comunidades_it::comunidades_it, get_siu_info::get_siu_info, hacer_algo::hacer_algo, links_utiles::links_utiles, mails_de_escuela::get_mails_de_escuela}, models::errors::BotErrors}; /// Enumeration of commands accepted by the bot. #[derive(BotCommands, Clone)] @@ -26,7 +26,8 @@ pub enum Command { CalendarioAcademico, #[command(description = "Obtener estado del SIU")] SIU, - + #[command(description = "Obtener mails de Escuela")] + MailDeEscuela(String), } impl Command { @@ -47,7 +48,8 @@ impl Command { Command::Links => links_utiles(&msg, &bot).await?, Command::CalendarioAcademico => calendario_academico(&msg, &bot).await?, Command::SIU => get_siu_info(&msg, &bot).await?, - Command::ComunidadesIT => comunidades_it(&msg, &bot).await? + Command::ComunidadesIT => comunidades_it(&msg, &bot).await?, + Command::MailDeEscuela(escuela) => get_mails_de_escuela(&msg, &bot, escuela).await? } Ok(()) diff --git a/src/commands/mails_de_escuela.rs b/src/commands/mails_de_escuela.rs new file mode 100644 index 0000000..df93ada --- /dev/null +++ b/src/commands/mails_de_escuela.rs @@ -0,0 +1,44 @@ +use std::fs::read_to_string; + +use teloxide::{requests::{Requester, ResponseResult}, types::Message, utils::html::escape, Bot, RequestError}; + +use crate::models::mails_de_escuelas::Escuela; +use std::fmt::Write; + +pub async fn get_mails_de_escuela(msg: &Message, bot: &Bot, nombre_de_escuela: String) -> ResponseResult<()> { + bot.send_message(msg.chat.id, "Acá tenes los mails").await?; + let content = read_to_string("assets/mails_escuelas.json")?; + let escuelas: Vec = serde_json::from_str(&content).map_err(|e|RequestError::InvalidJson { source: e, raw: content.into_boxed_str() })?; + + if nombre_de_escuela.is_empty() { + let mut message = "Estos son los mails que tenemos:\n".to_string(); + for escuela in escuelas { + let escuela_title = escape(&escuela.escuela); + let _ = writeln!(&mut message, "Escuela de {}", escuela_title); + for mail in escuela.mails { + let name = escape(&mail.name); + let mail = escape(&mail.mail); + let _ = writeln!(&mut message, "{name}: {mail}"); + } + + } + bot.send_message(msg.chat.id, message).await?; + } else{ + let Some(escuela) = escuelas.iter().find(|escuela| escuela.escuela.to_lowercase().contains(&nombre_de_escuela.to_lowercase())) else { + bot.send_message(msg.chat.id, "No se encontró escuela que coincida con el nombre enviado").await?; + return Ok(()) + }; + let mut message = "Estos son los mails que tenemos:\n".to_string(); + + let escuela_title = escape(&escuela.escuela); + let _ = writeln!(&mut message, "Escuela de {}", &escuela_title); + for mail in escuela.mails.iter() { + let name = escape(&mail.name); + let mail = escape(&mail.mail); + let _ = writeln!(&mut message, "{name}: {mail}"); + } + bot.send_message(msg.chat.id, message).await?; + } + + Ok(()) +} \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 1f58ca4..2d81450 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2,4 +2,5 @@ pub mod hacer_algo; pub mod links_utiles; pub mod calendario_academico; pub mod get_siu_info; -pub mod comunidades_it; \ No newline at end of file +pub mod comunidades_it; +pub mod mails_de_escuela; \ No newline at end of file diff --git a/src/models/errors.rs b/src/models/errors.rs index b69e00a..ee4c6e4 100644 --- a/src/models/errors.rs +++ b/src/models/errors.rs @@ -3,7 +3,7 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum BotErrors { - #[error("data store disconnected")] + #[error(transparent)] TeloxideErrors(#[from] teloxide::RequestError), #[error("ocurrio un error haciendo ping")] diff --git a/src/models/mails_de_escuelas.rs b/src/models/mails_de_escuelas.rs new file mode 100644 index 0000000..96dfc88 --- /dev/null +++ b/src/models/mails_de_escuelas.rs @@ -0,0 +1,15 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Escuela { + pub escuela: String, + pub mails: Vec, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Mail { + pub name: String, + pub mail: String, +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 200cc07..2a5cad6 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,2 +1,3 @@ pub mod link; -pub mod errors; \ No newline at end of file +pub mod errors; +pub mod mails_de_escuelas; \ No newline at end of file