Telegram Media Downloader Bot β FastAPI + Pyrogram Webhook
π Telegram Media Downloader Bot
A highβperformance Telegram webhook bot built with FastAPI and Pyrogram that can download and resend media from Telegram posts β including photos, videos, audio, documents, and media groups.
Designed for Docker deployments (including serverless containers), this project runs as a single app process with webhook support β no polling required.
β¨ Features
- π₯ Download media from any Telegram post link
- πΌ Supports photos, videos, audio, documents, stickers, animations
- π§© Handles media groups (albums)
- π Batch download post ranges
- β‘ Webhook-based (fast & scalable)
- π³ Docker-ready deployment
- π§Ή Temp file auto-cleanup
- π Live bot system stats
- π§΅ Concurrent task management
- π Rotating log files
π Tech Stack
- Backend: FastAPI
- Telegram Client: Pyrogram (Bot + User session)
- Server: Uvicorn
- Container: Docker
- Utilities: python-dotenv, psutil
π Project Structure
.
βββ main.py # Single-file webhook bot
βββ config.env # Environment configuration
βββ requirements.txt # Python dependencies
βββ Dockerfile # Container build instructions
βββ downloads/ # Temporary media storage (auto-created)
βοΈ Environment Configuration
Create a file named config.env and fill in your Telegram credentials:
API_ID=
API_HASH=
BOT_TOKEN=
SESSION_STRING=
WEBHOOK_SECRET=
MAX_CONCURRENT_DOWNLOADS=1
BATCH_SIZE=1
FLOOD_WAIT_DELAY=10
π Where to get credentials
- API_ID / API_HASH: https://my.telegram.org
- BOT_TOKEN: Create bot via @BotFather
- SESSION_STRING: Generate via Pyrogram tools or session generators
βΆοΈ Run Locally
1οΈβ£ Install dependencies
pip install -r requirements.txt
2οΈβ£ Start server
uvicorn main:app --host 0.0.0.0 --port 7860
3οΈβ£ Set Telegram Webhook
https://api.telegram.org/bot<BOT_TOKEN>/setWebhook?url=<YOUR_SERVER_URL>/webhook
π³ Docker Deployment
Build Image
docker build -t tg-media-bot .
Run Container
docker run -d -p 7860:7860 --env-file config.env tg-media-bot
π€ Bot Commands
| Command | Description |
|---|---|
/start |
Welcome message |
/help |
Show usage guide |
/dl <url> |
Download single post |
/bdl <start> <end> |
Batch download range |
/stats |
System statistics |
/cleanup |
Remove temp files |
/logs |
Get log file |
/killall |
Cancel running tasks |
You can also paste a Telegram post link directly without using commands.
π How It Works
- Telegram sends updates to the webhook endpoint
- FastAPI receives and parses commands
- User session fetches original post
- Media is downloaded temporarily
- Bot account re-uploads media to user
- Temp files are cleaned automatically
π§ Concurrency & Limits
- Configurable parallel downloads
- FloodWait auto-handling
- Batch throttling delay
- Telegram file size checks
π Logs
- Rotating log file:
logs.txt - Auto-regenerated each run
- Downloadable via
/logs
π§Ή Cleanup
Temporary files are stored in:
downloads/<chat_id>/
Removed automatically after sending.
Manual cleanup available via /cleanup.
π Requirements
- Bot must be admin (if private channel)
- User session must be member of target chat
- Valid Telegram post links
π¦ Download Source Code
You can download all project files as a ZIP archive here:
π Download Project ZIP
β Support
If this project helped you, consider starring the repo and sharing it with others!