This project uses the Telethon library to connect to a Telegram user account and fetch messages from all dialogs (chats, groups, and channels). It saves the last 1000 messages (or any specified limit) from each dialog into a text file.
- Connects to a Telegram account using the Telegram API.
- Fetches messages from all private chats, groups, and channels.
- Saves messages to a file (
telegram_messages.txt
), with each dialog separated for easy reading. - Includes detailed logging for debugging and troubleshooting.
- Python 3.7+
- A Telegram account with an API ID and API Hash (see instructions below).
- Telethon library.
- python-dotenv library.
-
Clone the repository (or create your project directory).
git clone https://github.com/your-username/telegram-message-fetcher.git cd telegram-message-fetcher
-
Install dependencies:
pip install telethon python-dotenv
-
Get Telegram API credentials:
- Go to my.telegram.org and log in with your phone number.
- Navigate to API development tools and create a new application.
- Copy the API ID and API Hash.
-
Create a
.env
file:- In the root of your project directory, create a file named
.env
. - Add the following environment variables, replacing the values with your credentials:
API_ID=your_api_id API_HASH=your_api_hash PHONE=your_phone_number
- In the root of your project directory, create a file named
-
Run the script:
python3 main.py
-
Output:
- The script will fetch messages from all dialogs in your Telegram account and save them to
telegram_messages.txt
. - Each dialog will be separated by a header with the chat title and ID.
- Only the last 1000 messages from each dialog will be saved (you can adjust this limit in the code).
- The script will fetch messages from all dialogs in your Telegram account and save them to
- MESSAGE_LIMIT: Set the maximum number of messages to fetch per dialog. Adjust this in the code as needed.
--- Messages from Example Chat (ID: 123456789) ---
2024-11-01 12:34:56 - 987654321: Hello, this is a test message.
2024-11-01 12:35:10 - 123456789: [Non-text message]
...
--- Messages from Example Group (ID: 987654321) ---
2024-11-01 15:00:00 - 654321987: Welcome to the group!
2024-11-01 15:10:05 - 123987456: [Non-text message]
...
- No messages in output file: Ensure dialogs are being fetched and messages contain text or relevant content.
- Session errors: If you're prompted for 2FA, enter the password to complete the login.
- Connection issues: Ensure you have an active internet connection and correct API credentials.
- Privacy and Permissions: Ensure you have permission to fetch and store messages, especially from groups and channels.
- Rate Limits: Avoid running the script too frequently to prevent triggering Telegram's rate limits.
This project is open-source and available under the MIT License.