1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-25 12:34:19 +00:00

Refactor services again to use traits and to allow bot to update its own command list

This commit is contained in:
Steffo 2024-07-11 08:24:29 +02:00
parent 7a8d30e1d0
commit 05592396e7
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
4 changed files with 45 additions and 23 deletions

View file

@ -1,5 +1,4 @@
use anyhow::Result; use anyhow::Result;
use services::telegram;
use crate::services::RoyalnetService; use crate::services::RoyalnetService;
pub(crate) mod database; pub(crate) mod database;
@ -15,12 +14,12 @@ async fn main() -> Result<()> {
// Telegram setup // Telegram setup
log::trace!("Setting up Telegram bot service..."); log::trace!("Setting up Telegram bot service...");
let telegram = telegram::init(); let telegram = services::telegram::BotService::from_config();
// Run all services concurrently // Run all services concurrently
log::info!("Starting services..."); log::info!("Starting services...");
let result = tokio::try_join![ let result = tokio::try_join![
telegram.run_royalnet(), telegram.run(),
]; ];
// This should never happen, but just in case... // This should never happen, but just in case...

View file

@ -4,15 +4,5 @@ use anyhow::Result;
pub mod telegram; pub mod telegram;
pub trait RoyalnetService { pub trait RoyalnetService {
async fn run_royalnet(self) -> Result<Infallible>; async fn run(self) -> Result<Infallible>;
}
impl RoyalnetService for teloxide::dispatching::Dispatcher<teloxide::Bot, anyhow::Error, teloxide::dispatching::DefaultKey> {
async fn run_royalnet(mut self) -> Result<Infallible> {
log::info!("Starting Telegram service...");
self.dispatch().await;
log::error!("Telegram dispatcher has exited, bailing out...");
anyhow::bail!("Telegram dispatcher has exited.")
}
} }

View file

@ -1,7 +1,7 @@
// See the following link for an example of how to use this file: // See the following link for an example of how to use this file:
// https://github.com/teloxide/teloxide/blob/master/crates/teloxide/examples/dispatching_features.rs // https://github.com/teloxide/teloxide/blob/master/crates/teloxide/examples/dispatching_features.rs
use anyhow::{Context, Error}; use anyhow::{Context, Error, Result};
use teloxide::{Bot, dptree}; use teloxide::{Bot, dptree};
use teloxide::dispatching::{DefaultKey, Dispatcher, HandlerExt, UpdateFilterExt}; use teloxide::dispatching::{DefaultKey, Dispatcher, HandlerExt, UpdateFilterExt};
use teloxide::dptree::entry; use teloxide::dptree::entry;
@ -37,6 +37,20 @@ pub enum Command {
Reminder(reminder::ReminderArgs), Reminder(reminder::ReminderArgs),
} }
impl Command {
pub async fn set_commands(bot: &mut Bot) -> Result<()> {
log::trace!("Determining bot commands...");
let commands = Self::bot_commands();
log::trace!("Setting commands on {bot:?}: {commands:#?}");
let reply = bot.set_my_commands(commands).await
.context("Impossibile aggiornare l'elenco comandi del bot.")?;
log::trace!("Setting commands on {bot:?} successful: {reply:#?}");
Ok(())
}
}
async fn handle_command(bot: Bot, command: Command, message: Message) -> CommandResult { async fn handle_command(bot: Bot, command: Command, message: Message) -> CommandResult {
log::trace!("Received command: {command:?}"); log::trace!("Received command: {command:?}");

View file

@ -1,14 +1,33 @@
use anyhow::Error; use std::convert::Infallible;
use teloxide::Bot; use teloxide::Bot;
use teloxide::dispatching::{DefaultKey, Dispatcher}; use super::RoyalnetService;
mod config; mod config;
mod commands; mod commands;
pub fn init() -> Dispatcher<Bot, Error, DefaultKey> { pub struct BotService {
commands::dispatcher( pub bot: Bot
Bot::new( }
config::TELEGRAM_BOT_TOKEN()
) impl BotService {
) pub fn from_config() -> Self {
Self {
bot: Bot::new(config::TELEGRAM_BOT_TOKEN())
}
}
}
impl RoyalnetService for BotService {
async fn run(mut self) -> anyhow::Result<Infallible> {
log::info!("Starting Telegram service...");
log::debug!("Setting bot commands...");
commands::Command::set_commands(&mut self.bot).await?;
log::debug!("Starting Telegram dispatcher...");
commands::dispatcher(self.bot).dispatch().await;
log::error!("Telegram dispatcher has exited, bailing out...");
anyhow::bail!("Telegram dispatcher has exited.")
}
} }