diff --git a/migrations/1_init.sql b/migrations/1_init.sql index ebcab32..334275d 100644 --- a/migrations/1_init.sql +++ b/migrations/1_init.sql @@ -6,7 +6,8 @@ CREATE TABLE "user" first_name TEXT NOT NULL, last_name TEXT, can_download INTEGER NOT NULL, - is_admin INTEGER NOT NULL + is_admin INTEGER NOT NULL, + has_private_chat INTEGER NOT NULL ); CREATE TABLE "chat" @@ -47,6 +48,4 @@ AFTER UPDATE OF is_approved ON "request" WHEN new.is_approved = 1 BEGIN UPDATE user SET can_download = 1 WHERE user.id = new.requested_by; -END; - --- TODO: add dialog start table \ No newline at end of file +END; \ No newline at end of file diff --git a/src/bot.rs b/src/bot.rs index 7e137ac..b1d6c64 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -2,4 +2,5 @@ pub mod bot; pub mod dl; pub mod op; pub mod sanitize; +pub mod start; pub mod types; diff --git a/src/bot/bot.rs b/src/bot/bot.rs index 6312438..0d69152 100644 --- a/src/bot/bot.rs +++ b/src/bot/bot.rs @@ -1,19 +1,17 @@ use anyhow; -use sqlx::SqlitePool; use std::env; use std::fmt; use std::str; use std::str::FromStr; -use std::sync::Arc; use std::time::Duration; use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler}; -use teloxide::types::Recipient; use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands}; use tracing::{event, Level}; use super::types::*; use crate::db::DbPool; +use super::start::cmd_start; use super::dl::cmd_download; use super::op::cmd_op; @@ -58,6 +56,7 @@ fn schema() -> UpdateHandler { let command_handler = teloxide::filter_command::() .branch(case![Command::Test].endpoint(cmd_test)) + .branch(case![Command::Start].endpoint(cmd_start)) .branch(case![Command::Download(url)].endpoint(cmd_download)) .branch(case![Command::OP].endpoint(cmd_op)); @@ -74,6 +73,9 @@ fn schema() -> UpdateHandler { enum Command { Test, + #[command(alias = "start")] + Start, + #[command(alias = "dl")] Download(String), @@ -83,10 +85,12 @@ enum Command { async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult { bot.send_message(msg.chat.id, "test response").await?; + dbg!(msg); Ok(()) } async fn handle_message(_bot: Bot, _dialogue: MyDialogue, _msg: Message) -> HandlerResult { + dbg!(_msg); Ok(()) } diff --git a/src/bot/start.rs b/src/bot/start.rs new file mode 100644 index 0000000..d1a853e --- /dev/null +++ b/src/bot/start.rs @@ -0,0 +1,23 @@ +use sqlx::Row; +use teloxide::prelude::*; +use tracing::{event, Level}; + +use super::types::HandlerResult; +use crate::db::user::find_or_create_user; +use crate::db::DbPool; + +pub async fn cmd_start(bot: Bot, msg: Message, db: DbPool) -> HandlerResult { + if msg.chat.is_private() { + if let Some(user) = msg.from() { + let user = find_or_create_user(&db, user).await?; + sqlx::query("UPDATE user SET has_private_chat = 1 WHERE id = $1;") + .bind(user.id) + .execute(&db) + .await?; + + event!(Level::INFO, "user {} has started private chat with bot", user); + bot.send_message(msg.chat.id, "Since you've initiated private chat now you could receive messages from bot").await?; + } + } + Ok(()) +} diff --git a/src/db/user.rs b/src/db/user.rs index e58f7ae..478baad 100644 --- a/src/db/user.rs +++ b/src/db/user.rs @@ -11,7 +11,7 @@ pub async fn create_user( sqlx::query( "INSERT OR IGNORE INTO user (tg_id, username, first_name, last_name, can_download, is_admin) - VALUES ($1,$2,$3,$4,$5,$6);", + VALUES ($1,$2,$3,$4,$5,$6,$7);", ) .bind(user.id.0 as i64) .bind(&user.username) @@ -19,6 +19,7 @@ pub async fn create_user( .bind(&user.last_name) .bind(can_download as i64) .bind(is_admin as i64) + .bind(0) .execute(db) .await?; diff --git a/src/main.rs b/src/main.rs index e196eee..4bb61dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use dotenv::dotenv; mod bot;