telegram_bot_raw/requests/
send_photo.rs

1use std::borrow::Cow;
2
3use crate::requests::*;
4use crate::types::*;
5
6/// Use this method to send photos
7#[derive(Debug, Clone, PartialEq, PartialOrd)]
8#[must_use = "requests do nothing unless sent"]
9pub struct SendPhoto<'c> {
10    chat_id: ChatRef,
11    photo: InputFile,
12    caption: Option<Cow<'c, str>>,
13    parse_mode: Option<ParseMode>,
14    reply_to_message_id: Option<MessageId>,
15    disable_notification: bool,
16    reply_markup: Option<ReplyMarkup>,
17}
18
19impl<'c> ToMultipart for SendPhoto<'c> {
20    fn to_multipart(&self) -> Result<Multipart, Error> {
21        multipart_map! {
22            self,
23            (chat_id (text));
24            (photo (raw));
25            (caption (text), optional);
26            (parse_mode (text), optional);
27            (reply_to_message_id (text), optional);
28            (disable_notification (text), when_true);
29            (reply_markup (json), optional);
30        }
31    }
32}
33
34impl<'c> Request for SendPhoto<'c> {
35    type Type = MultipartRequestType<Self>;
36    type Response = JsonIdResponse<Message>;
37
38    fn serialize(&self) -> Result<HttpRequest, Error> {
39        Self::Type::serialize(RequestUrl::method("sendPhoto"), self)
40    }
41}
42
43impl<'c> SendPhoto<'c> {
44    pub fn new<C, V>(chat: C, photo: V) -> Self
45    where
46        C: ToChatRef,
47        V: Into<InputFile>,
48    {
49        Self {
50            chat_id: chat.to_chat_ref(),
51            photo: photo.into(),
52            caption: None,
53            parse_mode: None,
54            reply_to_message_id: None,
55            reply_markup: None,
56            disable_notification: false,
57        }
58    }
59
60    pub fn caption<T>(&mut self, caption: T) -> &mut Self
61    where
62        T: Into<Cow<'c, str>>,
63    {
64        self.caption = Some(caption.into());
65        self
66    }
67
68    pub fn parse_mode(&mut self, parse_mode: ParseMode) -> &mut Self {
69        self.parse_mode = Some(parse_mode);
70        self
71    }
72
73    pub fn reply_to<R>(&mut self, to: R) -> &mut Self
74    where
75        R: ToMessageId,
76    {
77        self.reply_to_message_id = Some(to.to_message_id());
78        self
79    }
80
81    pub fn disable_notification(&mut self) -> &mut Self {
82        self.disable_notification = true;
83        self
84    }
85
86    pub fn reply_markup<R>(&mut self, reply_markup: R) -> &mut Self
87    where
88        R: Into<ReplyMarkup>,
89    {
90        self.reply_markup = Some(reply_markup.into());
91        self
92    }
93}
94
95/// Can reply with an photo
96pub trait CanReplySendPhoto {
97    fn photo_reply<'c, T>(&self, photo: T) -> SendPhoto<'c>
98    where
99        T: Into<InputFile>;
100}
101
102impl<M> CanReplySendPhoto for M
103where
104    M: ToMessageId + ToSourceChat,
105{
106    fn photo_reply<'c, T>(&self, photo: T) -> SendPhoto<'c>
107    where
108        T: Into<InputFile>,
109    {
110        let mut req = SendPhoto::new(self.to_source_chat(), photo);
111        req.reply_to(self);
112        req
113    }
114}
115
116/// Send an photo
117pub trait CanSendPhoto {
118    fn photo<'c, T>(&self, photo: T) -> SendPhoto<'c>
119    where
120        T: Into<InputFile>;
121}
122
123impl<M> CanSendPhoto for M
124where
125    M: ToChatRef,
126{
127    fn photo<'c, T>(&self, photo: T) -> SendPhoto<'c>
128    where
129        T: Into<InputFile>,
130    {
131        SendPhoto::new(self.to_chat_ref(), photo)
132    }
133}