Skip to content

Commit

Permalink
feat: initial get mails
Browse files Browse the repository at this point in the history
  • Loading branch information
Phosphorus-M committed Mar 7, 2024
1 parent 913f384 commit aefb918
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 6 deletions.
67 changes: 67 additions & 0 deletions assets/mails_escuelas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[
{
"escuela": "Informática",
"mails": [
{
"name": "Escuela",
"mail": "[email protected]"
},
{
"name": "Inscripciones",
"mail": "[email protected]"
}
]
},
{
"escuela": "Ingenieria",
"mails": [
{
"name": "Escuela",
"mail": "[email protected]"
},
{
"name": "Alumnos",
"mail": "[email protected]"
}
]
},
{
"escuela": "Humanidades",
"mails": [
{
"name": "Escuela",
"mail": "[email protected]"
},
{
"name": "Alumnos",
"mail": "[email protected]"
}
]
},
{
"escuela": "Administracion",
"mails": [
{
"name": "Escuela",
"mail": "[email protected]"
},
{
"name": "Alumnos",
"mail": "[email protected]"
}
]
},
{
"escuela": "Enfermería",
"mails": [
{
"name": "Escuela",
"mail": "[email protected]"
},
{
"name": "Alumnos",
"mail": "[email protected]"
}
]
}
]
8 changes: 5 additions & 3 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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 {
Expand All @@ -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(())
Expand Down
44 changes: 44 additions & 0 deletions src/commands/mails_de_escuela.rs
Original file line number Diff line number Diff line change
@@ -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<Escuela> = 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(())
}
3 changes: 2 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
pub mod comunidades_it;
pub mod mails_de_escuela;
2 changes: 1 addition & 1 deletion src/models/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
15 changes: 15 additions & 0 deletions src/models/mails_de_escuelas.rs
Original file line number Diff line number Diff line change
@@ -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<Mail>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Mail {
pub name: String,
pub mail: String,
}
3 changes: 2 additions & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod link;
pub mod errors;
pub mod errors;
pub mod mails_de_escuelas;

0 comments on commit aefb918

Please sign in to comment.