Telegram Message Archiver is a tool to archive messages from Telegram dialogs into a database.
- Connects to Telegram using Telethon
- Archives messages from Telegram dialogs
- Stores messages in a database using SQLAlchemy
- Provides CLI commands for initializing the database and running the archiver
- Python 3.8+
- MySQL (or any other SQLAlchemy-supported database)
- Telethon
- SQLAlchemy
- Click
-
Clone the repository:
git clone https://github.com/yourusername/telegram_messages_archiver.git cd telegram_messages_archiver
-
Install Poetry:
pip install poetry
-
Install the required packages:
poetry install
-
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
. - Fill settings according to Configuration section below.
- Fill Database settings for creating Docker container.
- In the root of your project directory, create a file named
-
Create docker container with Database:
docker-compose up -d
- API_ID: Your Telegram API ID
- API_HASH: Your Telegram API Hash
- PHONE: Your Telegram phone number
- MYSQL_DATABASE: Name of Database (you can leave default name). Using for Docker container
- MYSQL_USER: User name for Database (default is "user"). Using for Docker container
- MYSQL_PASSWORD: Password for Mysql User. Using for Docker container
- MYSQL_ROOT_PASSWORD: Password for Mysql Root User. Using for Docker container
- DB_PORT: This is local port for connection to Database. !!! Don't change it if you use Docker !!!
- DB_DOCKER_PORT: Port for connection to Docker container (default is 33006)
- MESSAGE_LIMIT: Limit of messages to fetch per dialog
- DSN: Data Source Name for the database (you may leave as is)
- DEBUG: Enable debug messages (true/false)
example file .envdbsampleforpostgres
- POSTGRES_DB: Name of Database. Using for Docker container
- POSTGRES_PASSWORD: Password for Postgres User. Using for Docker container
- DB_PORT: This is local port for connection to Database. !!! Don't change it if you use Docker !!!
- DB_DOCKER_PORT: Port for connection to Docker container (default 54320)
- DSN: Data Source Name for the database (you may leave as is)
poetry shell
-
Initialize the Database
To initialize the database, run:
python . initdb
-
Run the script:
python .
- 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.
- If you run docker compose without filled env credentials, you can get error with connection to database.
Please fill .env file with correct credentials. And run
docker-compose up -d --build
.
This project was started by [@MiguelTracelon] (https://www.github.com/MiguelTracelon)
Special thank to:
- Tacelon (https://tracelon.com) for initial realease
- People from SEAL (https://securityalliance.org/) for ideological support
- 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 GPL v3 License.