begin embedding sqlx into teloxide handlers
This commit is contained in:
parent
999cba08b6
commit
b3900a83e3
6 changed files with 38 additions and 7 deletions
|
|
@ -10,7 +10,7 @@ anyhow = "1.0.75"
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
tokio = { version = "1.32.0", features = ["rt-multi-thread", "macros", "process"] }
|
tokio = { version = "1.32.0", features = ["rt-multi-thread", "macros", "process"] }
|
||||||
teloxide = { version = "0.12.2", git ="https://github.com/teloxide/teloxide", features = ["macros"] }
|
teloxide = { version = "0.12.2", git ="https://github.com/teloxide/teloxide", features = ["macros"] }
|
||||||
sqlx = { version = "0.7.3", features = [ "runtime-tokio", "tls-native-tls" ] }
|
sqlx = { version = "0.7.3", features = [ "runtime-tokio", "tls-native-tls", "sqlite", "sqlx-sqlite" ] }
|
||||||
serde = { version = "1.0.196", features = ["derive"] }
|
serde = { version = "1.0.196", features = ["derive"] }
|
||||||
serde_json = "1.0.113"
|
serde_json = "1.0.113"
|
||||||
ordered-float = "4.2.0"
|
ordered-float = "4.2.0"
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod bot;
|
pub mod bot;
|
||||||
pub mod sanitize;
|
pub mod sanitize;
|
||||||
|
pub mod util;
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,9 @@ use teloxide::dispatching::dialogue::InMemStorage;
|
||||||
use teloxide::dispatching::UpdateHandler;
|
use teloxide::dispatching::UpdateHandler;
|
||||||
use teloxide::types::InputFile;
|
use teloxide::types::InputFile;
|
||||||
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
|
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
|
||||||
|
use sqlx::SqlitePool;
|
||||||
|
|
||||||
|
use super::util::make_database_url;
|
||||||
|
|
||||||
use crate::dl::delete_if_exists;
|
use crate::dl::delete_if_exists;
|
||||||
use crate::dl::download;
|
use crate::dl::download;
|
||||||
|
|
@ -33,6 +36,9 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn bot_main() -> anyhow::Result<()> {
|
pub async fn bot_main() -> anyhow::Result<()> {
|
||||||
|
let db_path = make_database_url();
|
||||||
|
let db = SqlitePool::connect(&db_path).await?;
|
||||||
|
|
||||||
let bot = Bot::new(env::var("BOT_TOKEN")?);
|
let bot = Bot::new(env::var("BOT_TOKEN")?);
|
||||||
let listener = Polling::builder(bot.clone())
|
let listener = Polling::builder(bot.clone())
|
||||||
.timeout(Duration::from_secs(parse_env("POLLING_TIMEOUT")))
|
.timeout(Duration::from_secs(parse_env("POLLING_TIMEOUT")))
|
||||||
|
|
@ -41,7 +47,7 @@ pub async fn bot_main() -> anyhow::Result<()> {
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Dispatcher::builder(bot, schema())
|
Dispatcher::builder(bot, schema())
|
||||||
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
.dependencies(dptree::deps![db])
|
||||||
.enable_ctrlc_handler()
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
.dispatch_with_listener(
|
.dispatch_with_listener(
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ pub fn extract_url(text: &str) -> Option<&str> {
|
||||||
let re = Regex::new(RE_URL).unwrap();
|
let re = Regex::new(RE_URL).unwrap();
|
||||||
match re.find(text) {
|
match re.find(text) {
|
||||||
Some(m) => Some(m.as_str()),
|
Some(m) => Some(m.as_str()),
|
||||||
None => None
|
None => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -23,8 +23,14 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_extract_url() {
|
fn test_extract_url() {
|
||||||
assert_eq!(extract_url("test http://www.test.com/id/1"), Some("http://www.test.com/id/1"));
|
assert_eq!(
|
||||||
assert_eq!(extract_url("https://www.test.com 3"), Some("https://www.test.com"));
|
extract_url("test http://www.test.com/id/1"),
|
||||||
|
Some("http://www.test.com/id/1")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
extract_url("https://www.test.com 3"),
|
||||||
|
Some("https://www.test.com")
|
||||||
|
);
|
||||||
assert_eq!(extract_url("there is no any url"), None);
|
assert_eq!(extract_url("there is no any url"), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
18
src/bot/util.rs
Normal file
18
src/bot/util.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
const VAR_LIB: &str = ".";
|
||||||
|
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
const VAR_LIB: &str = "/var/lib/mk-dl-bot";
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
const VAR_LOG: &str = ".";
|
||||||
|
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
const VAR_LOG: &str = "/var/log/mk-dl-bot";
|
||||||
|
|
||||||
|
pub fn make_database_url() -> String {
|
||||||
|
let path = Path::new(VAR_LIB).join("mk-dl-bot.db");
|
||||||
|
format!("sqlite://{}", path.as_os_str().to_str().unwrap()).to_string()
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue