Skip to content

Commit

Permalink
topic frinedly check && username validation
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 22, 2023
1 parent ed302c9 commit 005534b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/functions/check.rs
Original file line number Diff line number Diff line change
@@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion src/functions/offtop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
23 changes: 15 additions & 8 deletions src/utils/message.rs
Original file line number Diff line number Diff line change
@@ -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<Payload = SendMessage, Err = Self::Err>;

/// For Telegram documentation see [`SendMessage`].
fn send_message_tf<C, T>(&self, chat_id: C, text: T) -> Self::SendMessageTF
fn send_message_tf<C, T>(&self, chat_id: C, text: T, message: &Message) -> Self::SendMessageTF
where
C: Into<Recipient>,
T: Into<String>;
}

impl RequestTopicFriendly for Bot {
impl Rustina for Bot {
type Err = teloxide::errors::RequestError;
type SendMessageTF = JsonRequest<teloxide::payloads::SendMessage>;

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

0 comments on commit 005534b

Please sign in to comment.