telbot_types/
file.rs

1use serde::{Deserialize, Serialize};
2
3use crate::markup::{MessageEntity, ParseMode};
4use crate::{JsonMethod, TelegramMethod};
5
6/// This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound).
7#[derive(Debug, Deserialize)]
8pub struct Animation {
9    /// Identifier for this file, which can be used to download or reuse the file
10    pub file_id: String,
11    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
12    /// Can't be used to download or reuse the file.
13    pub file_unique_id: String,
14    /// Video width as defined by sender
15    pub width: usize,
16    /// Video height as defined by sender
17    pub height: usize,
18    /// Duration of the video in seconds as defined by sender
19    pub duration: u32,
20    /// Animation thumbnail as defined by sender
21    pub thumb: Option<PhotoSize>,
22    /// Original animation filename as defined by sender
23    pub file_name: Option<String>,
24    /// MIME type of the file as defined by sender
25    pub mime_type: Option<String>,
26    /// File size
27    pub file_size: Option<usize>,
28}
29
30/// This object represents an audio file to be treated as music by the Telegram clients.
31#[derive(Debug, Deserialize)]
32pub struct Audio {
33    /// Identifier for this file, which can be used to download or reuse the file
34    pub file_id: String,
35    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
36    /// Can't be used to download or reuse the file.
37    pub file_unique_id: String,
38    /// Duration of the audio in seconds as defined by sender
39    pub duration: u32,
40    /// Performer of the audio as defined by sender or by audio tags
41    pub performer: Option<String>,
42    /// Title of the audio as defined by sender or by audio tags
43    pub title: Option<String>,
44    /// Original filename as defined by sender
45    pub file_name: Option<String>,
46    /// MIME type of the file as defined by sender
47    pub mime_type: Option<String>,
48    /// File size
49    pub file_size: Option<usize>,
50    /// Thumbnail of the album cover to which the music file belongs
51    pub thumb: Option<PhotoSize>,
52}
53
54/// This object represents a general file (as opposed to
55/// [photos](https://core.telegram.org/bots/api#photosize),
56/// [voice messages](https://core.telegram.org/bots/api#voice) and
57/// [audio files](https://core.telegram.org/bots/api#audio)).
58#[derive(Debug, Deserialize)]
59pub struct Document {
60    /// Identifier for this file, which can be used to download or reuse the file
61    pub file_id: String,
62    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
63    /// Can't be used to download or reuse the file.
64    pub file_unique_id: String,
65    /// Document thumbnail as defined by sender
66    pub thumb: Option<PhotoSize>,
67    /// Original filename as defined by sender
68    pub file_name: Option<String>,
69    /// MIME type of the file as defined by sender
70    pub mime_type: Option<String>,
71    /// File size
72    pub file_size: Option<usize>,
73}
74
75/// This object represents one size of a photo or a
76/// [file](https://core.telegram.org/bots/api#document) /
77/// [sticker](https://core.telegram.org/bots/api#sticker) thumbnail.
78#[derive(Debug, Deserialize)]
79pub struct PhotoSize {
80    /// Identifier for this file, which can be used to download or reuse the file
81    pub file_id: String,
82    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
83    /// Can't be used to download or reuse the file.
84    pub file_unique_id: String,
85    /// Photo width
86    pub width: u32,
87    /// Photo height
88    pub height: u32,
89    /// File size
90    pub file_size: u32,
91}
92
93/// This object represents a video file.
94#[derive(Debug, Deserialize)]
95pub struct Video {
96    /// Identifier for this file, which can be used to download or reuse the file
97    pub file_id: String,
98    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
99    /// Can't be used to download or reuse the file.
100    pub file_unique_id: String,
101    /// Video width as defined by sender
102    pub width: u32,
103    /// Video height as defined by sender
104    pub height: u32,
105    /// Duration of the video in seconds as defined by sender
106    pub duration: u32,
107    /// Video thumbnail
108    pub thumb: Option<PhotoSize>,
109    /// Original animation filename as defined by sender
110    pub file_name: Option<String>,
111    /// MIME type of the file as defined by sender
112    pub mime_type: Option<String>,
113    /// File size
114    pub file_size: Option<u32>,
115}
116
117/// This object represents a [video message](https://telegram.org/blog/video-messages-and-telescope)
118/// (available in Telegram apps as of [v.4.0](https://telegram.org/blog/video-messages-and-telescope)).
119#[derive(Debug, Deserialize)]
120pub struct VideoNote {
121    /// Identifier for this file, which can be used to download or reuse the file
122    pub file_id: String,
123    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
124    /// Can't be used to download or reuse the file.
125    pub file_unique_id: String,
126    /// Video width and height (diameter of the video message) as defined by sender
127    pub length: u32,
128    /// Duration of the video in seconds as defined by sender
129    pub duration: u32,
130    /// Video thumbnail
131    pub thumb: Option<PhotoSize>,
132    /// File size
133    pub file_size: Option<u32>,
134}
135
136/// This object represents a voice note.
137#[derive(Debug, Deserialize)]
138pub struct Voice {
139    /// Identifier for this file, which can be used to download or reuse the file
140    pub file_id: String,
141    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
142    /// Can't be used to download or reuse the file.
143    pub file_unique_id: String,
144    /// Duration of the audio in seconds as defined by sender
145    pub duration: u32,
146    /// MIME type of the file as defined by sender
147    pub mime_type: Option<String>,
148    /// File size
149    pub file_size: Option<u32>,
150}
151
152/// This object represents a file ready to be downloaded.
153///
154/// The file can be downloaded via the link `https://api.telegram.org/file/bot<token>/<file_path>`.
155/// It is guaranteed that the link will be valid for at least 1 hour.
156/// When the link expires, a new one can be requested by calling [getFile](https://core.telegram.org/bots/api#getfile).
157#[derive(Debug, Deserialize)]
158pub struct File {
159    /// Identifier for this file, which can be used to download or reuse the file
160    pub file_id: String,
161    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
162    /// Can't be used to download or reuse the file.
163    pub file_unique_id: String,
164    /// File size, if known
165    pub file_size: Option<u32>,
166    /// File path. Use `https://api.telegram.org/file/bot<token>/<file_path>` to get the file.
167    pub file_path: Option<String>,
168}
169
170/// This object represents the content of a media message to be sent.
171/// It should be one of
172/// - InputMediaAnimation
173/// - InputMediaDocument
174/// - InputMediaAudio
175/// - InputMediaPhoto
176/// - InputMediaVideo
177#[derive(Clone, Serialize)]
178#[serde(rename_all = "snake_case", tag = "type")]
179pub enum InputMedia {
180    /// Represents a photo to be sent.
181    Photo {
182        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
183        /// pass an HTTP URL for Telegram to get a file from the Internet,
184        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
185        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
186        media: String,
187        /// Caption of the photo to be sent, 0-1024 characters after entities parsing
188        #[serde(skip_serializing_if = "Option::is_none")]
189        caption: Option<String>,
190        /// Mode for parsing entities in the photo caption.
191        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
192        #[serde(skip_serializing_if = "Option::is_none")]
193        parse_mode: Option<ParseMode>,
194        /// List of special entities that appear in the caption,
195        /// which can be specified instead of parse_mode
196        #[serde(skip_serializing_if = "Option::is_none")]
197        caption_entities: Option<Vec<MessageEntity>>,
198    },
199    /// Represents a video to be sent.
200    Video {
201        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
202        /// pass an HTTP URL for Telegram to get a file from the Internet,
203        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
204        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
205        media: String,
206        /// Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side.
207        /// The thumbnail should be in JPEG format and less than 200 kB in size.
208        /// A thumbnail's width and height should not exceed 320.
209        /// Ignored if the file is not uploaded using multipart/form-data.
210        /// Thumbnails can't be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>.
211        /// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
212        #[serde(skip_serializing_if = "Option::is_none")]
213        thumb: Option<InputFileVariant>,
214        /// Video width
215        #[serde(skip_serializing_if = "Option::is_none")]
216        width: Option<u32>,
217        /// Video height
218        #[serde(skip_serializing_if = "Option::is_none")]
219        height: Option<u32>,
220        /// Video duration
221        #[serde(skip_serializing_if = "Option::is_none")]
222        duration: Option<u32>,
223        /// Pass True, if the uploaded video is suitable for streaming
224        supports_streaming: Option<bool>,
225        /// Caption of the video to be sent, 0-1024 characters after entities parsing
226        #[serde(skip_serializing_if = "Option::is_none")]
227        caption: Option<String>,
228        /// Mode for parsing entities in the video caption.
229        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
230        #[serde(skip_serializing_if = "Option::is_none")]
231        parse_mode: Option<ParseMode>,
232        /// List of special entities that appear in the caption,
233        /// which can be specified instead of *parse_mode*
234        #[serde(skip_serializing_if = "Option::is_none")]
235        caption_entities: Option<Vec<MessageEntity>>,
236    },
237    /// Represents an animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent.
238    Animation {
239        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
240        /// pass an HTTP URL for Telegram to get a file from the Internet,
241        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
242        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
243        media: String,
244        /// Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side.
245        /// The thumbnail should be in JPEG format and less than 200 kB in size.
246        /// A thumbnail's width and height should not exceed 320.
247        /// Ignored if the file is not uploaded using multipart/form-data.
248        /// Thumbnails can't be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>.
249        /// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
250        #[serde(skip_serializing_if = "Option::is_none")]
251        thumb: Option<InputFileVariant>,
252        /// Animation width
253        #[serde(skip_serializing_if = "Option::is_none")]
254        width: Option<u32>,
255        /// Animation height
256        #[serde(skip_serializing_if = "Option::is_none")]
257        height: Option<u32>,
258        /// Animation duration
259        #[serde(skip_serializing_if = "Option::is_none")]
260        duration: Option<u32>,
261        /// Caption of the animation to be sent, 0-1024 characters after entities parsing
262        #[serde(skip_serializing_if = "Option::is_none")]
263        caption: Option<String>,
264        /// Mode for parsing entities in the animation caption.
265        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
266        #[serde(skip_serializing_if = "Option::is_none")]
267        parse_mode: Option<ParseMode>,
268        /// List of special entities that appear in the caption,
269        /// which can be specified instead of *parse_mode*
270        #[serde(skip_serializing_if = "Option::is_none")]
271        caption_entities: Option<Vec<MessageEntity>>,
272    },
273    /// Represents an audio file to be treated as music to be sent.
274    Audio {
275        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
276        /// pass an HTTP URL for Telegram to get a file from the Internet,
277        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
278        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
279        media: String,
280        /// Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side.
281        /// The thumbnail should be in JPEG format and less than 200 kB in size.
282        /// A thumbnail's width and height should not exceed 320.
283        /// Ignored if the file is not uploaded using multipart/form-data.
284        /// Thumbnails can't be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>.
285        /// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
286        #[serde(skip_serializing_if = "Option::is_none")]
287        thumb: Option<InputFileVariant>,
288        /// Performer of the audio
289        #[serde(skip_serializing_if = "Option::is_none")]
290        performer: Option<String>,
291        /// Title of the audio
292        #[serde(skip_serializing_if = "Option::is_none")]
293        title: Option<String>,
294        /// Duration of the audio in seconds
295        #[serde(skip_serializing_if = "Option::is_none")]
296        duration: Option<u32>,
297        /// Caption of the audio to be sent, 0-1024 characters after entities parsing
298        #[serde(skip_serializing_if = "Option::is_none")]
299        caption: Option<String>,
300        /// Mode for parsing entities in the audio caption.
301        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
302        #[serde(skip_serializing_if = "Option::is_none")]
303        parse_mode: Option<ParseMode>,
304        /// List of special entities that appear in the caption,
305        /// which can be specified instead of *parse_mode*
306        #[serde(skip_serializing_if = "Option::is_none")]
307        caption_entities: Option<Vec<MessageEntity>>,
308    },
309    /// Represents a general file to be sent.
310    Document {
311        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
312        /// pass an HTTP URL for Telegram to get a file from the Internet,
313        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
314        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
315        media: String,
316        /// Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side.
317        /// The thumbnail should be in JPEG format and less than 200 kB in size.
318        /// A thumbnail's width and height should not exceed 320.
319        /// Ignored if the file is not uploaded using multipart/form-data.
320        /// Thumbnails can't be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>.
321        /// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
322        #[serde(skip_serializing_if = "Option::is_none")]
323        thumb: Option<InputFileVariant>,
324        /// Caption of the document to be sent, 0-1024 characters after entities parsing
325        #[serde(skip_serializing_if = "Option::is_none")]
326        caption: Option<String>,
327        /// Mode for parsing entities in the document caption.
328        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
329        #[serde(skip_serializing_if = "Option::is_none")]
330        parse_mode: Option<ParseMode>,
331        /// List of special entities that appear in the caption,
332        /// which can be specified instead of *parse_mode*
333        #[serde(skip_serializing_if = "Option::is_none")]
334        caption_entities: Option<Vec<MessageEntity>>,
335    },
336}
337
338/// Thumbnail type.
339#[derive(Clone, Serialize)]
340#[serde(untagged)]
341pub enum InputFileVariant {
342    /// Use existing file
343    File(InputFile),
344    /// Upload a new file
345    Id(String),
346}
347
348impl From<InputFile> for InputFileVariant {
349    fn from(file: InputFile) -> Self {
350        Self::File(file)
351    }
352}
353
354impl From<String> for InputFileVariant {
355    fn from(id: String) -> Self {
356        Self::Id(id)
357    }
358}
359
360impl From<&str> for InputFileVariant {
361    fn from(id: &str) -> Self {
362        Self::Id(id.to_string())
363    }
364}
365
366#[derive(Clone)]
367pub struct InputFile {
368    pub name: String,
369    pub data: Vec<u8>,
370    pub mime: String,
371}
372
373impl Serialize for InputFile {
374    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
375    where
376        S: serde::Serializer,
377    {
378        "".serialize(serializer)
379    }
380}
381
382/// Use this method to get basic info about a file and prepare it for downloading.
383/// For the moment, bots can download files of up to 20MB in size.
384/// On success, a [File](https://core.telegram.org/bots/api#file) object is returned.
385/// The file can then be downloaded via the link `https://api.telegram.org/file/bot<token>/<file_path>`, where `<file_path>` is taken from the response.
386/// It is guaranteed that the link will be valid for at least 1 hour.
387/// When the link expires, a new one can be requested by calling [getFile](https://core.telegram.org/bots/api#getfile) again.
388///
389/// **Note:** This function may not preserve the original file name and MIME type.
390/// You should save the file's MIME type and name (if available) when the File object is received.
391#[derive(Clone, Serialize)]
392pub struct GetFile {
393    /// File identifier to get info about
394    pub file_id: String,
395}
396
397impl GetFile {
398    /// Create a new getFile request
399    pub fn new(file_id: impl Into<String>) -> Self {
400        Self {
401            file_id: file_id.into(),
402        }
403    }
404}
405
406impl TelegramMethod for GetFile {
407    type Response = File;
408
409    fn name() -> &'static str {
410        "getFile"
411    }
412}
413
414impl JsonMethod for GetFile {}