Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Media & Files

Upload and send a photo

#![allow(unused)]
fn main() {
// Upload from disk path
let uploaded = client.upload_file("photo.jpg").await?;

// Send as compressed photo
client.send_file(
    peer,
    uploaded.as_photo_media(),
    Some("My caption here"),
).await?;
}

Upload and send a document (any file)

#![allow(unused)]
fn main() {
let uploaded = client.upload_file("report.pdf").await?;

// Send as document (preserves original quality/format)
client.send_file(
    peer,
    uploaded.as_document_media(),
    Some("Monthly report"),
).await?;
}

TIP: For photos, as_photo_media() lets Telegram compress and display them inline. Use as_document_media() to preserve original file quality and format.

Upload from a stream

#![allow(unused)]
fn main() {
use tokio::fs::File;

let file    = File::open("video.mp4").await?;
let name    = "video.mp4".to_string();
let size    = file.metadata().await?.len() as i32;
let mime    = "video/mp4".to_string();

let uploaded = client.upload_stream(file, size, name, mime).await?;
client.send_file(peer, uploaded.as_document_media(), None).await?;
}

Send an album (multiple photos/videos)

#![allow(unused)]
fn main() {
let img1 = client.upload_file("photo1.jpg").await?;
let img2 = client.upload_file("photo2.jpg").await?;
let img3 = client.upload_file("photo3.jpg").await?;

client.send_album(
    peer,
    vec![
        img1.as_photo_media(),
        img2.as_photo_media(),
        img3.as_photo_media(),
    ],
    Some("Our trip 📸"),
).await?;
}

Albums are grouped as a single visual unit in the chat.

UploadedFile — methods

MethodReturnsDescription
uploaded.name()&strOriginal filename
uploaded.mime_type()&strDetected MIME type
uploaded.as_photo_media()InputMediaSend as compressed photo
uploaded.as_document_media()InputMediaSend as document

Download media from a message

#![allow(unused)]
fn main() {
if let tl::enums::Message::Message(m) = &raw_msg {
    if let Some(media) = &m.media {
        // download_media returns an async iterator of chunks
        let location = client.download_location(media);
        if let Some(loc) = location {
            let mut iter = client.iter_download(loc);
            let mut file = tokio::fs::File::create("download.bin").await?;

            while let Some(chunk) = iter.next().await? {
                tokio::io::AsyncWriteExt::write_all(&mut file, &chunk).await?;
            }
        }
    }
}
}

DownloadIter — options

#![allow(unused)]
fn main() {
let mut iter = client.iter_download(location)
    .chunk_size(512 * 1024);  // 512 KB per request (default: 128 KB)
}

MIME type reference

File typeMIMEDisplays as
JPEG, PNG, WebPimage/jpeg, image/pngPhoto (compressed)
GIFimage/gifAnimated image
MP4, MOVvideo/mp4Video player
OGG (Opus codec)audio/oggVoice message
MP3, FLACaudio/mpegAudio player
PDFapplication/pdfDocument with preview
ZIP, RARapplication/zipGeneric document
TGSapplication/x-tgstickerAnimated sticker

Get profile photos

#![allow(unused)]
fn main() {
let photos = client.get_profile_photos(peer, 10).await?;

for photo in &photos {
    if let tl::enums::Photo::Photo(p) = photo {
        println!("Photo ID {} — {} sizes", p.id, p.sizes.len());

        // Find the largest size
        let best = p.sizes.iter()
            .filter_map(|s| match s {
                tl::enums::PhotoSize::PhotoSize(ps) => Some(ps),
                _ => None,
            })
            .max_by_key(|ps| ps.size);

        if let Some(size) = best {
            println!("  Largest: {}x{} ({}B)", size.w, size.h, size.size);
        }
    }
}
}