diff --git a/locales/en.yml b/locales/en.yml index 8d65a4b..2b6b89e 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -2,4 +2,5 @@ test_response: "test response" op_yourself: "Now you're an admin" has_to_reply: "You have to reply on target's message" cant_do_that: "You can't do that bruh" -started_private_chat: "Since you've initiated private chat now you could receive messages from bot" \ No newline at end of file +started_private_chat: "Since you've initiated private chat now you could receive messages from bot" +started_public_chat: "For using the bot you may want to request access via /request or /request_chat" \ No newline at end of file diff --git a/src/bot/bot.rs b/src/bot/bot.rs index d5c8214..bd9660f 100644 --- a/src/bot/bot.rs +++ b/src/bot/bot.rs @@ -1,15 +1,16 @@ use anyhow; use rust_i18n::t; -use teloxide::types::UpdateKind; use std::env; use std::fmt; use std::str; use std::str::FromStr; use std::time::Duration; use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler}; +use teloxide::types::{Me, MessageKind, MessageNewChatMembers, UpdateKind}; use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands}; use tracing::{event, Level}; +use super::start::handle_new_chat_member; use super::types::*; use crate::db::DbPool; @@ -74,7 +75,27 @@ fn schema() -> UpdateHandler { async fn handle_update(_bot: Bot, upd: Update, db: DbPool) -> HandlerResult { match upd.kind { UpdateKind::MyChatMember(upd) => handle_my_chat_member(db, upd).await, - _ => event!(Level::WARN, "unhandled update {:?}", upd) + _ => event!(Level::WARN, "unhandled update {:?}", upd), + } + + Ok(()) +} + +async fn handle_message( + bot: Bot, + _dialogue: MyDialogue, + msg: Message, + db: DbPool, + me: Me, +) -> HandlerResult { + match msg.kind { + MessageKind::NewChatMembers(MessageNewChatMembers { new_chat_members }) => { + handle_new_chat_member(bot, &msg.chat, new_chat_members, db, me).await? + }, + MessageKind::Common(_) => (), + _ => { + dbg!(msg); + } } Ok(()) @@ -101,8 +122,3 @@ async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult { 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 index 0fc3a72..aa062c9 100644 --- a/src/bot/start.rs +++ b/src/bot/start.rs @@ -1,10 +1,11 @@ use rust_i18n::t; use teloxide::prelude::*; +use teloxide::types::{Me, MessageNewChatMembers}; use tracing::{event, Level}; use super::types::HandlerResult; -use crate::db::user::find_or_create_user; use crate::db::chat::find_or_create_chat; +use crate::db::user::find_or_create_user; use crate::db::DbPool; pub async fn cmd_start(bot: Bot, msg: Message, db: DbPool) -> HandlerResult { @@ -31,6 +32,26 @@ pub async fn cmd_start(bot: Bot, msg: Message, db: DbPool) -> HandlerResult { pub async fn handle_my_chat_member(db: DbPool, upd: ChatMemberUpdated) { match find_or_create_chat(&db, &upd.chat).await { Ok(chat) => event!(Level::INFO, "started public chat {}", chat), - Err(e) => event!(Level::ERROR, "{}", e) + Err(e) => event!(Level::ERROR, "{}", e), } -} \ No newline at end of file +} + +pub async fn handle_new_chat_member( + bot: Bot, + tg_chat: &teloxide::types::Chat, + new: Vec, + db: DbPool, + me: Me, +) -> HandlerResult { + for member in new { + if member.id == me.id { + // We've been added in chat + let chat = find_or_create_chat(&db, tg_chat).await?; + event!(Level::INFO, "started public chat {}", chat); + bot.send_message(tg_chat.id, t!("started_public_chat")) + .await?; + } + } + + Ok(()) +} diff --git a/src/db.rs b/src/db.rs index 9fbd3e4..cd369ec 100644 --- a/src/db.rs +++ b/src/db.rs @@ -108,4 +108,4 @@ macro_rules! unwrap_or_create { } } } -} \ No newline at end of file +} diff --git a/src/db/chat.rs b/src/db/chat.rs index 9b710f0..c3549a8 100644 --- a/src/db/chat.rs +++ b/src/db/chat.rs @@ -17,15 +17,17 @@ pub async fn create_chat(db: &DbPool, chat: &types::Chat) -> Result Result { - let res: Result = sqlx::query_as( - "SELECT * FROM chat WHERE tg_id = $1 LIMIT 1;") - .bind(chat.id.0 as i64) - .fetch_one(db).await; + let res: Result = + sqlx::query_as("SELECT * FROM chat WHERE tg_id = $1 LIMIT 1;") + .bind(chat.id.0 as i64) + .fetch_one(db) + .await; unwrap_or_create!(db, chat, res, create_chat) -} \ No newline at end of file +}