implement command start (for private chats)
This commit is contained in:
parent
b460747a72
commit
206965ed57
6 changed files with 36 additions and 10 deletions
|
|
@ -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;
|
||||
|
|
@ -2,4 +2,5 @@ pub mod bot;
|
|||
pub mod dl;
|
||||
pub mod op;
|
||||
pub mod sanitize;
|
||||
pub mod start;
|
||||
pub mod types;
|
||||
|
|
|
|||
|
|
@ -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
23
src/bot/start.rs
Normal 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(())
|
||||
}
|
||||
|
|
@ -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?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use dotenv::dotenv;
|
||||
|
||||
mod bot;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue