From 005534becbcaa30c011156c47b0f2d380fe2f797 Mon Sep 17 00:00:00 2001 From: Sokhibjon Orzikulov Date: Wed, 22 Nov 2023 19:50:56 +0500 Subject: [PATCH] topic frinedly check && username validation --- src/functions/check.rs | 5 ++--- src/functions/offtop.rs | 2 +- src/utils/message.rs | 23 +++++++++++++++-------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/functions/check.rs b/src/functions/check.rs index 5bc519f..a83488b 100644 --- a/src/functions/check.rs +++ b/src/functions/check.rs @@ -1,10 +1,9 @@ +use crate::utils::message::Rustina; use teloxide::{payloads::SendMessageSetters, prelude::*, types::ParseMode}; pub async fn command(bot: &Bot, msg: &Message) -> ResponseResult<()> { - println!("Command triggered: {:?}", msg); - bot.send_message(msg.chat.id, view(msg)) + bot.send_message_tf(msg.chat.id, view(msg), msg) .parse_mode(ParseMode::Html) - // .reply_markup(keyboard()) .await?; Ok(()) diff --git a/src/functions/offtop.rs b/src/functions/offtop.rs index 7dd372d..7e2b840 100644 --- a/src/functions/offtop.rs +++ b/src/functions/offtop.rs @@ -14,7 +14,7 @@ pub async fn command(bot: &Bot, msg: &Message, me: &Me) -> ResponseResult<()> { // if replied person is bot itself, send fail message if let Some(user) = msg.reply_to_message().as_ref().unwrap().from() { - if user.username.clone().unwrap() == me.username() { + if user.username.is_some() && user.username.clone().unwrap() == me.username() { return { bot.send_message(msg.chat.id, TEXT_FAIL).await?; Ok(()) diff --git a/src/utils/message.rs b/src/utils/message.rs index 371a600..2cc1fa7 100644 --- a/src/utils/message.rs +++ b/src/utils/message.rs @@ -1,28 +1,35 @@ use teloxide::{payloads::*, prelude::*, requests::JsonRequest, types::*}; -trait RequestTopicFriendly { +pub trait Rustina { type Err: std::error::Error + Send; type SendMessageTF: Request; /// For Telegram documentation see [`SendMessage`]. - fn send_message_tf(&self, chat_id: C, text: T) -> Self::SendMessageTF + fn send_message_tf(&self, chat_id: C, text: T, message: &Message) -> Self::SendMessageTF where C: Into, T: Into; } -impl RequestTopicFriendly for Bot { +impl Rustina for Bot { type Err = teloxide::errors::RequestError; type SendMessageTF = JsonRequest; - fn send_message_tf(&self, chat_id: C, text: T) -> Self::SendMessageTF + fn send_message_tf(&self, chat_id: C, text: T, message: &Message) -> Self::SendMessageTF where C: Into, T: Into, { - Self::SendMessageTF::new( - self.clone(), - teloxide::payloads::SendMessage::new(chat_id, text), - ) + match message.thread_id { + Some(thread_id) => Self::SendMessageTF::new( + self.clone(), + teloxide::payloads::SendMessage::new(chat_id, text), + ) + .message_thread_id(thread_id), + None => Self::SendMessageTF::new( + self.clone(), + teloxide::payloads::SendMessage::new(chat_id, text), + ), + } } }