Skip to content

Commit

Permalink
using topic friendly sending everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 22, 2023
1 parent 005534b commit 04349f9
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 56 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/functions/about.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{hooks, utils::keyboard::Keyboard};
use crate::{
hooks,
utils::{keyboard::Keyboard, message::Rustina},
};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand All @@ -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?;
Expand Down
3 changes: 2 additions & 1 deletion src/functions/groups.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::{
groups::{Group, Groups},
keyboard::Keyboard,
message::Rustina,
};
use teloxide::{
payloads::{EditMessageTextSetters, SendMessageSetters},
Expand All @@ -11,7 +12,7 @@ use teloxide::{
static TEXT: &str = "<b>Telegramdagi Rust Hamjamiyatlari yoki Guruhlari:</b>\nAgar o'zingizni guruhingizni qo'shmoqchi bo'lsangiz, bizni <a href='https://github.com/rust-lang-uz/rustina/blob/main/data/communities.json'>community.json</a> 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)
Expand Down
4 changes: 2 additions & 2 deletions src/functions/help.rs
Original file line number Diff line number Diff line change
@@ -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)] = &[
Expand Down Expand Up @@ -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?;
Expand Down
25 changes: 6 additions & 19 deletions src/functions/joined.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use teloxide::{prelude::*, types::*};

use crate::utils::message::{delete_timer, Rustina};

static TEXT: &str = "<b>Salom bo'lajak Rustacean!</b>\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. \
Expand All @@ -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(())
}
4 changes: 2 additions & 2 deletions src/functions/latest.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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?;
Expand Down
9 changes: 5 additions & 4 deletions src/functions/offtop.rs
Original file line number Diff line number Diff line change
@@ -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!";
Expand All @@ -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(())
};
}
Expand All @@ -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(())
};
}
Expand All @@ -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?;
Expand Down
4 changes: 2 additions & 2 deletions src/functions/start.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::keyboard::Keyboard;
use crate::utils::{keyboard::Keyboard, message::Rustina};
use teloxide::{
payloads::SendMessageSetters,
prelude::*,
Expand All @@ -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?;
Expand Down
3 changes: 2 additions & 1 deletion src/functions/useful.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::{
keyboard::Keyboard,
message::Rustina,
resources::{Resource, Resources},
};
use teloxide::{
Expand All @@ -16,7 +17,7 @@ source.json</a> 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)
Expand Down
4 changes: 2 additions & 2 deletions src/functions/version.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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?;
Expand Down
16 changes: 6 additions & 10 deletions src/hooks/is_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "⚠️ <b>Bu komanda faqat shaxsiy chat uchun!</b>";

Expand All @@ -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(())
}
44 changes: 33 additions & 11 deletions src/utils/message.rs
Original file line number Diff line number Diff line change
@@ -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<Payload = SendMessage, Err = Self::Err>;
Expand All @@ -11,25 +18,40 @@ pub trait Rustina {
T: Into<String>;
}

#[async_trait]
impl Rustina for Bot {
type Err = teloxide::errors::RequestError;
type SendMessageTF = JsonRequest<teloxide::payloads::SendMessage>;
type SendMessageTF = JsonRequest<payloads::SendMessage>;

fn send_message_tf<C, T>(&self, chat_id: C, text: T, message: &Message) -> Self::SendMessageTF
where
C: Into<Recipient>,
T: Into<String>,
{
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(())
}

0 comments on commit 04349f9

Please sign in to comment.