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
use anyhow::Context;
use futures::TryStreamExt;
use teloxide::{net::Download, types::File, Bot};

/// Download a Telegram `File` and return its contents as bytes.
///
/// # Examples
///
/// ```ignore
/// use teloxide::prelude::*;
///
/// async fn handle_photo(bot: Bot, message: Message) -> Result<(), Box<dyn std::error::Error>> {
///     if let Some(photos) = message.photo(){
///         if let Some(photo) = photos.last() {
///             let file = bot.get_file(&photo.file.id).await?;
///             let bytes = get_file(&bot, &file)?;
///
///             // ... do something with the photo bytes ...
///
///         }
///     }
///
///     Ok(())
/// }
/// ```
pub async fn get_file(bot: &Bot, file: &File) -> anyhow::Result<bytes::Bytes> {
    bot.download_file_stream(&file.path)
        .try_collect()
        .await
        .context("Failed to download file")
        .map(bytes::BytesMut::freeze)
}