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 bot;
|
||||||
|
pub mod dl;
|
||||||
pub mod sanitize;
|
pub mod sanitize;
|
||||||
|
pub mod types;
|
||||||
|
|
|
||||||
|
|
@ -8,20 +8,11 @@ use std::time::Duration;
|
||||||
use teloxide::dispatching::dialogue;
|
use teloxide::dispatching::dialogue;
|
||||||
use teloxide::dispatching::dialogue::InMemStorage;
|
use teloxide::dispatching::dialogue::InMemStorage;
|
||||||
use teloxide::dispatching::UpdateHandler;
|
use teloxide::dispatching::UpdateHandler;
|
||||||
use teloxide::types::InputFile;
|
|
||||||
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
|
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
|
||||||
use tracing::{event, Level};
|
use tracing::{event, Level};
|
||||||
|
|
||||||
use crate::dl::delete_if_exists;
|
use super::dl::cmd_download;
|
||||||
use crate::dl::download;
|
use super::types::*;
|
||||||
|
|
||||||
use crate::db;
|
|
||||||
|
|
||||||
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
|
fn parse_env<T>(name: &str) -> T
|
||||||
where
|
where
|
||||||
|
|
@ -83,37 +74,12 @@ enum Command {
|
||||||
Download(String),
|
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?;
|
bot.send_message(msg.chat.id, "test response").await?;
|
||||||
|
|
||||||
Ok(())
|
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 {
|
async fn handle_message(_bot: Bot, _dialogue: MyDialogue, _msg: Message) -> HandlerResult {
|
||||||
Ok(())
|
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;
|
use super::util::make_database_url;
|
||||||
|
|
||||||
|
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
|
|
@ -21,7 +20,7 @@ pub struct Chat {
|
||||||
pub tg_id: i64,
|
pub tg_id: i64,
|
||||||
pub username: Option<String>,
|
pub username: Option<String>,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub can_download: i64
|
pub can_download: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
|
|
@ -30,7 +29,7 @@ pub struct Link {
|
||||||
pub domain: String,
|
pub domain: String,
|
||||||
pub path: Option<String>,
|
pub path: Option<String>,
|
||||||
pub download_allowed: i64,
|
pub download_allowed: i64,
|
||||||
pub auto_download: i64
|
pub auto_download: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
|
|
@ -39,7 +38,7 @@ pub struct Request {
|
||||||
pub requested_by: i64,
|
pub requested_by: i64,
|
||||||
pub approved_by: Option<i64>,
|
pub approved_by: Option<i64>,
|
||||||
pub message: Option<String>,
|
pub message: Option<String>,
|
||||||
pub is_approved: i64
|
pub is_approved: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn db_init() -> SqlitePool {
|
pub async fn db_init() -> SqlitePool {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue