diff --git a/Cargo.lock b/Cargo.lock index 3edc286e..bc052f47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1352,7 +1352,7 @@ dependencies = [ "pretty_env_logger", "rand", "regex", - "reqwest 0.12.5", + "reqwest", "serde", "teloxide", "tokio", diff --git a/Cargo.toml b/Cargo.toml index 5da03204..37e6ae76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ parse_datetime = "0.6.0" regex = "1.10.5" once_cell = "1.19.0" reqwest = { version = "0.12.5", features = ["json"] } -serde = { version = "1.0.204", features = ["serde_derive"] } +serde = { version = "1.0.204", features = ["derive"] } [[bin]] name = "royalnet" diff --git a/src/services/telegram/commands/dog.rs b/src/services/telegram/commands/dog.rs new file mode 100644 index 00000000..72404582 --- /dev/null +++ b/src/services/telegram/commands/dog.rs @@ -0,0 +1,38 @@ +use anyhow::Context; +use reqwest::Url; +use teloxide::Bot; +use teloxide::payloads::SendPhotoSetters; +use teloxide::requests::Requester; +use teloxide::types::{InputFile, Message}; +use serde::Deserialize; +use super::{CommandResult}; + + +const DOG_API_URL: &str = "https://dog.ceo/api/breeds/image/random"; + +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +struct DogQueryResponse { + // status: String, + pub message: String, +} + + +pub async fn handler(bot: &Bot, message: &Message) -> CommandResult { + let response = reqwest::get(DOG_API_URL).await + .context("Non è stato possibile richiedere un cane all'API.")? + .json::().await + .context("Il cane ricevuto in risposta dall'API è indecifrabile, quindi non è stato possibile riceverlo.")?; + + let url: Url = response.message.parse() + .context("L'URL del cane ricevuto in risposta dall'API è malformato, quindi non è stato possibile riceverlo.")?; + + let input = InputFile::url(url); + + let _reply = bot + .send_photo(message.chat.id, input) + .reply_to_message_id(message.id) + .await + .context("Non è stato possibile inviare un cane in risposta a questo messaggio.")?; + + Ok(()) +} \ No newline at end of file diff --git a/src/services/telegram/commands/mod.rs b/src/services/telegram/commands/mod.rs index 95e6f635..836632c9 100644 --- a/src/services/telegram/commands/mod.rs +++ b/src/services/telegram/commands/mod.rs @@ -15,6 +15,7 @@ mod help; mod whoami; mod answer; mod reminder; +mod dog; mod cat; #[derive(Debug, Clone, PartialEq, Eq, BotCommands)] @@ -34,6 +35,8 @@ pub enum Command { Answer(String), #[command(description = "Ricorda la chat di qualcosa che avverrà in futuro. Non persiste ai riavvii del bot.")] Reminder(reminder::ReminderArgs), + #[command(description = "Invia un cane casuale in chat.")] + Dog, #[command(description = "Invia un gatto casuale in chat.")] Cat, } @@ -66,6 +69,7 @@ impl Command { Command::WhoAmI => whoami::handler(&bot, &message).await, Command::Answer(_) => answer::handler(&bot, &message).await, Command::Reminder(args) => reminder::handler(&bot, &message, args).await, + Command::Dog => dog::handler(&bot, &message).await, Command::Cat => cat::handler(&bot, &message).await, };