mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-22 02:54:21 +00:00
Refactor services again to use traits and to allow bot to update its own command list
This commit is contained in:
parent
7a8d30e1d0
commit
05592396e7
4 changed files with 45 additions and 23 deletions
|
@ -1,5 +1,4 @@
|
|||
use anyhow::Result;
|
||||
use services::telegram;
|
||||
use crate::services::RoyalnetService;
|
||||
|
||||
pub(crate) mod database;
|
||||
|
@ -15,12 +14,12 @@ async fn main() -> Result<()> {
|
|||
|
||||
// Telegram setup
|
||||
log::trace!("Setting up Telegram bot service...");
|
||||
let telegram = telegram::init();
|
||||
let telegram = services::telegram::BotService::from_config();
|
||||
|
||||
// Run all services concurrently
|
||||
log::info!("Starting services...");
|
||||
let result = tokio::try_join![
|
||||
telegram.run_royalnet(),
|
||||
telegram.run(),
|
||||
];
|
||||
|
||||
// This should never happen, but just in case...
|
||||
|
|
|
@ -4,15 +4,5 @@ use anyhow::Result;
|
|||
pub mod telegram;
|
||||
|
||||
pub trait RoyalnetService {
|
||||
async fn run_royalnet(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.")
|
||||
}
|
||||
async fn run(self) -> Result<Infallible>;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// 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
|
||||
|
||||
use anyhow::{Context, Error};
|
||||
use anyhow::{Context, Error, Result};
|
||||
use teloxide::{Bot, dptree};
|
||||
use teloxide::dispatching::{DefaultKey, Dispatcher, HandlerExt, UpdateFilterExt};
|
||||
use teloxide::dptree::entry;
|
||||
|
@ -37,6 +37,20 @@ pub enum Command {
|
|||
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 {
|
||||
log::trace!("Received command: {command:?}");
|
||||
|
||||
|
|
|
@ -1,14 +1,33 @@
|
|||
use anyhow::Error;
|
||||
use std::convert::Infallible;
|
||||
use teloxide::Bot;
|
||||
use teloxide::dispatching::{DefaultKey, Dispatcher};
|
||||
use super::RoyalnetService;
|
||||
|
||||
mod config;
|
||||
mod commands;
|
||||
|
||||
pub fn init() -> Dispatcher<Bot, Error, DefaultKey> {
|
||||
commands::dispatcher(
|
||||
Bot::new(
|
||||
config::TELEGRAM_BOT_TOKEN()
|
||||
)
|
||||
)
|
||||
pub struct BotService {
|
||||
pub bot: Bot
|
||||
}
|
||||
|
||||
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.")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue