Added link preview sugar

This commit is contained in:
LasterAlex 2024-08-30 06:28:39 +03:00
parent 19ae8dd7a9
commit cc0f2a73a8
No known key found for this signature in database

View file

@ -11,7 +11,7 @@ macro_rules! impl_request_reply_ext {
fn reply_to<M>(self, message_id: M) -> Self fn reply_to<M>(self, message_id: M) -> Self
where where
M: Into<MessageId>, M: Into<MessageId>,
Self: teloxide_core::requests::HasPayload + Sized, Self: Sized,
{ {
self.reply_parameters(ReplyParameters::new(message_id.into())) self.reply_parameters(ReplyParameters::new(message_id.into()))
} }
@ -20,6 +20,28 @@ macro_rules! impl_request_reply_ext {
}; };
} }
macro_rules! impl_request_link_preview_ext {
($($t:ty),*) => {
$(
impl RequestLinkPreviewExt for $t {
fn disable_link_preview(self) -> Self
where
Self: Sized
{
let link_preview_options = LinkPreviewOptions {
is_disabled: true,
url: None,
prefer_small_media: false,
prefer_large_media: false,
show_above_text: false,
};
self.link_preview_options(link_preview_options)
}
}
)*
};
}
pub trait RequestReplyExt { pub trait RequestReplyExt {
fn reply_to<M>(self, message_id: M) -> Self fn reply_to<M>(self, message_id: M) -> Self
where where
@ -27,10 +49,20 @@ pub trait RequestReplyExt {
Self: Sized; Self: Sized;
} }
pub trait RequestLinkPreviewExt {
fn disable_link_preview(self) -> Self
where
Self: Sized;
}
impl_request_reply_ext! { impl_request_reply_ext! {
<Bot as Requester>::SendMessage <Bot as Requester>::SendMessage
} }
impl_request_link_preview_ext! {
<Bot as Requester>::SendMessage
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::ops::Deref; use std::ops::Deref;
@ -48,4 +80,21 @@ mod tests {
assert_eq!(real_reply_req.deref(), sugar_reply_req.deref()) assert_eq!(real_reply_req.deref(), sugar_reply_req.deref())
} }
#[test]
fn test_disable_link_preview() {
let link_preview_options = LinkPreviewOptions {
is_disabled: true,
url: None,
prefer_small_media: false,
prefer_large_media: false,
show_above_text: false,
};
let bot = Bot::new("TOKEN");
let real_link_req =
bot.send_message(ChatId(1234), "test").link_preview_options(link_preview_options);
let sugar_link_req = bot.send_message(ChatId(1234), "test").disable_link_preview();
assert_eq!(real_link_req.deref(), sugar_link_req.deref())
}
} }