From 04349f94d49911f0e4c4430252414051cc825961 Mon Sep 17 00:00:00 2001 From: Sokhibjon Orzikulov Date: Wed, 22 Nov 2023 23:17:15 +0500 Subject: [PATCH] using topic friendly sending everywhere --- Cargo.lock | 1 + Cargo.toml | 1 + src/functions/about.rs | 7 +++++-- src/functions/groups.rs | 3 ++- src/functions/help.rs | 4 ++-- src/functions/joined.rs | 25 ++++++----------------- src/functions/latest.rs | 4 ++-- src/functions/offtop.rs | 9 ++++---- src/functions/start.rs | 4 ++-- src/functions/useful.rs | 3 ++- src/functions/version.rs | 4 ++-- src/hooks/is_private.rs | 16 ++++++--------- src/utils/message.rs | 44 ++++++++++++++++++++++++++++++---------- 13 files changed, 69 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f5aea19..20bc077 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1269,6 +1269,7 @@ dependencies = [ name = "rustina" version = "0.1.0" dependencies = [ + "async-trait", "crates_io_api", "log", "octocrab", diff --git a/Cargo.toml b/Cargo.toml index a121c92..86c9198 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ uuid = { version = "1.5.0", features = ["v4"] } serde_json = "1.0.108" serde = { version = "1.0.192", features = ["derive"] } octocrab = { version = "0.32.0" } +async-trait = "0.1.74" [profile.release] strip = true diff --git a/src/functions/about.rs b/src/functions/about.rs index 29e464e..db7fb63 100644 --- a/src/functions/about.rs +++ b/src/functions/about.rs @@ -1,4 +1,7 @@ -use crate::{hooks, utils::keyboard::Keyboard}; +use crate::{ + hooks, + utils::{keyboard::Keyboard, message::Rustina}, +}; use teloxide::{ payloads::SendMessageSetters, prelude::*, @@ -19,7 +22,7 @@ pub async fn command(bot: &Bot, msg: &Message) -> ResponseResult<()> { }; } - bot.send_message(msg.chat.id, TEXT) + bot.send_message_tf(msg.chat.id, TEXT, msg) .parse_mode(ParseMode::Html) .reply_markup(keyboard()) .await?; diff --git a/src/functions/groups.rs b/src/functions/groups.rs index f1a4ea7..9a54523 100644 --- a/src/functions/groups.rs +++ b/src/functions/groups.rs @@ -1,6 +1,7 @@ use crate::utils::{ groups::{Group, Groups}, keyboard::Keyboard, + message::Rustina, }; use teloxide::{ payloads::{EditMessageTextSetters, SendMessageSetters}, @@ -11,7 +12,7 @@ use teloxide::{ static TEXT: &str = "Telegramdagi Rust Hamjamiyatlari yoki Guruhlari:\nAgar o'zingizni guruhingizni qo'shmoqchi bo'lsangiz, bizni community.json ni yangilang!"; pub async fn command(bot: &Bot, msg: &Message, groups: &Groups) -> ResponseResult<()> { - bot.send_message(msg.chat.id, TEXT) + bot.send_message_tf(msg.chat.id, TEXT, msg) .parse_mode(ParseMode::Html) .reply_markup(keyboard_list(groups, 1)) .disable_web_page_preview(true) diff --git a/src/functions/help.rs b/src/functions/help.rs index 4df89a9..a52a288 100644 --- a/src/functions/help.rs +++ b/src/functions/help.rs @@ -1,5 +1,5 @@ use super::start::keyboard; -use crate::Command; +use crate::{utils::message::Rustina, Command}; use teloxide::{payloads::SendMessageSetters, prelude::*, types::ParseMode}; static TEXT: &[(&str, &str)] = &[ @@ -30,7 +30,7 @@ pub async fn command(bot: &Bot, msg: &Message, _cmd: &Command) -> ResponseResult text.push('\n'); } - bot.send_message(msg.chat.id, text) + bot.send_message_tf(msg.chat.id, text, msg) .parse_mode(ParseMode::Html) .reply_markup(keyboard()) .await?; diff --git a/src/functions/joined.rs b/src/functions/joined.rs index 1b58d4e..d6b83cd 100644 --- a/src/functions/joined.rs +++ b/src/functions/joined.rs @@ -1,5 +1,7 @@ use teloxide::{prelude::*, types::*}; +use crate::utils::message::{delete_timer, Rustina}; + static TEXT: &str = "Salom bo'lajak Rustacean!\n\n\ Sizlarni bu guruhda ko'rib turganimizdan mamnunmiz. Bu guruh Rust dasturlash tiliga qaratilgan hisoblanib, \ bu yerda ushbu til haqida gaplashish, savollar berish yoki o'z fikrlaringiz bilan bo'lishishingiz mumkin. \ @@ -9,26 +11,11 @@ maslahatlar, va hamda /useful yordamoda foydali resurslar olishingiz mumkin. pub async fn trigger(bot: &Bot, msg: &Message) -> ResponseResult<()> { let message = bot - .send_message(msg.chat.id, TEXT) - .parse_mode(ParseMode::Html); - - let message: Message = if msg.thread_id.is_some() { - message - .message_thread_id(msg.thread_id.unwrap()) - .send() - .await? - } else { - message.send().await? - }; + .send_message_tf(msg.chat.id, TEXT, msg) + .parse_mode(ParseMode::Html) + .await?; - let thread_bot = bot.clone(); - tokio::spawn(async move { - tokio::time::sleep(tokio::time::Duration::from_secs(60 * 5)).await; - match thread_bot.delete_message(message.chat.id, message.id).await { - Ok(_) => {} - Err(_) => {} - }; - }); + delete_timer(bot, &message, 60 * 5).await?; Ok(()) } diff --git a/src/functions/latest.rs b/src/functions/latest.rs index 2b3fc82..8935a26 100644 --- a/src/functions/latest.rs +++ b/src/functions/latest.rs @@ -1,4 +1,4 @@ -use crate::utils::{github::GitHub, keyboard::Keyboard}; +use crate::utils::{github::GitHub, keyboard::Keyboard, message::Rustina}; use octocrab::models::repos::Release; use teloxide::{ payloads::SendMessageSetters, @@ -9,7 +9,7 @@ use teloxide::{ pub async fn command(bot: &Bot, github: GitHub, msg: &Message) -> ResponseResult<()> { let latest = github.get_latest().await.unwrap(); - bot.send_message(msg.chat.id, view(&latest)) + bot.send_message_tf(msg.chat.id, view(&latest), msg) .parse_mode(ParseMode::Html) .reply_markup(keyboard(&latest)) .await?; diff --git a/src/functions/offtop.rs b/src/functions/offtop.rs index 7e2b840..d71e4c9 100644 --- a/src/functions/offtop.rs +++ b/src/functions/offtop.rs @@ -1,4 +1,4 @@ -use crate::utils::keyboard::Keyboard; +use crate::utils::{keyboard::Keyboard, message::Rustina}; use teloxide::{prelude::*, types::*}; static TEXT_FAIL: &str = "Ha-ha... yaxshi urinish!"; @@ -7,7 +7,8 @@ static TEXT_NON_REPLY: &str = "↪ Reply bilan ko'rsatingchi habarni!"; pub async fn command(bot: &Bot, msg: &Message, me: &Me) -> ResponseResult<()> { if msg.reply_to_message().is_none() { return { - bot.send_message(msg.chat.id, TEXT_NON_REPLY).await?; + bot.send_message_tf(msg.chat.id, TEXT_NON_REPLY, msg) + .await?; Ok(()) }; } @@ -16,7 +17,7 @@ pub async fn command(bot: &Bot, msg: &Message, me: &Me) -> ResponseResult<()> { if let Some(user) = msg.reply_to_message().as_ref().unwrap().from() { if user.username.is_some() && user.username.clone().unwrap() == me.username() { return { - bot.send_message(msg.chat.id, TEXT_FAIL).await?; + bot.send_message_tf(msg.chat.id, TEXT_FAIL, msg).await?; Ok(()) }; } @@ -26,7 +27,7 @@ pub async fn command(bot: &Bot, msg: &Message, me: &Me) -> ResponseResult<()> { bot.delete_message(msg.chat.id, msg.reply_to_message().unwrap().id) .await?; - bot.send_message(msg.chat.id, view(msg.reply_to_message().unwrap())) + bot.send_message_tf(msg.chat.id, view(msg.reply_to_message().unwrap()), msg) .parse_mode(ParseMode::Html) .reply_markup(keyboard()) .await?; diff --git a/src/functions/start.rs b/src/functions/start.rs index 832d5b7..2a114fa 100644 --- a/src/functions/start.rs +++ b/src/functions/start.rs @@ -1,4 +1,4 @@ -use crate::utils::keyboard::Keyboard; +use crate::utils::{keyboard::Keyboard, message::Rustina}; use teloxide::{ payloads::SendMessageSetters, prelude::*, @@ -12,7 +12,7 @@ Sizni ko'rib turganimdan bag'oyatda xursandman. Men O'zbek Rust jamiyati tomonid "#; pub async fn command(bot: &Bot, msg: &Message) -> ResponseResult<()> { - bot.send_message(msg.chat.id, TEXT) + bot.send_message_tf(msg.chat.id, TEXT, msg) .parse_mode(ParseMode::Html) .reply_markup(keyboard()) .await?; diff --git a/src/functions/useful.rs b/src/functions/useful.rs index 99ae364..135294a 100644 --- a/src/functions/useful.rs +++ b/src/functions/useful.rs @@ -1,5 +1,6 @@ use crate::utils::{ keyboard::Keyboard, + message::Rustina, resources::{Resource, Resources}, }; use teloxide::{ @@ -16,7 +17,7 @@ source.json ni yangilang!"; pub async fn command(bot: &Bot, msg: &Message, resources: &Resources) -> ResponseResult<()> { let categories = resources.get_keys(); - bot.send_message(msg.chat.id, TEXT) + bot.send_message_tf(msg.chat.id, TEXT, msg) .parse_mode(ParseMode::Html) .reply_markup(keyboard_list(categories)) .disable_web_page_preview(true) diff --git a/src/functions/version.rs b/src/functions/version.rs index b1d4559..62452ed 100644 --- a/src/functions/version.rs +++ b/src/functions/version.rs @@ -1,4 +1,4 @@ -use crate::utils::{github::GitHub, keyboard::Keyboard}; +use crate::utils::{github::GitHub, keyboard::Keyboard, message::Rustina}; use octocrab::models::repos::Release; use teloxide::{ payloads::SendMessageSetters, @@ -12,7 +12,7 @@ pub async fn command(bot: &Bot, github: GitHub, msg: &Message) -> ResponseResult let versions = github.get_list(1).await.unwrap(); let next_page = github.get_list(2).await.unwrap(); - bot.send_message(msg.chat.id, TEXT) + bot.send_message_tf(msg.chat.id, TEXT, msg) .parse_mode(ParseMode::Html) .reply_markup(keyboard_list(1, versions, Some(next_page))) .await?; diff --git a/src/hooks/is_private.rs b/src/hooks/is_private.rs index fcb520f..c04d289 100644 --- a/src/hooks/is_private.rs +++ b/src/hooks/is_private.rs @@ -4,7 +4,10 @@ use teloxide::{ types::{InlineKeyboardMarkup, ParseMode}, }; -use crate::utils::keyboard::Keyboard; +use crate::utils::{ + keyboard::Keyboard, + message::{delete_timer, Rustina}, +}; static TEXT: &str = "⚠️ Bu komanda faqat shaxsiy chat uchun!"; @@ -20,19 +23,12 @@ pub async fn is_private(bot: &Bot, msg: &Message) -> ResponseResult<()> { }; let message = bot - .send_message(msg.chat.id, TEXT) + .send_message_tf(msg.chat.id, TEXT, msg) .parse_mode(ParseMode::Html) .reply_markup(keyboard()) .await?; - let thread_bot = bot.clone(); - tokio::spawn(async move { - tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; - match thread_bot.delete_message(message.chat.id, message.id).await { - Ok(_) => {} - Err(_) => {} - }; - }); + delete_timer(bot, &message, 10).await?; Ok(()) } diff --git a/src/utils/message.rs b/src/utils/message.rs index 2cc1fa7..8121267 100644 --- a/src/utils/message.rs +++ b/src/utils/message.rs @@ -1,5 +1,12 @@ -use teloxide::{payloads::*, prelude::*, requests::JsonRequest, types::*}; +use async_trait::async_trait; +use teloxide::{ + prelude::*, + requests::JsonRequest, + types::*, + {payloads, payloads::*}, +}; +#[async_trait] pub trait Rustina { type Err: std::error::Error + Send; type SendMessageTF: Request; @@ -11,9 +18,10 @@ pub trait Rustina { T: Into; } +#[async_trait] impl Rustina for Bot { type Err = teloxide::errors::RequestError; - type SendMessageTF = JsonRequest; + type SendMessageTF = JsonRequest; fn send_message_tf(&self, chat_id: C, text: T, message: &Message) -> Self::SendMessageTF where @@ -21,15 +29,29 @@ impl Rustina for Bot { T: Into, { 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), - ), + Some(thread_id) => { + Self::SendMessageTF::new(self.clone(), payloads::SendMessage::new(chat_id, text)) + .message_thread_id(thread_id) + } + None => { + Self::SendMessageTF::new(self.clone(), payloads::SendMessage::new(chat_id, text)) + } } } } + +// Delete a message after a certain time +pub async fn delete_timer(bot: &Bot, message: &Message, timer: u64) -> ResponseResult<()> { + let bot = bot.clone(); + let message = message.clone(); + + tokio::spawn(async move { + tokio::time::sleep(tokio::time::Duration::from_secs(timer)).await; + match bot.delete_message(message.chat.id, message.id).await { + Ok(_) => {} + Err(_) => {} + }; + }); + + Ok(()) +}