implement new chat memebers message handler to be able to know where bot have been added to register chat in database
This commit is contained in:
parent
3dfbb6fd6e
commit
b4e711a07d
5 changed files with 58 additions and 18 deletions
|
|
@ -2,4 +2,5 @@ test_response: "test response"
|
||||||
op_yourself: "Now you're an admin"
|
op_yourself: "Now you're an admin"
|
||||||
has_to_reply: "You have to reply on target's message"
|
has_to_reply: "You have to reply on target's message"
|
||||||
cant_do_that: "You can't do that bruh"
|
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"
|
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"
|
||||||
|
|
@ -1,15 +1,16 @@
|
||||||
use anyhow;
|
use anyhow;
|
||||||
use rust_i18n::t;
|
use rust_i18n::t;
|
||||||
use teloxide::types::UpdateKind;
|
|
||||||
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::time::Duration;
|
use std::time::Duration;
|
||||||
use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler};
|
use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler};
|
||||||
|
use teloxide::types::{Me, MessageKind, MessageNewChatMembers, UpdateKind};
|
||||||
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::start::handle_new_chat_member;
|
||||||
use super::types::*;
|
use super::types::*;
|
||||||
use crate::db::DbPool;
|
use crate::db::DbPool;
|
||||||
|
|
||||||
|
|
@ -74,7 +75,27 @@ fn schema() -> UpdateHandler<HandlerErr> {
|
||||||
async fn handle_update(_bot: Bot, upd: Update, db: DbPool) -> HandlerResult {
|
async fn handle_update(_bot: Bot, upd: Update, db: DbPool) -> HandlerResult {
|
||||||
match upd.kind {
|
match upd.kind {
|
||||||
UpdateKind::MyChatMember(upd) => handle_my_chat_member(db, upd).await,
|
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(())
|
Ok(())
|
||||||
|
|
@ -101,8 +122,3 @@ async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_message(_bot: Bot, _dialogue: MyDialogue, _msg: Message) -> HandlerResult {
|
|
||||||
dbg!(_msg);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
use rust_i18n::t;
|
use rust_i18n::t;
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
use teloxide::types::{Me, MessageNewChatMembers};
|
||||||
use tracing::{event, Level};
|
use tracing::{event, Level};
|
||||||
|
|
||||||
use super::types::HandlerResult;
|
use super::types::HandlerResult;
|
||||||
use crate::db::user::find_or_create_user;
|
|
||||||
use crate::db::chat::find_or_create_chat;
|
use crate::db::chat::find_or_create_chat;
|
||||||
|
use crate::db::user::find_or_create_user;
|
||||||
use crate::db::DbPool;
|
use crate::db::DbPool;
|
||||||
|
|
||||||
pub async fn cmd_start(bot: Bot, msg: Message, db: DbPool) -> HandlerResult {
|
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) {
|
pub async fn handle_my_chat_member(db: DbPool, upd: ChatMemberUpdated) {
|
||||||
match find_or_create_chat(&db, &upd.chat).await {
|
match find_or_create_chat(&db, &upd.chat).await {
|
||||||
Ok(chat) => event!(Level::INFO, "started public chat {}", chat),
|
Ok(chat) => event!(Level::INFO, "started public chat {}", chat),
|
||||||
Err(e) => event!(Level::ERROR, "{}", e)
|
Err(e) => event!(Level::ERROR, "{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn handle_new_chat_member(
|
||||||
|
bot: Bot,
|
||||||
|
tg_chat: &teloxide::types::Chat,
|
||||||
|
new: Vec<teloxide::types::User>,
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,4 +108,4 @@ macro_rules! unwrap_or_create {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,15 +17,17 @@ pub async fn create_chat(db: &DbPool, chat: &types::Chat) -> Result<Chat, sqlx::
|
||||||
|
|
||||||
let chat: Chat = sqlx::query_as("SELECT * FROM chat WHERE tg_id = $1 LIMIT 1;")
|
let chat: Chat = sqlx::query_as("SELECT * FROM chat WHERE tg_id = $1 LIMIT 1;")
|
||||||
.bind(chat.id.0 as i64)
|
.bind(chat.id.0 as i64)
|
||||||
.fetch_one(db).await?;
|
.fetch_one(db)
|
||||||
|
.await?;
|
||||||
Ok(chat)
|
Ok(chat)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn find_or_create_chat(db: &DbPool, chat: &types::Chat) -> Result<Chat, sqlx::Error> {
|
pub async fn find_or_create_chat(db: &DbPool, chat: &types::Chat) -> Result<Chat, sqlx::Error> {
|
||||||
let res: Result<Chat, sqlx::Error> = sqlx::query_as(
|
let res: Result<Chat, sqlx::Error> =
|
||||||
"SELECT * FROM chat WHERE tg_id = $1 LIMIT 1;")
|
sqlx::query_as("SELECT * FROM chat WHERE tg_id = $1 LIMIT 1;")
|
||||||
.bind(chat.id.0 as i64)
|
.bind(chat.id.0 as i64)
|
||||||
.fetch_one(db).await;
|
.fetch_one(db)
|
||||||
|
.await;
|
||||||
|
|
||||||
unwrap_or_create!(db, chat, res, create_chat)
|
unwrap_or_create!(db, chat, res, create_chat)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue