From f53adea01cc4592384bd108bafc325f50fb52bbc Mon Sep 17 00:00:00 2001 From: Phosphorus Date: Sat, 20 Apr 2024 18:37:51 -0300 Subject: [PATCH] feat: add command me #30 --- src/command.rs | 5 ++++- src/commands/me.rs | 27 +++++++++++++++++++++++++++ src/commands/mod.rs | 3 ++- src/models/errors.rs | 3 +++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/commands/me.rs diff --git a/src/command.rs b/src/command.rs index 452072a..15914db 100644 --- a/src/command.rs +++ b/src/command.rs @@ -3,7 +3,7 @@ use std::{error::Error, sync::Arc}; use teloxide::{prelude::*, types::Me, utils::command::BotCommands}; use tracing::error; -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, roadmap::roadmap, sedes::sedes}, 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, me::me, roadmap::roadmap, sedes::sedes}, models::errors::BotErrors}; /// Enumeration of commands accepted by the bot. #[derive(BotCommands, Clone)] @@ -21,6 +21,8 @@ pub enum Command { HacerAlgo, #[command(description = "Muestra el link de la imagen del roadmap de la carrera")] Roadmap, + #[command(description = "Permite usar el /me de IRC")] + Me(String), #[command(description = "Devuelve una lista de links")] Links, #[command(description = "Lista las comunidades IT que tenemos")] @@ -48,6 +50,7 @@ impl Command { let response = match cmd { Command::Help => help(&msg, &bot).await, Command::HacerAlgo => hacer_algo(&msg, &bot).await, + Command::Me(action) => me(&msg, action, &bot).await, Command::Roadmap => roadmap(&msg, &bot).await, Command::Links => links_utiles(&msg, &bot).await, Command::CalendarioAcademico => calendario_academico(&msg, &bot).await, diff --git a/src/commands/me.rs b/src/commands/me.rs new file mode 100644 index 0000000..de98772 --- /dev/null +++ b/src/commands/me.rs @@ -0,0 +1,27 @@ +use teloxide::{requests::Requester, types::{ChatId, Message}, Bot}; +use tracing::info; + +use crate::models::errors::BotErrors; + +pub async fn me(msg: &Message, action: String, bot: &Bot) -> Result<(), BotErrors> { + if action.is_empty() { + return Err(BotErrors::MeCommandBadUsed); + } + + if let Some(user) = msg.from() { + if (user.id.0 as i64).eq(&msg.chat.id.0) { + let username = if let Some(username) = user.username.clone() { + username + } else { + user.full_name() + }; + + // our telegram chat id + bot.send_message(ChatId(-1001217390053), format!("{username} {action}", )).await?; + } + } + + info!(" {}", msg.chat.id); + + Ok(()) +} \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d3f4c7d..b6d7bfd 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -5,4 +5,5 @@ pub mod get_siu_info; pub mod comunidades_it; pub mod mails_de_escuela; pub mod roadmap; -pub mod sedes; \ No newline at end of file +pub mod sedes; +pub mod me; \ No newline at end of file diff --git a/src/models/errors.rs b/src/models/errors.rs index 75980f9..278de4d 100644 --- a/src/models/errors.rs +++ b/src/models/errors.rs @@ -3,6 +3,9 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum BotErrors { + #[error("Deberías de enviar un mensaje al privado seguido de una acción: /me hizo algo")] + MeCommandBadUsed, + #[error("Ha habido un problema de comunicación con Telegram")] TeloxideErrors(#[from] teloxide::RequestError),