A Python tool to backup Telegram chat history and media using the Telegram API. Messages are saved in IRC-style text format.
Note: This tool was created by an LLM (Claude) based on user specifications written by Salvatore Sanfilippo.
- Downloads chat history in IRC-style format:
[timestamp] <username> message - Optional media download with filtering by type and size
- Progress reporting with retry handling
- Read-only operation (safe, won't modify your chats)
- Minimal dependencies (only Telethon)
pip install -r requirements.txtRequires Python 3.7 or later.
Before using this tool, you need to obtain API credentials from Telegram:
- Go to https://my.telegram.org/auth
- Log in with your phone number
- Click on "API development tools"
- Fill in the application details (name and platform)
- Copy your
api_id(numeric) andapi_hash(hexadecimal string)
These credentials are tied to your Telegram account and should be kept private.
View all options:
python telegram_backup.py --helpBasic usage:
python telegram_backup.py <api_id> <api_hash> <chat_username>Download chat history only (no media):
python telegram_backup.py 12345678 abcdef1234567890 mychatDownload chat with all media:
python telegram_backup.py 12345678 abcdef1234567890 mychat --download-mediaDownload only images:
python telegram_backup.py 12345678 abcdef1234567890 mychat --download-media --media-filter imageDownload images under 5MB:
python telegram_backup.py 12345678 abcdef1234567890 mychat --download-media --media-filter image --media-max-size 5242880--download-media- Download media files (disabled by default)--media-filter TYPE- Filter by type:image,audio,video,other,all(default:all)--media-max-size BYTES- Maximum file size in bytes (default: unlimited)--output-dir DIR- Output directory (default:backup)
On first run, Telegram will send a verification code to your account. Enter it when prompted. A session file will be created for future runs.
The tool creates:
-
Chat history:
<chat_username>_history.txt[2025-01-01 12:34:56] <Alice> Hello! [2025-01-01 12:35:10] <Bob> Hi there! [2025-01-01 12:35:30] <Alice> [MEDIA: msg_12345.jpg] -
Media folder:
media/(if--download-mediais used)- Files are named
msg_<id>.<ext> - Filtered files are noted in the chat log
- Files are named
- Large media downloads show progress dots (one per 100KB)
- Timeouts are handled with automatic retries (3 attempts)
- Media download is optional to avoid long wait times
- All text is saved in UTF-8 encoding