From b59374e1a8232ac3ff71a323583c7ed3f2c30329 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 11 Jul 2024 08:55:14 +0200 Subject: [PATCH] Add start notification for `services::telegram::BotService` --- src/services/telegram/commands/start.rs | 7 +++-- src/services/telegram/config.rs | 16 ++++++++++++ src/services/telegram/mod.rs | 34 ++++++++++++++++++++++--- src/utils/mod.rs | 1 + src/utils/version.rs | 3 +++ 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/utils/version.rs diff --git a/src/services/telegram/commands/start.rs b/src/services/telegram/commands/start.rs index 8a221925..cbb75d0a 100644 --- a/src/services/telegram/commands/start.rs +++ b/src/services/telegram/commands/start.rs @@ -6,9 +6,6 @@ use teloxide::types::{Message}; use super::{CommandResult}; -const VERSION: &str = env!("CARGO_PKG_VERSION"); - - pub async fn handler(bot: &Bot, message: &Message) -> CommandResult { let author = message.from() .context("Non è stato possibile determinare chi ha inviato questo comando.")?; @@ -30,9 +27,11 @@ pub async fn handler(bot: &Bot, message: &Message) -> CommandResult { let me_username = me.username.as_ref() .context("Non è stato possibile determinare l'username del bot.")?; + let version = crate::utils::version::VERSION; + let text = format!( "👋 Ciao {author_username}! Sono @{me_username}, il robot tuttofare della RYG!\n\n\ - Sto eseguendo la versione {VERSION}.\n\n\ + Sto eseguendo la versione {version}.\n\n\ Puoi vedere l'elenco delle mie funzionalità dal menu in basso.\n\n\ Cosa posso fare per te oggi?", ); diff --git a/src/services/telegram/config.rs b/src/services/telegram/config.rs index d45f0407..cafe472a 100644 --- a/src/services/telegram/config.rs +++ b/src/services/telegram/config.rs @@ -1,5 +1,21 @@ use micronfig::config; +// Everything ok, RustRover? config! { TELEGRAM_BOT_TOKEN, + TELEGRAM_NOTIFICATION_CHATID?: String > i64 -> ChatIdConversionHack -> teloxide::types::ChatId, +} + +struct ChatIdConversionHack(i64); + +impl From for ChatIdConversionHack { + fn from(value: i64) -> Self { + Self(value) + } +} + +impl From for teloxide::types::ChatId { + fn from(value: ChatIdConversionHack) -> Self { + Self(value.0) + } } diff --git a/src/services/telegram/mod.rs b/src/services/telegram/mod.rs index 5d6d5e28..0542575c 100644 --- a/src/services/telegram/mod.rs +++ b/src/services/telegram/mod.rs @@ -1,8 +1,10 @@ use std::convert::Infallible; use teloxide::Bot; +use anyhow::{Context, Result}; +use teloxide::requests::Requester; use super::RoyalnetService; -mod config; +pub(self) mod config; mod commands; pub struct BotService { @@ -15,14 +17,40 @@ impl BotService { bot: Bot::new(config::TELEGRAM_BOT_TOKEN()) } } + + async fn send_start_notification(&mut self) -> Result<()> { + let chat_id = config::TELEGRAM_NOTIFICATION_CHATID() + .context("Variabile d'ambiente TELEGRAM_NOTIFICATION_CHATID mancante.")?; + + let text = format!( + "💠 Servizio Telegram avviato\n\ + Royalnet v{}", + crate::utils::version::VERSION, + ); + + self.bot.send_message(chat_id, text) + .await + .context("Invio della notifica di avvio non riuscito.")?; + + Ok(()) + } } impl RoyalnetService for BotService { - async fn run(mut self) -> anyhow::Result { + async fn run(mut self) -> Result { log::info!("Starting Telegram service..."); log::debug!("Setting bot commands..."); - commands::Command::set_commands(&mut self.bot).await?; + match commands::Command::set_commands(&mut self.bot).await { + Err(e) => log::warn!("Failed to set bot commands: {e}"), + _ => log::trace!("Bot commands set successfully!"), + } + + log::debug!("Sending start notification..."); + match self.send_start_notification().await { + Err(e) => log::warn!("Failed to send start notification: {e}"), + _ => log::trace!("Start notification sent successfully!"), + } log::debug!("Starting Telegram dispatcher..."); commands::dispatcher(self.bot).dispatch().await; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 077885d7..bf2d0478 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +1,2 @@ pub mod time; +pub mod version; diff --git a/src/utils/version.rs b/src/utils/version.rs new file mode 100644 index 00000000..e017aefe --- /dev/null +++ b/src/utils/version.rs @@ -0,0 +1,3 @@ +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); + +