telegram_bot2/models/sent_file.rs
1use serde::{Deserialize, Serialize};
2
3/// Describe a file to be sent to the API.
4/// There are three ways to send files (photos, stickers, audio, media, etc.):
5/// - [`Id`][SentFile::Id]: If the file is already stored somewhere on the Telegram servers, you don't need to re-upload it: each file object has a file_id field, simply pass this file_id as a parameter instead of uploading. There are no limits for files sent this way.
6/// - [`Url`][SentFile::Url]: Provide Telegram with an HTTP URL for the file to be sent. Telegram will download and send the file. 5 MB max size for photos and 20 MB max for other types of content.
7/// - [`Upload`][SentFile::Upload]: Post the file using multipart/form-data in the usual way that files are uploaded via the browser. 10 MB max size for photos, 50 MB for other files
8#[derive(Serialize, Deserialize, Clone, Debug)]
9pub enum SentFile {
10 /// For files already on Telegram's servers
11 /// - It is not possible to change the file type when resending by file_id. I.e. a video can't be sent as a photo, a photo can't be sent as a document, etc.
12 /// - It is not possible to resend thumbnails.
13 /// - Resending a photo by file_id will send all of its sizes.
14 /// - file_id is unique for each individual bot and can't be transferred from one bot to another.
15 /// - file_id uniquely identifies a file, but a file can have different valid file_ids even for the same bot
16 Id(String),
17 /// File to be downloaded from a Url
18 /// - The target file must have the correct MIME type (e.g., audio/mpeg for sendAudio, etc.).
19 /// - sendDocument currently only works for GIF, PDF and ZIP files.
20 /// - To use sendVoice, the file must have the type audio/ogg and be no more than 1MB in size. 1-20MB voice notes will be sent as files.
21 /// - Other configurations may work but we can't guarantee that they will
22 Url(String),
23 /// Path of a local file to be uploaded to Telegram's servers. 10 MB max size for photos, 50 MB for other files
24 Upload(String),
25}
26
27impl SentFile {
28 /// Whether this is the [`Upload`][SentFile::Upload] variant
29 pub fn is_upload(&self) -> bool {
30 matches!(self, SentFile::Upload(_))
31 }
32}
33
34/// Checks whether the parameters is `Some(SentFile::Upload(_))` (used for serialization skip)
35pub(crate) fn skip_ser_sf_opt(f: &Option<SentFile>) -> bool {
36 matches!(f, Some(SentFile::Upload(_)))
37}
38
39/// Information about a file to send to Telegram's servers
40pub(crate) struct SentFileInfo {
41 /// Name of the field containing the field
42 pub field: &'static str,
43
44 /// Path of the file
45 pub path: String,
46}
47
48/// Describe a struct that may contain 0 to many files
49pub(crate) trait FileHolder {
50 /// Returns the array of [`SentFileInfo`] of the held files
51 fn files(&self) -> Vec<SentFileInfo>;
52}
53
54impl FileHolder for () {
55 fn files(&self) -> Vec<SentFileInfo> {
56 vec![]
57 }
58}