A Python script to archive all media from a Telegram group, including groups that restrict forwarding and downloading.
The script uses Telethon, a Python MTProto client that authenticates as a user account and communicates directly with the Telegram API. Because it operates at the protocol level, it can download any media your account can see — regardless of the UI-level forward/download restriction set by group admins.
- Python 3.8+
- A Telegram account
- Telegram API credentials (free, see setup below)
- Go to https://my.telegram.org/apps
- Log in and create an application (any name/platform works)
- Note your API ID and API Hash
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtCopy .env.example to .env and fill in your values:
pip
cp .env.example .envOr export them directly:
export TG_API_ID=12345678
export TG_API_HASH=abcdef1234567890abcdef1234567890python archive.py <group> [options]
| Argument | Description |
|---|---|
group |
Group title, username (e.g. mygroupname), or numeric ID |
| Flag | Default | Description |
|---|---|---|
-o / --output |
./archive |
Base output directory |
--session |
archiver |
Telethon session file name |
--min-id |
— | Only download messages with ID ≥ this |
--max-id |
— | Only download messages with ID ≤ this |
--dry-run |
— | List what would be downloaded without writing |
--list-chats |
— | List all accessible groups/channels and exit |
--api-id |
$TG_API_ID |
Telegram API ID |
--api-hash |
$TG_API_HASH |
Telegram API Hash |
# List all your groups to find the right one
python archive.py --list-chats
# Archive a group by name
python archive.py "My Group Name"
# Archive a group by username
python archive.py mygroupname
# Archive a group by numeric ID
python archive.py 1234567890
# Dry run first
python archive.py "My Group Name" --dry-run
# Custom output directory
python archive.py "My Group Name" -o /mnt/nas/backups
# Archive a specific message range
python archive.py "My Group Name" --min-id 1000 --max-id 2000On the first run, Telethon will prompt you to log in:
Please enter your phone (or bot token): +1...
Please enter the code you received: 12345
A .session file is created locally so subsequent runs connect automatically.
Files are saved under <output>/<group name>/:
archive/
└── My Group Name/
├── 20240101_120000_42_photo.jpg
├── 20240101_120500_43_document.pdf
└── 20240102_083000_44_video.mp4
Each filename is prefixed with <YYYYMMDD_HHMMSS>_<msg_id>_ to avoid collisions. Re-running is safe — already-downloaded files are skipped automatically.
MIT — see LICENSE.