Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caption entities for photos #215

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions raw/src/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub mod send_location;
pub mod send_message;
pub mod send_photo;
pub mod send_poll;
pub mod send_sticker;
pub mod send_venue;
pub mod send_video;
pub mod stop_message_live_location;
Expand Down Expand Up @@ -64,6 +65,7 @@ pub use self::send_location::*;
pub use self::send_message::*;
pub use self::send_photo::*;
pub use self::send_poll::*;
pub use self::send_sticker::*;
pub use self::send_venue::*;
pub use self::send_video::*;
pub use self::stop_message_live_location::*;
Expand Down
111 changes: 111 additions & 0 deletions raw/src/requests/send_sticker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use crate::requests::*;
use crate::types::*;

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[must_use = "requests do nothing unless sent"]
pub struct SendSticker {
chat_id: ChatRef,
sticker: InputFile,
disable_notification: bool,
reply_to_message_id: Option<MessageId>,
reply_markup: Option<ReplyMarkup>,
}

impl ToMultipart for SendSticker {
fn to_multipart(&self) -> Result<Multipart, Error> {
multipart_map! {
self,
(chat_id (text));
(sticker (raw));
(disable_notification (text), when_true);
(reply_to_message_id (text), optional);
(reply_markup (json), optional);
}
}
}

impl Request for SendSticker {
type Type = MultipartRequestType<Self>;
type Response = JsonIdResponse<Message>;

fn serialize(&self) -> Result<HttpRequest, Error> {
Self::Type::serialize(RequestUrl::method("sendSticker"), self)
}
}

impl SendSticker {
pub fn new<C, V>(chat: C, photo: V) -> Self
where
C: ToChatRef,
V: Into<InputFile>,
{
Self {
chat_id: chat.to_chat_ref(),
sticker: photo.into(),
reply_to_message_id: None,
reply_markup: None,
disable_notification: false,
}
}

pub fn reply_to<R>(&mut self, to: R) -> &mut Self
where
R: ToMessageId,
{
self.reply_to_message_id = Some(to.to_message_id());
self
}

pub fn disable_notification(&mut self) -> &mut Self {
self.disable_notification = true;
self
}

pub fn reply_markup<R>(&mut self, reply_markup: R) -> &mut Self
where
R: Into<ReplyMarkup>,
{
self.reply_markup = Some(reply_markup.into());
self
}
}

/// Can reply with an photo
pub trait CanReplySendSticker {
fn sticker_reply<T>(&self, photo: T) -> SendSticker
where
T: Into<InputFile>;
}

impl<M> CanReplySendSticker for M
where
M: ToMessageId + ToSourceChat,
{
fn sticker_reply<T>(&self, photo: T) -> SendSticker
where
T: Into<InputFile>,
{
let mut req = SendSticker::new(self.to_source_chat(), photo);
req.reply_to(self);
req
}
}

/// Send an photo
pub trait CanSendSticker {
fn sticker<T>(&self, photo: T) -> SendSticker
where
T: Into<InputFile>;
}

impl<M> CanSendSticker for M
where
M: ToChatRef,
{
fn sticker<T>(&self, photo: T) -> SendSticker
where
T: Into<InputFile>,
{
SendSticker::new(self.to_chat_ref(), photo)
}
}
31 changes: 29 additions & 2 deletions raw/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub enum MessageKind {
data: Vec<PhotoSize>,
/// Caption for the photo, 0-200 characters.
caption: Option<String>,

caption_entities: Option<Vec<MessageEntity>>,
/// The unique identifier of a media message group this message belongs to.
media_group_id: Option<String>,
},
Expand Down Expand Up @@ -304,6 +306,19 @@ impl Message {
}};
}

macro_rules! maybe_field_with_caption_and_group_and_caption_entities {
($name:ident, $variant:ident) => {{
if let Some(val) = raw.$name {
return make_message(MessageKind::$variant {
data: val,
caption: raw.caption,
caption_entities: raw.entities,
media_group_id: raw.media_group_id,
});
}
}};
}

macro_rules! maybe_true_field {
($name:ident, $variant:ident) => {{
if let Some(True) = raw.$name {
Expand All @@ -322,7 +337,7 @@ impl Message {

maybe_field!(audio, Audio);
maybe_field_with_caption!(document, Document);
maybe_field_with_caption_and_group!(photo, Photo);
maybe_field_with_caption_and_group_and_caption_entities!(photo, Photo);
maybe_field!(sticker, Sticker);
maybe_field_with_caption_and_group!(video, Video);
maybe_field!(voice, Voice);
Expand Down Expand Up @@ -443,6 +458,18 @@ impl ChannelPost {
}};
}

macro_rules! maybe_field_with_caption_and_group_and_caption_entities {
($name:ident, $variant:ident) => {{
if let Some(val) = raw.$name {
return make_message(MessageKind::$variant {
data: val,
caption: raw.caption,
caption_entities: raw.entities,
media_group_id: raw.media_group_id,
});
}
}};
}
macro_rules! maybe_true_field {
($name:ident, $variant:ident) => {{
if let Some(True) = raw.$name {
Expand All @@ -461,7 +488,7 @@ impl ChannelPost {

maybe_field!(audio, Audio);
maybe_field_with_caption!(document, Document);
maybe_field_with_caption_and_group!(photo, Photo);
maybe_field_with_caption_and_group_and_caption_entities!(photo, Photo);
maybe_field!(sticker, Sticker);
maybe_field_with_caption_and_group!(video, Video);
maybe_field!(voice, Voice);
Expand Down