From 2db26854c9235f94c33d5a8ab3d5ee2b7c278ed8 Mon Sep 17 00:00:00 2001 From: Cookie-CHR Date: Tue, 9 Jul 2024 23:24:52 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Prima=20versione=20com?= =?UTF-8?q?ando=20Answer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/telegram/commands/answer.rs | 124 ++++++++++++++++++++++++++++++++ src/telegram/commands/mod.rs | 4 ++ 2 files changed, 128 insertions(+) create mode 100644 src/telegram/commands/answer.rs diff --git a/src/telegram/commands/answer.rs b/src/telegram/commands/answer.rs new file mode 100644 index 00000000..86ef9234 --- /dev/null +++ b/src/telegram/commands/answer.rs @@ -0,0 +1,124 @@ +use std::hash::{Hash, Hasher}; +use anyhow::{Context}; +use chrono::Datelike; +use rand::SeedableRng; +use rand::seq::SliceRandom; +use teloxide::Bot; +use teloxide::payloads::SendMessageSetters; +use teloxide::prelude::{Message, Requester}; +use crate::telegram::commands::{CommandResult}; + +// Cerchiamo di tenere bilanciate le tre colonne, o almeno le prime due. +// Se avete un'idea ma metterebbe troppe opzioni in un'unica categoria, mettetela sotto commento. +const ANSWERS: [&str; 60] = [ + // risposte "sΓ¬": 20 + "πŸ”΅ SΓ¬.", + "πŸ”΅ Decisamente sΓ¬!", + "πŸ”΅ Uhm, secondo me sΓ¬.", + "πŸ”΅ SΓ¬! SΓ¬! SÌ!", + "πŸ”΅ Yup.", + "πŸ”΅ Direi proprio di sΓ¬.", + "πŸ”΅ Assolutamente sΓ¬.", + "πŸ”΅ Ma certo!", + "πŸ”΅ Esatto!", + "πŸ”΅ Senz'altro!", + "πŸ”΅ Ovviamente.", + "πŸ”΅ Questa domanda ha risposta affermativa.", + "πŸ”΅ Hell yeah.", + "πŸ”΅ YES! YES! YES!", + "πŸ”΅ yusssssss", + "πŸ”΅ Non vedo perchΓ¨ no", + "πŸ”΅ Ha senso, ha perfettamente senso, nulla da obiettare, ha senso.", + "πŸ”΅ Yos!", + "πŸ”΅ SΓ¬, ma tienilo segreto...", + "πŸ”΅ [RADIO] Affermativo.", + + // risposte "no": 20 + "❌ No.", + "❌ Decisamente no!", + "❌ Uhm, secondo me sΓ¬. No, aspetta, ci ho ripensato. È un no.", + "❌ No, no, e ancora NO!", + "❌ Nope.", + "❌ Direi proprio di no.", + "❌ Assolutamente no.", + "❌ Certo che no!", + "❌ Neanche per idea!", + "❌ Neanche per sogno!", + "❌ Niente affatto!", + "❌ Questa domanda ha risposta negativa.", + "❌ Hell no.", + "❌ NO! NO! NO!", + "❌ lolno", + "❌ NEIN NEIN NEIN NEIN", + "❌ Delet dis", + "❌ Nopety nope!", + "❌ No, ma tienilo segreto.", + "❌ [RADIO] Negativo.", + + // risposte "boh": 20 + "❔ Boh.", + "❔ E io che ne so?!", + "❔ Non so proprio rispondere.", + "❔ Non lo so...", + "❔ Mi avvalgo della facoltΓ  di non rispondere.", + "❔ Non parlerΓ² senza il mio avvocato!", + "❔ Dunno.", + "❔ PerchΓ© lo chiedi a me?", + "❔ Ah, non lo so io!", + r"❔ Β―\_(ツ)_/Β―", + "❔ No idea.", + "❔ Dunno.", + "❔ Boooooh!", + "❔ Non ne ho la piΓΉ pallida idea.", + "❔ No comment.", + "❔ maibi", + "❔ maibi not", + "❔ idk dude", + "❔ Non mi Γ¨ permesso condividere questa informazione.", + "❔ [RADIO] Mantengo la posizione.", +]; + +struct FortuneKey { + now: chrono::NaiveDate, +} + +impl Hash for FortuneKey { + fn hash(&self, state: &mut H) { + let now: i32 = self.today.num_days_from_ce(); + + state.write_i32(now); + } +} + +pub async fn handler(bot: &Bot, message: &Message) -> CommandResult { + let now = chrono::Local::now().date_naive(); + + let key = FortuneKey {now}; + + let mut hasher = std::hash::DefaultHasher::new(); + key.hash(&mut hasher); + let hash = hasher.finish() + .to_le_bytes() + .into_iter() + .cycle() + .take(32) + .collect::>() + .try_into(); + if hash.is_err() { + anyhow::bail!("Non Γ¨ stato possibile determinare una risposta."); + } + let hash = hash.unwrap(); + + let mut rng = rand::rngs::SmallRng::from_seed(hash); + + let answer = ANSWERS.choose(&mut rng) + .context("Non Γ¨ stato possibile selezionare una risposta.")?; + + let _reply = bot + .send_message(message.chat.id, answer.to_string()) + .reply_to_message_id(message.id) + .await + .context("Non Γ¨ stato possibile inviare la risposta.")?; + + Ok(()) +} \ No newline at end of file diff --git a/src/telegram/commands/mod.rs b/src/telegram/commands/mod.rs index 45282d56..c84cf703 100644 --- a/src/telegram/commands/mod.rs +++ b/src/telegram/commands/mod.rs @@ -15,6 +15,7 @@ mod fortune; mod echo; mod help; mod whoami; +mod answer; #[derive(Debug, Clone, PartialEq, Eq, BotCommands)] #[command(rename_rule = "lowercase")] @@ -29,6 +30,8 @@ pub(self) enum Command { Echo(String), #[command(description = "Controlla a che account RYG Γ¨ associato il tuo account Telegram.")] WhoAmI, + #[command(description = "Rispondi ad una domanda.")] + Answer(String), } async fn handle_command(bot: Bot, command: Command, message: Message) -> CommandResult { @@ -43,6 +46,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, + Command::Answer(question) => answer::handler(&bot, &message).await, }; if result.is_ok() { From 0d14f25b98eaccd7c6a7a7df22afa47fd2b07097 Mon Sep 17 00:00:00 2001 From: Cookie-CHR Date: Wed, 10 Jul 2024 00:06:44 +0200 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=90=9B=20Made=20it=20actually=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/telegram/commands/answer.rs | 16 ++++++++-------- src/telegram/commands/mod.rs | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/telegram/commands/answer.rs b/src/telegram/commands/answer.rs index 86ef9234..ee329873 100644 --- a/src/telegram/commands/answer.rs +++ b/src/telegram/commands/answer.rs @@ -1,6 +1,6 @@ use std::hash::{Hash, Hasher}; use anyhow::{Context}; -use chrono::Datelike; +use chrono::{DateTime, Utc}; use rand::SeedableRng; use rand::seq::SliceRandom; use teloxide::Bot; @@ -78,22 +78,22 @@ const ANSWERS: [&str; 60] = [ "❔ [RADIO] Mantengo la posizione.", ]; -struct FortuneKey { - now: chrono::NaiveDate, +struct AnswerKey { + seed: chrono::DateTime, } -impl Hash for FortuneKey { +impl Hash for AnswerKey { fn hash(&self, state: &mut H) { - let now: i32 = self.today.num_days_from_ce(); + let seed: i64 = self.seed.timestamp(); - state.write_i32(now); + state.write_i64(seed); } } pub async fn handler(bot: &Bot, message: &Message) -> CommandResult { - let now = chrono::Local::now().date_naive(); + let seed = chrono::Utc::now(); - let key = FortuneKey {now}; + let key = AnswerKey {seed}; let mut hasher = std::hash::DefaultHasher::new(); key.hash(&mut hasher); diff --git a/src/telegram/commands/mod.rs b/src/telegram/commands/mod.rs index c84cf703..e05c3a2d 100644 --- a/src/telegram/commands/mod.rs +++ b/src/telegram/commands/mod.rs @@ -46,7 +46,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, - Command::Answer(question) => answer::handler(&bot, &message).await, + Command::Answer(_question) => answer::handler(&bot, &message).await, }; if result.is_ok() { From af66ff66c058078d53c719a1c070450725699011 Mon Sep 17 00:00:00 2001 From: Cookie <58516648+Cookie-CHR@users.noreply.github.com> Date: Wed, 10 Jul 2024 23:27:45 +0200 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=97=91=20Tolto=20nome=20della=20varia?= =?UTF-8?q?nt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefano Pigozzi --- src/telegram/commands/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/telegram/commands/mod.rs b/src/telegram/commands/mod.rs index e05c3a2d..27606ed9 100644 --- a/src/telegram/commands/mod.rs +++ b/src/telegram/commands/mod.rs @@ -46,7 +46,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, - Command::Answer(_question) => answer::handler(&bot, &message).await, + Command::Answer(_) => answer::handler(&bot, &message).await, }; if result.is_ok() { From 4859e72929229e46664a698264f6aa9ba9c5ddce Mon Sep 17 00:00:00 2001 From: Cookie-CHR Date: Wed, 10 Jul 2024 23:33:04 +0200 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=97=91=20No=20need=20for=20seed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/telegram/commands/answer.rs | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/src/telegram/commands/answer.rs b/src/telegram/commands/answer.rs index ee329873..01f35aac 100644 --- a/src/telegram/commands/answer.rs +++ b/src/telegram/commands/answer.rs @@ -1,6 +1,5 @@ use std::hash::{Hash, Hasher}; use anyhow::{Context}; -use chrono::{DateTime, Utc}; use rand::SeedableRng; use rand::seq::SliceRandom; use teloxide::Bot; @@ -78,38 +77,8 @@ const ANSWERS: [&str; 60] = [ "❔ [RADIO] Mantengo la posizione.", ]; -struct AnswerKey { - seed: chrono::DateTime, -} - -impl Hash for AnswerKey { - fn hash(&self, state: &mut H) { - let seed: i64 = self.seed.timestamp(); - - state.write_i64(seed); - } -} - pub async fn handler(bot: &Bot, message: &Message) -> CommandResult { - let seed = chrono::Utc::now(); - - let key = AnswerKey {seed}; - - let mut hasher = std::hash::DefaultHasher::new(); - key.hash(&mut hasher); - let hash = hasher.finish() - .to_le_bytes() - .into_iter() - .cycle() - .take(32) - .collect::>() - .try_into(); - if hash.is_err() { - anyhow::bail!("Non Γ¨ stato possibile determinare una risposta."); - } - let hash = hash.unwrap(); - - let mut rng = rand::rngs::SmallRng::from_seed(hash); + let mut rng = rand::rngs::SmallRng::from_entropy(); let answer = ANSWERS.choose(&mut rng) .context("Non Γ¨ stato possibile selezionare una risposta.")?; From c040b8abfdcfc04ce1dbd214886f2b6fd07a03ec Mon Sep 17 00:00:00 2001 From: Cookie <58516648+Cookie-CHR@users.noreply.github.com> Date: Wed, 10 Jul 2024 23:34:23 +0200 Subject: [PATCH 5/5] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Raw=20string=20syntax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefano Pigozzi --- src/telegram/commands/answer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/telegram/commands/answer.rs b/src/telegram/commands/answer.rs index ee329873..86efc7a9 100644 --- a/src/telegram/commands/answer.rs +++ b/src/telegram/commands/answer.rs @@ -65,7 +65,7 @@ const ANSWERS: [&str; 60] = [ "❔ Dunno.", "❔ PerchΓ© lo chiedi a me?", "❔ Ah, non lo so io!", - r"❔ Β―\_(ツ)_/Β―", + r#"❔ Β―\_(ツ)_/Β―"#, "❔ No idea.", "❔ Dunno.", "❔ Boooooh!",