telegram_bot_raw/requests/
send_document.rs

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