stable_diffusion_bot/bot/helpers.rs
1use anyhow::Context;
2use futures::TryStreamExt;
3use teloxide::{net::Download, types::File, Bot};
4
5/// Download a Telegram `File` and return its contents as bytes.
6///
7/// # Examples
8///
9/// ```ignore
10/// use teloxide::prelude::*;
11///
12/// async fn handle_photo(bot: Bot, message: Message) -> Result<(), Box<dyn std::error::Error>> {
13/// if let Some(photos) = message.photo(){
14/// if let Some(photo) = photos.last() {
15/// let file = bot.get_file(&photo.file.id).await?;
16/// let bytes = get_file(&bot, &file)?;
17///
18/// // ... do something with the photo bytes ...
19///
20/// }
21/// }
22///
23/// Ok(())
24/// }
25/// ```
26pub async fn get_file(bot: &Bot, file: &File) -> anyhow::Result<bytes::Bytes> {
27 bot.download_file_stream(&file.path)
28 .try_collect()
29 .await
30 .context("Failed to download file")
31 .map(bytes::BytesMut::freeze)
32}