Telegram Chat Extractor is a command-line tool written in Python that allows you to download and save chat histories from Telegram groups or channels. It uses the Telethon library to interact with the Telegram API and provides advanced filtering options, allowing you to extract exactly the messages you need based on time, date, or specific users.
- First-Run Configuration: On your first run, the script prompts you once for your Telegram API credentials and saves them locally to a
config.jsonfile. Future runs will automatically load these credentials. - Private Local Files: Credentials, Telegram session files, and generated exports are excluded from Git.
- User Filtering: Extract messages from everyone in a group, or filter the extraction to only include specific usernames.
- Flexible Extraction Limits:
- By Count: Extract the last N messages (e.g., the last 100 messages).
- By Date: Extract all messages sent after a specific date.
- By Time Range: Extract messages from a specific start time to the present, or between a specific start and end time.
- Reply Tracking: The exported text file maps replies to their original line numbers for easy context reading.
- Chronological Sorting: Regardless of how the messages are fetched, the final output file is sorted in standard chronological order (oldest to newest).
Before using this tool, you must have the following:
- Python 3.7 or higher installed on your system.
- Telegram API Credentials: You need an
API IDandAPI Hash.- Go to https://my.telegram.org/auth
- Log in with your Telegram account.
- Go to "API development tools" and create a new application to get your credentials.
-
Clone this repository to your local machine:
git clone https://github.com/yourusername/TeleExtract.git cd TeleExtract -
Install the required Python dependency (
telethon):pip install telethon
To start the script, run the following command in your terminal:
python Scrapper.pyThe first time you run the script, it will ask once for your API ID, API Hash, and Phone Number. These will be saved into a file named config.json in the same directory.
Telegram will send a login code to your Telegram app (or via SMS). You will need to enter this code into the terminal to authenticate the session. If you have Two-Factor Authentication enabled, you will also be prompted for your password.
Once authenticated, the terminal will guide you through the extraction process:
- Target Group: Enter the username or invite link of the group/channel you want to extract messages from.
- Output Filename: Choose a name for the output text file (defaults to
extracted_chats.txt). Every export is saved in theextracted_files/folder. - User Selection: Choose whether to extract messages from everyone or input a comma-separated list of specific usernames.
- Range Selection: Choose how far back to scan (Count, Date, or Time Range).
The tool will display a live progress counter as it scans the chat history. You can press Ctrl + C at any time to interrupt the scan and instantly save whatever data has been collected up to that point.
The extracted messages are saved as plain text files in extracted_files/. The format is designed to be highly readable.
Example format:
Line 1 | [2026-05-23 14:00] @username1: Hello everyone!
Line 2 | [2026-05-23 14:05] @username2: Does anyone have the meeting notes?
Line 3 [REPLYING TO LINE 2] | [2026-05-23 14:06] @username1: Yes, I will send them shortly.
Line 4 | [2026-05-23 14:10] @username3: [Media/Sticker]
Note: Media files (images, videos, stickers) are not downloaded. The text will simply indicate [Media/Sticker] in place of the file.
This tool is intended for personal archiving and data management. Please respect the privacy of others and ensure you comply with Telegram's Terms of Service when using API tools to interact with the platform. Automated scraping of data without consent may result in your Telegram account being restricted or banned.