move everything related to telegram bot to appropriate module

This commit is contained in:
mykola2312 2024-02-21 14:52:54 +02:00
parent cdcd9f7fe6
commit 0bee30ea0a
3 changed files with 100 additions and 95 deletions

1
src/bot.rs Normal file
View file

@ -0,0 +1 @@
pub mod bot;

95
src/bot/bot.rs Normal file
View file

@ -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<State, InMemStorage<State>>;
type HandlerErr = Box<dyn std::error::Error + Send + Sync>;
type HandlerResult = Result<(), HandlerErr>;
fn parse_env<T>(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::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch_with_listener(
listener,
LoggingErrorHandler::with_custom_text("update listener error"),
)
.await;
Ok(())
}
fn schema() -> UpdateHandler<HandlerErr> {
use dptree::case;
let command_handler = teloxide::filter_command::<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::<Update, InMemStorage<()>, (), _>()
.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("<empty>")
);
Ok(())
}

View file

@ -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<State, InMemStorage<State>>;
type HandlerErr = Box<dyn std::error::Error + Send + Sync>;
type HandlerResult = Result<(), HandlerErr>;
fn parse_env<T>(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::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch_with_listener(
listener,
LoggingErrorHandler::with_custom_text("update listener error"),
)
.await;
Ok(())
}
fn schema() -> UpdateHandler<HandlerErr> {
use dptree::case;
let command_handler = teloxide::filter_command::<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::<Update, InMemStorage<()>, (), _>()
.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("<empty>")
);
Ok(())
}