rearrange file structure for bot commands
This commit is contained in:
parent
bb6687ac46
commit
1e6d671feb
5 changed files with 49 additions and 42 deletions
|
|
@ -1,2 +1,4 @@
|
|||
pub mod bot;
|
||||
pub mod dl;
|
||||
pub mod sanitize;
|
||||
pub mod types;
|
||||
|
|
|
|||
|
|
@ -8,20 +8,11 @@ use std::time::Duration;
|
|||
use teloxide::dispatching::dialogue;
|
||||
use teloxide::dispatching::dialogue::InMemStorage;
|
||||
use teloxide::dispatching::UpdateHandler;
|
||||
use teloxide::types::InputFile;
|
||||
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
|
||||
use tracing::{event, Level};
|
||||
|
||||
use crate::dl::delete_if_exists;
|
||||
use crate::dl::download;
|
||||
|
||||
use crate::db;
|
||||
|
||||
type State = ();
|
||||
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
||||
|
||||
type HandlerErr = Box<dyn std::error::Error + Send + Sync>;
|
||||
type HandlerResult = Result<(), HandlerErr>;
|
||||
use super::dl::cmd_download;
|
||||
use super::types::*;
|
||||
|
||||
fn parse_env<T>(name: &str) -> T
|
||||
where
|
||||
|
|
@ -83,37 +74,12 @@ enum Command {
|
|||
Download(String),
|
||||
}
|
||||
|
||||
async fn cmd_test(bot: Bot, msg: Message, db: SqlitePool) -> HandlerResult {
|
||||
async fn cmd_test(bot: Bot, msg: Message, _db: SqlitePool) -> HandlerResult {
|
||||
bot.send_message(msg.chat.id, "test response").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn bot_download(bot: Bot, msg: Message, url: String) -> HandlerResult {
|
||||
let output_path = match download(url.as_str()).await {
|
||||
Ok(path) => path,
|
||||
Err(e) => {
|
||||
event!(Level::ERROR, "{}", e.to_string());
|
||||
bot.send_message(msg.chat.id, e.to_string()).await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(e) = bot
|
||||
.send_video(msg.chat.id, InputFile::file(&output_path))
|
||||
.await
|
||||
{
|
||||
delete_if_exists(&output_path);
|
||||
return Err(Box::new(e));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn cmd_download(bot: Bot, msg: Message, url: String) -> HandlerResult {
|
||||
bot_download(bot, msg, url).await
|
||||
}
|
||||
|
||||
async fn handle_message(_bot: Bot, _dialogue: MyDialogue, _msg: Message) -> HandlerResult {
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
32
src/bot/dl.rs
Normal file
32
src/bot/dl.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use teloxide::prelude::*;
|
||||
use teloxide::types::InputFile;
|
||||
use tracing::{event, Level};
|
||||
|
||||
use super::types::HandlerResult;
|
||||
use crate::dl::delete_if_exists;
|
||||
use crate::dl::download;
|
||||
|
||||
async fn bot_download(bot: Bot, msg: Message, url: String) -> HandlerResult {
|
||||
let output_path = match download(url.as_str()).await {
|
||||
Ok(path) => path,
|
||||
Err(e) => {
|
||||
event!(Level::ERROR, "{}", e.to_string());
|
||||
bot.send_message(msg.chat.id, e.to_string()).await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(e) = bot
|
||||
.send_video(msg.chat.id, InputFile::file(&output_path))
|
||||
.await
|
||||
{
|
||||
delete_if_exists(&output_path);
|
||||
return Err(Box::new(e));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_download(bot: Bot, msg: Message, url: String) -> HandlerResult {
|
||||
bot_download(bot, msg, url).await
|
||||
}
|
||||
8
src/bot/types.rs
Normal file
8
src/bot/types.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
use teloxide::dispatching::dialogue::InMemStorage;
|
||||
use teloxide::prelude::*;
|
||||
|
||||
pub type State = ();
|
||||
pub type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
||||
|
||||
pub type HandlerErr = Box<dyn std::error::Error + Send + Sync>;
|
||||
pub type HandlerResult = Result<(), HandlerErr>;
|
||||
|
|
@ -3,7 +3,6 @@ use sqlx::{Sqlite, SqlitePool};
|
|||
|
||||
use super::util::make_database_url;
|
||||
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
pub struct User {
|
||||
pub id: i64,
|
||||
|
|
@ -21,7 +20,7 @@ pub struct Chat {
|
|||
pub tg_id: i64,
|
||||
pub username: Option<String>,
|
||||
pub title: String,
|
||||
pub can_download: i64
|
||||
pub can_download: i64,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
|
|
@ -30,7 +29,7 @@ pub struct Link {
|
|||
pub domain: String,
|
||||
pub path: Option<String>,
|
||||
pub download_allowed: i64,
|
||||
pub auto_download: i64
|
||||
pub auto_download: i64,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
|
|
@ -39,7 +38,7 @@ pub struct Request {
|
|||
pub requested_by: i64,
|
||||
pub approved_by: Option<i64>,
|
||||
pub message: Option<String>,
|
||||
pub is_approved: i64
|
||||
pub is_approved: i64,
|
||||
}
|
||||
|
||||
pub async fn db_init() -> SqlitePool {
|
||||
|
|
@ -54,4 +53,4 @@ pub async fn db_init() -> SqlitePool {
|
|||
sqlx::migrate!().run(&db).await.unwrap();
|
||||
|
||||
db
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue