From cc0f2a73a8e412ac22084cd74ab57860e4e61689 Mon Sep 17 00:00:00 2001 From: LasterAlex Date: Fri, 30 Aug 2024 06:28:39 +0300 Subject: [PATCH] Added link preview sugar --- crates/teloxide/src/sugar/request.rs | 51 +++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/crates/teloxide/src/sugar/request.rs b/crates/teloxide/src/sugar/request.rs index a3b82091..dc31ae50 100644 --- a/crates/teloxide/src/sugar/request.rs +++ b/crates/teloxide/src/sugar/request.rs @@ -11,7 +11,7 @@ macro_rules! impl_request_reply_ext { fn reply_to(self, message_id: M) -> Self where M: Into, - Self: teloxide_core::requests::HasPayload + Sized, + Self: Sized, { 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 { fn reply_to(self, message_id: M) -> Self where @@ -27,10 +49,20 @@ pub trait RequestReplyExt { Self: Sized; } +pub trait RequestLinkPreviewExt { + fn disable_link_preview(self) -> Self + where + Self: Sized; +} + impl_request_reply_ext! { ::SendMessage } +impl_request_link_preview_ext! { + ::SendMessage +} + #[cfg(test)] mod tests { use std::ops::Deref; @@ -48,4 +80,21 @@ mod tests { 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()) + } }