implement command start (for private chats)

This commit is contained in:
mykola2312 2024-03-02 19:04:02 +02:00
parent 197429a422
commit 842a710928
6 changed files with 36 additions and 10 deletions

View file

@ -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
END;

View file

@ -2,4 +2,5 @@ pub mod bot;
pub mod dl;
pub mod op;
pub mod sanitize;
pub mod start;
pub mod types;

View file

@ -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<HandlerErr> {
let command_handler = teloxide::filter_command::<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<HandlerErr> {
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(())
}

23
src/bot/start.rs Normal file
View file

@ -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(())
}

View file

@ -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?;

View file

@ -1,5 +1,3 @@
use std::sync::Arc;
use dotenv::dotenv;
mod bot;