diff --git a/src/bot.rs b/src/bot.rs new file mode 100644 index 0000000..96f1e66 --- /dev/null +++ b/src/bot.rs @@ -0,0 +1 @@ +pub mod bot; \ No newline at end of file diff --git a/src/bot/bot.rs b/src/bot/bot.rs new file mode 100644 index 0000000..1b750cd --- /dev/null +++ b/src/bot/bot.rs @@ -0,0 +1,95 @@ +use anyhow; +use std::env; +use std::fmt; +use std::str; +use std::str::FromStr; +use std::time::Duration; +use teloxide::dispatching::dialogue; +use teloxide::dispatching::dialogue::InMemStorage; +use teloxide::dispatching::UpdateHandler; +use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands}; + +type State = (); +type MyDialogue = Dialogue>; + +type HandlerErr = Box; +type HandlerResult = Result<(), HandlerErr>; + +fn parse_env(name: &str) -> T +where + T: FromStr, + T::Err: fmt::Debug, +{ + str::parse( + env::var(name) + .expect(format!("env '{}' variable not defined", name).as_str()) + .as_str(), + ) + .expect(format!("env '{}' parse error", name).as_str()) +} + +pub async fn bot_main() -> anyhow::Result<()> { + let bot = Bot::new(env::var("BOT_TOKEN")?); + let listener = Polling::builder(bot.clone()) + .timeout(Duration::from_secs(parse_env("POLLING_TIMEOUT"))) + .limit(parse_env("POLLING_LIMIT")) + .drop_pending_updates() + .build(); + + Dispatcher::builder(bot, schema()) + .dependencies(dptree::deps![InMemStorage::::new()]) + .enable_ctrlc_handler() + .build() + .dispatch_with_listener( + listener, + LoggingErrorHandler::with_custom_text("update listener error"), + ) + .await; + + Ok(()) +} + +fn schema() -> UpdateHandler { + use dptree::case; + + let command_handler = teloxide::filter_command::() + .branch(case![Command::Test].endpoint(test)) + .branch(case![Command::Download(url)].endpoint(download)); + + let message_handler = Update::filter_message().branch(command_handler); + let raw_message_handler = Update::filter_message().branch(dptree::endpoint(handle_message)); + + dialogue::enter::, (), _>() + .branch(message_handler) + .branch(raw_message_handler) +} + +#[derive(BotCommands, Clone)] +#[command(rename_rule = "lowercase")] +enum Command { + Test, + + #[command(alias = "dl")] + Download(String), +} + +async fn test(bot: Bot, msg: Message) -> HandlerResult { + bot.send_message(msg.chat.id, "test response").await?; + + Ok(()) +} + +async fn download(bot: Bot, msg: Message, url: String) -> HandlerResult { + Ok(()) +} + +async fn handle_message(_bot: Bot, _dialogue: MyDialogue, msg: Message) -> HandlerResult { + println!( + "msg {} kind {:?} text {}", + msg.id, + msg.kind, + msg.text().unwrap_or("") + ); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index b232435..e0f64bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,38 +1,13 @@ -use anyhow; -use dotenv::dotenv; use std::env; -use std::fmt; -use std::str; -use std::str::FromStr; -use std::time::Duration; -use teloxide::dispatching::dialogue; -use teloxide::dispatching::dialogue::InMemStorage; -use teloxide::dispatching::UpdateHandler; -use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands}; +use dotenv::dotenv; + +mod bot; +use bot::bot::bot_main; mod dl; use dl::ffmpeg::FFMpeg; use dl::yt_dlp::YtDlp; -type State = (); -type MyDialogue = Dialogue>; - -type HandlerErr = Box; -type HandlerResult = Result<(), HandlerErr>; - -fn parse_env(name: &str) -> T -where - T: FromStr, - T::Err: fmt::Debug, -{ - str::parse( - env::var(name) - .expect(format!("env '{}' variable not defined", name).as_str()) - .as_str(), - ) - .expect(format!("env '{}' parse error", name).as_str()) -} - #[tokio::main] async fn main() -> anyhow::Result<()> { dotenv().ok(); @@ -60,69 +35,3 @@ async fn main() -> anyhow::Result<()> { Ok(()) //bot_main().await } - -async fn bot_main() -> anyhow::Result<()> { - let bot = Bot::new(env::var("BOT_TOKEN")?); - let listener = Polling::builder(bot.clone()) - .timeout(Duration::from_secs(parse_env("POLLING_TIMEOUT"))) - .limit(parse_env("POLLING_LIMIT")) - .drop_pending_updates() - .build(); - - Dispatcher::builder(bot, schema()) - .dependencies(dptree::deps![InMemStorage::::new()]) - .enable_ctrlc_handler() - .build() - .dispatch_with_listener( - listener, - LoggingErrorHandler::with_custom_text("update listener error"), - ) - .await; - - Ok(()) -} - -fn schema() -> UpdateHandler { - use dptree::case; - - let command_handler = teloxide::filter_command::() - .branch(case![Command::Test].endpoint(test)) - .branch(case![Command::Download(url)].endpoint(download)); - - let message_handler = Update::filter_message().branch(command_handler); - let raw_message_handler = Update::filter_message().branch(dptree::endpoint(handle_message)); - - dialogue::enter::, (), _>() - .branch(message_handler) - .branch(raw_message_handler) -} - -#[derive(BotCommands, Clone)] -#[command(rename_rule = "lowercase")] -enum Command { - Test, - - #[command(alias = "dl")] - Download(String), -} - -async fn test(bot: Bot, msg: Message) -> HandlerResult { - bot.send_message(msg.chat.id, "test response").await?; - - Ok(()) -} - -async fn download(bot: Bot, msg: Message, url: String) -> HandlerResult { - Ok(()) -} - -async fn handle_message(_bot: Bot, _dialogue: MyDialogue, msg: Message) -> HandlerResult { - println!( - "msg {} kind {:?} text {}", - msg.id, - msg.kind, - msg.text().unwrap_or("") - ); - - Ok(()) -}