diff --git a/src/telegram/commands/mod.rs b/src/telegram/commands/mod.rs index bd5e28a8..45282d56 100644 --- a/src/telegram/commands/mod.rs +++ b/src/telegram/commands/mod.rs @@ -14,6 +14,7 @@ mod start; mod fortune; mod echo; mod help; +mod whoami; #[derive(Debug, Clone, PartialEq, Eq, BotCommands)] #[command(rename_rule = "lowercase")] @@ -25,7 +26,9 @@ pub(self) enum Command { #[command(description = "Mostra il tuo oroscopo di oggi.")] Fortune, #[command(description = "Ripeti il testo inviato.")] - Echo(String) + Echo(String), + #[command(description = "Controlla a che account RYG è associato il tuo account Telegram.")] + WhoAmI, } async fn handle_command(bot: Bot, command: Command, message: Message) -> CommandResult { @@ -39,6 +42,7 @@ async fn handle_command(bot: Bot, command: Command, message: Message) -> Command }, Command::Fortune => fortune::handler(&bot, &message).await, Command::Echo(text) => echo::handler(&bot, &message, &text).await, + Command::WhoAmI => whoami::handler(&bot, &message).await, }; if result.is_ok() { diff --git a/src/telegram/commands/whoami.rs b/src/telegram/commands/whoami.rs new file mode 100644 index 00000000..0be59171 --- /dev/null +++ b/src/telegram/commands/whoami.rs @@ -0,0 +1,48 @@ +use anyhow::Context; +use diesel::{QueryDsl, RunQueryDsl}; +use teloxide::Bot; +use teloxide::payloads::SendMessageSetters; +use teloxide::requests::Requester; +use teloxide::types::{Message}; +use crate::database::models::{RoyalnetUser, TelegramUser}; +use super::{CommandResult}; + +pub async fn handler(bot: &Bot, message: &Message) -> CommandResult { + let author = message.from() + .context("Non è stato possibile determinare chi ha inviato questo comando.")?; + + let mut database = crate::database::connect(). + context("Non è stato possibile connettersi al database RYG.")?; + + let royalnet_user: RoyalnetUser = { + use diesel::prelude::*; + use diesel::{ExpressionMethods, QueryDsl}; + use crate::database::schema::telegram::dsl::*; + use crate::database::schema::users::dsl::*; + use crate::database::models::RoyalnetUser; + + telegram + .filter(telegram_id.eq::( + author.id.0.try_into() + .context("Non è stato possibile processare il tuo ID Telegram per via di un overflow.")? + )) + .inner_join(users) + .select(RoyalnetUser::as_select()) + .get_result(&mut database) + .context("Non è stato possibile recuperare il tuo utente Telegram dal database RYG.")? + }; + + let username = &royalnet_user.username; + + let text = format!( + "👤 Nel database RYG, tu hai l'username «{username}»." + ); + + let _reply = bot + .send_message(message.chat.id, text) + .reply_to_message_id(message.id) + .await + .context("Non è stato possibile inviare la risposta.")?; + + Ok(()) +}