mirror of
https://github.com/RYGhub/royalnet.git
synced 2025-04-17 03:50:30 +00:00
Add /whoami
command as an example of database usage
This commit is contained in:
parent
13e794a919
commit
26a2794be8
2 changed files with 53 additions and 1 deletions
|
@ -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() {
|
||||
|
|
48
src/telegram/commands/whoami.rs
Normal file
48
src/telegram/commands/whoami.rs
Normal file
|
@ -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::<i64>(
|
||||
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(())
|
||||
}
|
Loading…
Add table
Reference in a new issue