Python
Media downloads
Download photos, videos, stickers, and documents.
download_media
Download media to disk with automatic filename handling.
Python
async for message in client.iter_messages("telegram", limit=20):
if message.media:
for item in message.media:
# Download to directory (auto-generates filename)
path = await client.download_media(item, "./downloads")
print(f"Saved to {path}")
# With overwrite control
path = await client.download_media(
item,
"./downloads",
overwrite=False # Skip if file exists
)download_bytes
Download media to memory for processing without saving to disk.
Python
async for message in client.iter_messages("photos", limit=10):
if message.media:
for photo in message.media:
# Download to bytes
data = await client.download_bytes(photo)
# With size limit
data = await client.download_bytes(
photo,
max_bytes=10_000_000 # 10 MB limit
)
# Process in memory
from PIL import Image
import io
image = Image.open(io.BytesIO(data))
image.thumbnail((200, 200))
image.save("thumbnail.jpg")Media types
Messages can contain photos, videos, stickers, documents, and more.
Python
if message.media:
for item in message.media:
print(f"Type: {item.type}") # photo, video, sticker, document, etc.
print(f"URL: {item.url}") # Direct download URL
print(f"Size: {item.size_bytes}") # File size (if available)
# Type-specific fields
if item.type == "photo":
print(f"Dimensions: {item.width}x{item.height}")
elif item.type == "video":
print(f"Duration: {item.duration_seconds}s")Error handling
Handle download failures gracefully.
Python
from teleglance import RequestFailed
async for message in client.iter_messages("channel", limit=50):
if message.media:
for item in message.media:
try:
path = await client.download_media(item, "./downloads")
print(f"Downloaded: {path}")
except RequestFailed as e:
print(f"Failed to download {item.url}: {e}")
except OSError as e:
print(f"Disk error: {e}")