diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..d6064a7 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,30 @@ +use teloxide::{prelude::*, utils::command::BotCommands}; + +use crate::comments::{Comments, global_comments}; + +#[derive(BotCommands, Clone)] +#[command(rename_rule = "lowercase")] +pub enum Command { + /// Display this text. + #[command(aliases = ["h", "?"])] + Help, + /// Send a random comment + #[command()] + Curse, +} + +pub async fn answer(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> { + match cmd { + Command::Help => { + bot.send_message(msg.chat.id, Command::descriptions().to_string()) + .await? + } + Command::Curse => { + let comment = global_comments().map(Comments::build_caption); + bot.send_message(msg.chat.id, comment.unwrap_or_else(|| "To comment".into())) + .await? + } + }; + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index e7f3b95..2e0ac25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod commands; pub mod comments; pub mod download; pub mod error; diff --git a/src/main.rs b/src/main.rs index 2e65e94..0104a6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ use dotenv::dotenv; use std::sync::Arc; -use teloxide::{Bot, prelude::Requester, respond, types::Message}; +use teloxide::{Bot, prelude::Requester, repls::CommandReplExt, respond, types::Message}; use tg_relay_rs::{ + commands::{Command, answer}, comments::{Comments, init_global_comments}, handlers::SocialHandler, telemetry::setup_logger, @@ -38,6 +39,8 @@ async fn main() -> color_eyre::Result<()> { Arc::new(tg_relay_rs::handlers::TwitterHandler), ]; + Command::repl(bot.clone(), answer).await; + teloxide::repl(bot.clone(), move |bot: Bot, msg: Message| { // clone the handlers vector into the closure let handlers = handlers.clone(); @@ -58,7 +61,7 @@ async fn main() -> color_eyre::Result<()> { .await; } }); - // if one handler matcher, stop checking + // if one handler matched, stop checking break; } }