mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-10-16 14:37:27 +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 fortune;
|
||||||
mod echo;
|
mod echo;
|
||||||
mod help;
|
mod help;
|
||||||
|
mod whoami;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, BotCommands)]
|
#[derive(Debug, Clone, PartialEq, Eq, BotCommands)]
|
||||||
#[command(rename_rule = "lowercase")]
|
#[command(rename_rule = "lowercase")]
|
||||||
|
@ -25,7 +26,9 @@ pub(self) enum Command {
|
||||||
#[command(description = "Mostra il tuo oroscopo di oggi.")]
|
#[command(description = "Mostra il tuo oroscopo di oggi.")]
|
||||||
Fortune,
|
Fortune,
|
||||||
#[command(description = "Ripeti il testo inviato.")]
|
#[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 {
|
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::Fortune => fortune::handler(&bot, &message).await,
|
||||||
Command::Echo(text) => echo::handler(&bot, &message, &text).await,
|
Command::Echo(text) => echo::handler(&bot, &message, &text).await,
|
||||||
|
Command::WhoAmI => whoami::handler(&bot, &message).await,
|
||||||
};
|
};
|
||||||
|
|
||||||
if result.is_ok() {
|
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…
Reference in a new issue