1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-22 11:04:21 +00:00

Add start notification for services::telegram::BotService

This commit is contained in:
Steffo 2024-07-11 08:55:14 +02:00
parent d4b270b7fa
commit b59374e1a8
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
5 changed files with 54 additions and 7 deletions

View file

@ -6,9 +6,6 @@ use teloxide::types::{Message};
use super::{CommandResult}; use super::{CommandResult};
const VERSION: &str = env!("CARGO_PKG_VERSION");
pub async fn handler(bot: &Bot, message: &Message) -> CommandResult { pub async fn handler(bot: &Bot, message: &Message) -> CommandResult {
let author = message.from() let author = message.from()
.context("Non è stato possibile determinare chi ha inviato questo comando.")?; .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() let me_username = me.username.as_ref()
.context("Non è stato possibile determinare l'username del bot.")?; .context("Non è stato possibile determinare l'username del bot.")?;
let version = crate::utils::version::VERSION;
let text = format!( let text = format!(
"👋 Ciao {author_username}! Sono @{me_username}, il robot tuttofare della RYG!\n\n\ "👋 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\ Puoi vedere l'elenco delle mie funzionalità dal menu in basso.\n\n\
Cosa posso fare per te oggi?", Cosa posso fare per te oggi?",
); );

View file

@ -1,5 +1,21 @@
use micronfig::config; use micronfig::config;
// Everything ok, RustRover?
config! { config! {
TELEGRAM_BOT_TOKEN, TELEGRAM_BOT_TOKEN,
TELEGRAM_NOTIFICATION_CHATID?: String > i64 -> ChatIdConversionHack -> teloxide::types::ChatId,
}
struct ChatIdConversionHack(i64);
impl From<i64> for ChatIdConversionHack {
fn from(value: i64) -> Self {
Self(value)
}
}
impl From<ChatIdConversionHack> for teloxide::types::ChatId {
fn from(value: ChatIdConversionHack) -> Self {
Self(value.0)
}
} }

View file

@ -1,8 +1,10 @@
use std::convert::Infallible; use std::convert::Infallible;
use teloxide::Bot; use teloxide::Bot;
use anyhow::{Context, Result};
use teloxide::requests::Requester;
use super::RoyalnetService; use super::RoyalnetService;
mod config; pub(self) mod config;
mod commands; mod commands;
pub struct BotService { pub struct BotService {
@ -15,14 +17,40 @@ impl BotService {
bot: Bot::new(config::TELEGRAM_BOT_TOKEN()) 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 { impl RoyalnetService for BotService {
async fn run(mut self) -> anyhow::Result<Infallible> { async fn run(mut self) -> Result<Infallible> {
log::info!("Starting Telegram service..."); log::info!("Starting Telegram service...");
log::debug!("Setting bot commands..."); 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..."); log::debug!("Starting Telegram dispatcher...");
commands::dispatcher(self.bot).dispatch().await; commands::dispatcher(self.bot).dispatch().await;

View file

@ -1 +1,2 @@
pub mod time; pub mod time;
pub mod version;

3
src/utils/version.rs Normal file
View file

@ -0,0 +1,3 @@
pub const VERSION: &str = env!("CARGO_PKG_VERSION");