Scrape all messages from your Telegram groups, channels, and private conversations.
This script uses Telethon, a Python library that connects to your personal Telegram account via the MTProto API. This is not a bot β it's your own account connecting directly, as if you were opening Telegram on a new device.
- Connects to your Telegram account (phone number + OTP authentication)
- Lists all your dialogs (groups, channels, private messages)
- Scrapes messages from each dialog
- Exports everything as JSON, CSV, or Markdown
- β Sends no messages
- β Modifies no groups/contacts
- β Deletes nothing
- β Read-only
- Python 3.10+
- A Telegram account
- Telegram API credentials (free)
- Go to my.telegram.org
- Log in with your Telegram phone number
- Click on "API development tools"
- Fill out the form:
- App title: whatever you want (e.g.,
my_scraper) - Short name: a short name (e.g.,
myscr) - Platform:
Other - Description:
Personal scraper
- App title: whatever you want (e.g.,
- Click "Create application"
- Note the
api_id(number) andapi_hash(hex string)
pip install telethonTwo options (pick one):
Option A β config.json file (recommended)
cp config.example.json config.jsonThen edit config.json with your actual values:
{
"api_id": 12345678,
"api_hash": "your_api_hash_here"
}Option B β Environment variables
export TG_API_ID=12345678
export TG_API_HASH=your_api_hash_herepython3 telegram_scraper.pyThe script will prompt you for:
- Your phone number (with country code, e.g.,
+33612345678) - The OTP code that Telegram sends you through the Telegram app or via SMS
- (If enabled) Your Telegram 2FA password
β οΈ The first run creates asessionfile that saves the authentication. Subsequent runs won't require re-authentication.
python3 telegram_scraper.py
# β Connects directly, no re-auth# Scrape the last 50 messages from each dialog
python3 telegram_scraper.py --limit 50
# Scrape 500 messages per dialog (default)
python3 telegram_scraper.py
# Scrape the ENTIRE history (β οΈ can be VERY long)
python3 telegram_scraper.py --limit 0# Groups only
python3 telegram_scraper.py --types groups
# Channels only
python3 telegram_scraper.py --types channels
# Private conversations only
python3 telegram_scraper.py --types private# JSON (default) β most complete
python3 telegram_scraper.py --format json
# CSV β compatible with Excel / Google Sheets
python3 telegram_scraper.py --format csv
# Markdown β human-readable
python3 telegram_scraper.py --format markdown# Default: ./exports/
python3 telegram_scraper.py --output /path/to/folderexports/
βββ telegram_export_20260415_035253.json # Full messages with metadata
βββ telegram_summary_20260415_035253.json # Overview (dialog count, message count)
[
{
"name": "Group name",
"type": "group",
"id": 123456789,
"message_count_scraped": 50,
"members_count": 3390,
"messages": [
{
"id": 123,
"date": "2026-04-14T18:30:00+00:00",
"sender_id": 987654321,
"sender_name": "John Doe",
"text": "Message content",
"reply_to": null,
"media_type": null,
"forward": false
}
]
}
]{
"scraped_at": "2026-04-15T03:52:53",
"total_dialogs": 37,
"total_messages": 1546,
"format": "json",
"output_file": "exports/telegram_export_20260415_035253.json",
"dialogs": [
{ "name": "Group name", "type": "group", "id": 123, "messages_scraped": 50, "members": 3390 }
]
}| File | Content | Risk |
|---|---|---|
config.json |
api_id + api_hash | π‘ Moderate β allows creating apps |
session |
Telegram auth token | π΄ High β full account access |
exports/ |
Personal messages | π΄ High β private data |
- β
Never commit
config.json,session, orexports/ - β
The repo's
.gitignoreis already configured to exclude them - β
You can revoke access at any time in Telegram:
- Settings β Active devices β disconnect the server
- β
To delete the session:
rm session
You can run the scraper on a schedule with cron:
# Every 6 hours β scrape the last 100 messages
0 */6 * * * cd /path/to/repo && python3 telegram_scraper.py --limit 100 --output /path/to/exports >> /var/log/tg-scraper.log 2>&1Or use a systemd timer for more control.
β First run, this is normal. Enter your phone number with country code.
β The OTP code has expired or is incorrect. Rerun the script to get a new one.
β Telegram is asking for your 2FA password. Enter it when the script prompts you.
β The group/channel may have been deleted or you were banned from it.
β Telegram is rate-limiting requests. The script will wait automatically, or rerun later.
- Telethon β Telegram MTProto client for Python
- Python 3.10+ (for native type hint support)
Personal use. Do whatever you want with it.