implement command start (for private chats)
This commit is contained in:
parent
197429a422
commit
842a710928
6 changed files with 36 additions and 10 deletions
|
|
@ -6,7 +6,8 @@ CREATE TABLE "user"
|
||||||
first_name TEXT NOT NULL,
|
first_name TEXT NOT NULL,
|
||||||
last_name TEXT,
|
last_name TEXT,
|
||||||
can_download INTEGER NOT NULL,
|
can_download INTEGER NOT NULL,
|
||||||
is_admin INTEGER NOT NULL
|
is_admin INTEGER NOT NULL,
|
||||||
|
has_private_chat INTEGER NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE "chat"
|
CREATE TABLE "chat"
|
||||||
|
|
@ -48,5 +49,3 @@ WHEN new.is_approved = 1
|
||||||
BEGIN
|
BEGIN
|
||||||
UPDATE user SET can_download = 1 WHERE user.id = new.requested_by;
|
UPDATE user SET can_download = 1 WHERE user.id = new.requested_by;
|
||||||
END;
|
END;
|
||||||
|
|
||||||
-- TODO: add dialog start table
|
|
||||||
|
|
@ -2,4 +2,5 @@ pub mod bot;
|
||||||
pub mod dl;
|
pub mod dl;
|
||||||
pub mod op;
|
pub mod op;
|
||||||
pub mod sanitize;
|
pub mod sanitize;
|
||||||
|
pub mod start;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,17 @@
|
||||||
use anyhow;
|
use anyhow;
|
||||||
use sqlx::SqlitePool;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler};
|
use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler};
|
||||||
use teloxide::types::Recipient;
|
|
||||||
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 super::types::*;
|
use super::types::*;
|
||||||
use crate::db::DbPool;
|
use crate::db::DbPool;
|
||||||
|
|
||||||
|
use super::start::cmd_start;
|
||||||
use super::dl::cmd_download;
|
use super::dl::cmd_download;
|
||||||
use super::op::cmd_op;
|
use super::op::cmd_op;
|
||||||
|
|
||||||
|
|
@ -58,6 +56,7 @@ fn schema() -> UpdateHandler<HandlerErr> {
|
||||||
|
|
||||||
let command_handler = teloxide::filter_command::<Command, _>()
|
let command_handler = teloxide::filter_command::<Command, _>()
|
||||||
.branch(case![Command::Test].endpoint(cmd_test))
|
.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::Download(url)].endpoint(cmd_download))
|
||||||
.branch(case![Command::OP].endpoint(cmd_op));
|
.branch(case![Command::OP].endpoint(cmd_op));
|
||||||
|
|
||||||
|
|
@ -74,6 +73,9 @@ fn schema() -> UpdateHandler<HandlerErr> {
|
||||||
enum Command {
|
enum Command {
|
||||||
Test,
|
Test,
|
||||||
|
|
||||||
|
#[command(alias = "start")]
|
||||||
|
Start,
|
||||||
|
|
||||||
#[command(alias = "dl")]
|
#[command(alias = "dl")]
|
||||||
Download(String),
|
Download(String),
|
||||||
|
|
||||||
|
|
@ -83,10 +85,12 @@ enum Command {
|
||||||
|
|
||||||
async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult {
|
async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult {
|
||||||
bot.send_message(msg.chat.id, "test response").await?;
|
bot.send_message(msg.chat.id, "test response").await?;
|
||||||
|
dbg!(msg);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_message(_bot: Bot, _dialogue: MyDialogue, _msg: Message) -> HandlerResult {
|
async fn handle_message(_bot: Bot, _dialogue: MyDialogue, _msg: Message) -> HandlerResult {
|
||||||
|
dbg!(_msg);
|
||||||
Ok(())
|
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(
|
sqlx::query(
|
||||||
"INSERT OR IGNORE INTO user
|
"INSERT OR IGNORE INTO user
|
||||||
(tg_id, username, first_name, last_name, can_download, is_admin)
|
(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.id.0 as i64)
|
||||||
.bind(&user.username)
|
.bind(&user.username)
|
||||||
|
|
@ -19,6 +19,7 @@ pub async fn create_user(
|
||||||
.bind(&user.last_name)
|
.bind(&user.last_name)
|
||||||
.bind(can_download as i64)
|
.bind(can_download as i64)
|
||||||
.bind(is_admin as i64)
|
.bind(is_admin as i64)
|
||||||
|
.bind(0)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
|
||||||
mod bot;
|
mod bot;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue