clessy-rs/src/main.rs

80 lines
1.9 KiB
Rust

use std::sync::Arc;
use anyhow::Result;
use commands::{metafora, proverbio, sunny::Sunny};
use image::EncodableLayout;
use teloxide::{prelude::*, types::InputFile, utils::command::BotCommands};
mod commands;
#[tokio::main]
async fn main() -> Result<()> {
dotenvy::dotenv()?;
tracing_subscriber::fmt::init();
let bot = Bot::from_env();
let clessy = Arc::new(Clessy::new()?);
let handler = Update::filter_message()
.filter_command::<Command>()
.endpoint(answer);
Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![clessy])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
Ok(())
}
struct Clessy<'a> {
sunny: Sunny<'a>,
}
impl Clessy<'_> {
fn new() -> Result<Self> {
Ok(Self {
sunny: Sunny::init()?,
})
}
}
#[derive(BotCommands, Clone)]
#[command(rename_rule = "lowercase", description = "Comandi del bot:")]
enum Command {
#[command(description = "mostra questo testo")]
Help,
#[command(description = "genera metafora")]
Metafora,
#[command(description = "genera proverbio")]
Proverbio,
#[command(description = "It's Always Sunny in Philadelphia")]
Sunny(String),
}
async fn answer(
bot: Bot,
clessy: Arc<Clessy<'_>>,
msg: Message,
cmd: Command,
) -> ResponseResult<()> {
match cmd {
Command::Help => {
bot.send_message(msg.chat.id, Command::descriptions().to_string())
.await?;
}
Command::Metafora => {
bot.send_message(msg.chat.id, metafora::generate()).await?;
}
Command::Proverbio => {
bot.send_message(msg.chat.id, proverbio::generate()).await?;
}
Command::Sunny(text) => {
let pic = clessy.sunny.generate(&text).unwrap();
bot.send_photo(msg.chat.id, InputFile::memory(pic)).await?;
}
};
Ok(())
}