1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use serde::{Deserialize, Serialize};

use crate::markup::{MessageEntity, ParseMode};
use crate::{JsonMethod, TelegramMethod};

/// This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound).
#[derive(Deserialize)]
pub struct Animation {
    /// Identifier for this file, which can be used to download or reuse the file
    pub file_id: String,
    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
    /// Can't be used to download or reuse the file.
    pub file_unique_id: String,
    /// Video width as defined by sender
    pub width: usize,
    /// Video height as defined by sender
    pub height: usize,
    /// Duration of the video in seconds as defined by sender
    pub duration: u32,
    /// Animation thumbnail as defined by sender
    pub thumb: Option<PhotoSize>,
    /// Original animation filename as defined by sender
    pub file_name: Option<String>,
    /// MIME type of the file as defined by sender
    pub mime_type: Option<String>,
    /// File size
    pub file_size: Option<usize>,
}

/// This object represents an audio file to be treated as music by the Telegram clients.
#[derive(Deserialize)]
pub struct Audio {
    /// Identifier for this file, which can be used to download or reuse the file
    pub file_id: String,
    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
    /// Can't be used to download or reuse the file.
    pub file_unique_id: String,
    /// Duration of the audio in seconds as defined by sender
    pub duration: u32,
    /// Performer of the audio as defined by sender or by audio tags
    pub performer: Option<String>,
    /// Title of the audio as defined by sender or by audio tags
    pub title: Option<String>,
    /// Original filename as defined by sender
    pub file_name: Option<String>,
    /// MIME type of the file as defined by sender
    pub mime_type: Option<String>,
    /// File size
    pub file_size: Option<usize>,
    /// Thumbnail of the album cover to which the music file belongs
    pub thumb: Option<PhotoSize>,
}

/// This object represents a general file (as opposed to
/// [photos](https://core.telegram.org/bots/api#photosize),
/// [voice messages](https://core.telegram.org/bots/api#voice) and
/// [audio files](https://core.telegram.org/bots/api#audio)).
#[derive(Deserialize)]
pub struct Document {
    /// Identifier for this file, which can be used to download or reuse the file
    pub file_id: String,
    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
    /// Can't be used to download or reuse the file.
    pub file_unique_id: String,
    /// Document thumbnail as defined by sender
    pub thumb: Option<PhotoSize>,
    /// Original filename as defined by sender
    pub file_name: Option<String>,
    /// MIME type of the file as defined by sender
    pub mime_type: Option<String>,
    /// File size
    pub file_size: Option<usize>,
}

/// This object represents one size of a photo or a
/// [file](https://core.telegram.org/bots/api#document) /
/// [sticker](https://core.telegram.org/bots/api#sticker) thumbnail.
#[derive(Deserialize)]
pub struct PhotoSize {
    /// Identifier for this file, which can be used to download or reuse the file
    pub file_id: String,
    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
    /// Can't be used to download or reuse the file.
    pub file_unique_id: String,
    /// Photo width
    pub width: u32,
    /// Photo height
    pub height: u32,
    /// File size
    pub file_size: u32,
}

/// This object represents a video file.
#[derive(Deserialize)]
pub struct Video {
    /// Identifier for this file, which can be used to download or reuse the file
    pub file_id: String,
    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
    /// Can't be used to download or reuse the file.
    pub file_unique_id: String,
    /// Video width as defined by sender
    pub width: u32,
    /// Video height as defined by sender
    pub height: u32,
    /// Duration of the video in seconds as defined by sender
    pub duration: u32,
    /// Video thumbnail
    pub thumb: Option<PhotoSize>,
    /// Original animation filename as defined by sender
    pub file_name: Option<String>,
    /// MIME type of the file as defined by sender
    pub mime_type: Option<String>,
    /// File size
    pub file_size: Option<u32>,
}

/// This object represents a [video message](https://telegram.org/blog/video-messages-and-telescope)
/// (available in Telegram apps as of [v.4.0](https://telegram.org/blog/video-messages-and-telescope)).
#[derive(Deserialize)]
pub struct VideoNote {
    /// Identifier for this file, which can be used to download or reuse the file
    pub file_id: String,
    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
    /// Can't be used to download or reuse the file.
    pub file_unique_id: String,
    /// Video width and height (diameter of the video message) as defined by sender
    pub length: u32,
    /// Duration of the video in seconds as defined by sender
    pub duration: u32,
    /// Video thumbnail
    pub thumb: Option<PhotoSize>,
    /// File size
    pub file_size: Option<u32>,
}

/// This object represents a voice note.
#[derive(Deserialize)]
pub struct Voice {
    /// Identifier for this file, which can be used to download or reuse the file
    pub file_id: String,
    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
    /// Can't be used to download or reuse the file.
    pub file_unique_id: String,
    /// Duration of the audio in seconds as defined by sender
    pub duration: u32,
    /// MIME type of the file as defined by sender
    pub mime_type: Option<String>,
    /// File size
    pub file_size: Option<u32>,
}

/// This object represents a file ready to be downloaded.
///
/// The file can be downloaded via the link `https://api.telegram.org/file/bot<token>/<file_path>`.
/// It is guaranteed that the link will be valid for at least 1 hour.
/// When the link expires, a new one can be requested by calling [getFile](https://core.telegram.org/bots/api#getfile).
#[derive(Deserialize)]
pub struct File {
    /// Identifier for this file, which can be used to download or reuse the file
    pub file_id: String,
    /// Unique identifier for this file, which is supposed to be the same over time and for different bots.
    /// Can't be used to download or reuse the file.
    pub file_unique_id: String,
    /// File size, if known
    pub file_size: Option<u32>,
    /// File path. Use `https://api.telegram.org/file/bot<token>/<file_path>` to get the file.
    pub file_path: Option<String>,
}

/// This object represents the content of a media message to be sent.
/// It should be one of
/// - InputMediaAnimation
/// - InputMediaDocument
/// - InputMediaAudio
/// - InputMediaPhoto
/// - InputMediaVideo
#[derive(Serialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum InputMedia {
    /// Represents a photo to be sent.
    Photo {
        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
        /// pass an HTTP URL for Telegram to get a file from the Internet,
        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
        media: String,
        /// Caption of the photo to be sent, 0-1024 characters after entities parsing
        #[serde(skip_serializing_if = "Option::is_none")]
        caption: Option<String>,
        /// Mode for parsing entities in the photo caption.
        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
        #[serde(skip_serializing_if = "Option::is_none")]
        parse_mode: Option<ParseMode>,
        /// List of special entities that appear in the caption,
        /// which can be specified instead of parse_mode
        #[serde(skip_serializing_if = "Option::is_none")]
        caption_entities: Option<Vec<MessageEntity>>,
    },
    /// Represents a video to be sent.
    Video {
        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
        /// pass an HTTP URL for Telegram to get a file from the Internet,
        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
        media: String,
        /// Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side.
        /// The thumbnail should be in JPEG format and less than 200 kB in size.
        /// A thumbnail's width and height should not exceed 320.
        /// Ignored if the file is not uploaded using multipart/form-data.
        /// 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>.
        /// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
        #[serde(skip_serializing_if = "Option::is_none")]
        thumb: Option<InputFileVariant>,
        /// Video width
        #[serde(skip_serializing_if = "Option::is_none")]
        width: Option<u32>,
        /// Video height
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<u32>,
        /// Video duration
        #[serde(skip_serializing_if = "Option::is_none")]
        duration: Option<u32>,
        /// Pass True, if the uploaded video is suitable for streaming
        supports_streaming: Option<bool>,
        /// Caption of the video to be sent, 0-1024 characters after entities parsing
        #[serde(skip_serializing_if = "Option::is_none")]
        caption: Option<String>,
        /// Mode for parsing entities in the video caption.
        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
        #[serde(skip_serializing_if = "Option::is_none")]
        parse_mode: Option<ParseMode>,
        /// List of special entities that appear in the caption,
        /// which can be specified instead of *parse_mode*
        #[serde(skip_serializing_if = "Option::is_none")]
        caption_entities: Option<Vec<MessageEntity>>,
    },
    /// Represents an animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent.
    Animation {
        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
        /// pass an HTTP URL for Telegram to get a file from the Internet,
        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
        media: String,
        /// Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side.
        /// The thumbnail should be in JPEG format and less than 200 kB in size.
        /// A thumbnail's width and height should not exceed 320.
        /// Ignored if the file is not uploaded using multipart/form-data.
        /// 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>.
        /// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
        #[serde(skip_serializing_if = "Option::is_none")]
        thumb: Option<InputFileVariant>,
        /// Animation width
        #[serde(skip_serializing_if = "Option::is_none")]
        width: Option<u32>,
        /// Animation height
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<u32>,
        /// Animation duration
        #[serde(skip_serializing_if = "Option::is_none")]
        duration: Option<u32>,
        /// Caption of the animation to be sent, 0-1024 characters after entities parsing
        #[serde(skip_serializing_if = "Option::is_none")]
        caption: Option<String>,
        /// Mode for parsing entities in the animation caption.
        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
        #[serde(skip_serializing_if = "Option::is_none")]
        parse_mode: Option<ParseMode>,
        /// List of special entities that appear in the caption,
        /// which can be specified instead of *parse_mode*
        #[serde(skip_serializing_if = "Option::is_none")]
        caption_entities: Option<Vec<MessageEntity>>,
    },
    /// Represents an audio file to be treated as music to be sent.
    Audio {
        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
        /// pass an HTTP URL for Telegram to get a file from the Internet,
        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
        media: String,
        /// Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side.
        /// The thumbnail should be in JPEG format and less than 200 kB in size.
        /// A thumbnail's width and height should not exceed 320.
        /// Ignored if the file is not uploaded using multipart/form-data.
        /// 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>.
        /// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
        #[serde(skip_serializing_if = "Option::is_none")]
        thumb: Option<InputFileVariant>,
        /// Performer of the audio
        #[serde(skip_serializing_if = "Option::is_none")]
        performer: Option<String>,
        /// Title of the audio
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Duration of the audio in seconds
        #[serde(skip_serializing_if = "Option::is_none")]
        duration: Option<u32>,
        /// Caption of the audio to be sent, 0-1024 characters after entities parsing
        #[serde(skip_serializing_if = "Option::is_none")]
        caption: Option<String>,
        /// Mode for parsing entities in the audio caption.
        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
        #[serde(skip_serializing_if = "Option::is_none")]
        parse_mode: Option<ParseMode>,
        /// List of special entities that appear in the caption,
        /// which can be specified instead of *parse_mode*
        #[serde(skip_serializing_if = "Option::is_none")]
        caption_entities: Option<Vec<MessageEntity>>,
    },
    /// Represents a general file to be sent.
    Document {
        /// File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
        /// pass an HTTP URL for Telegram to get a file from the Internet,
        /// or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
        //// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
        media: String,
        /// Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side.
        /// The thumbnail should be in JPEG format and less than 200 kB in size.
        /// A thumbnail's width and height should not exceed 320.
        /// Ignored if the file is not uploaded using multipart/form-data.
        /// 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>.
        /// [More info on Sending Files »](https://core.telegram.org/bots/api#sending-files)
        #[serde(skip_serializing_if = "Option::is_none")]
        thumb: Option<InputFileVariant>,
        /// Caption of the document to be sent, 0-1024 characters after entities parsing
        #[serde(skip_serializing_if = "Option::is_none")]
        caption: Option<String>,
        /// Mode for parsing entities in the document caption.
        /// See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
        #[serde(skip_serializing_if = "Option::is_none")]
        parse_mode: Option<ParseMode>,
        /// List of special entities that appear in the caption,
        /// which can be specified instead of *parse_mode*
        #[serde(skip_serializing_if = "Option::is_none")]
        caption_entities: Option<Vec<MessageEntity>>,
    },
}

/// Thumbnail type.
#[derive(Serialize)]
#[serde(untagged)]
pub enum InputFileVariant {
    /// Use existing file
    File(InputFile),
    /// Upload a new file
    Id(String),
}

impl From<InputFile> for InputFileVariant {
    fn from(file: InputFile) -> Self {
        Self::File(file)
    }
}

impl From<String> for InputFileVariant {
    fn from(id: String) -> Self {
        Self::Id(id)
    }
}

impl From<&str> for InputFileVariant {
    fn from(id: &str) -> Self {
        Self::Id(id.to_string())
    }
}

pub struct InputFile {
    pub name: String,
    pub data: Vec<u8>,
    pub mime: String,
}

impl Serialize for InputFile {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        "".serialize(serializer)
    }
}

/// Use this method to get basic info about a file and prepare it for downloading.
/// For the moment, bots can download files of up to 20MB in size.
/// On success, a [File](https://core.telegram.org/bots/api#file) object is returned.
/// 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.
/// It is guaranteed that the link will be valid for at least 1 hour.
/// When the link expires, a new one can be requested by calling [getFile](https://core.telegram.org/bots/api#getfile) again.
///
/// **Note:** This function may not preserve the original file name and MIME type.
/// You should save the file's MIME type and name (if available) when the File object is received.
#[derive(Serialize)]
pub struct GetFile {
    /// File identifier to get info about
    pub file_id: String,
}

impl GetFile {
    /// Create a new getFile request
    pub fn new(file_id: impl Into<String>) -> Self {
        Self {
            file_id: file_id.into(),
        }
    }
}

impl TelegramMethod for GetFile {
    type Response = File;

    fn name() -> &'static str {
        "getFile"
    }
}

impl JsonMethod for GetFile {}