This project is designed to monitor Telegram messages and interact with them through a Telegram bot. The project consists of two main components: a script for parsing messages and a Telegram bot for displaying the latest messages.
- Overview
- Features
- Installation
- Setting up Environment Variables
- Starting the Project
- Run with Docker
- Getting Access
This project is built using Python and the following technologies:
- Telethon: An asynchronous Python Telegram client library.
- SQLAlchemy: The Python SQL toolkit and Object-Relational Mapping (ORM) library.
- Django: A high-level Python web framework.
- Docker: A platform for developing, shipping, and running applications in containers.
- Monitors Telegram messages from personal chats.
- Parses messages and stores them in a PostgreSQL database.
- Displays the latest messages from the database.
- Provides a 'Show latest 10 messages' button.
- Loads more messages on demand.
-
Clone the repository:
git clone https://github.com/DmytroHlazyrin/Telegram_parser.git
-
Go to the project directory:
cd Telegram_parser -
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On macOS or Linux venv\Scripts\activate # On Windows
-
Install the required dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the project root directory:touch .env
-
Add the following environment variables to the
.envfile:TELEGRAM_BOT_TOKEN=<your-telegram-bot-token> API_ID=<your-api-id> API_HASH=<your-api-hash> PHONE_NUMBER=<your-phone-number> DATABASE_URL=postgresql://<db_user>:<db_password>@<db_host>/<db_name> POSTGRES_USER=db_user POSTGRES_PASSWORD=db_user POSTGRES_DB=telegram_db
-
Creating the database tables and first parsing:
python telethon_app.py
-
Start the Telegram bot:
python telegram_bot.py
-
Build the Docker images and start the containers:
docker-compose build docker-compose up
-
When you connect for the first time, you need to manually run the script to enter your phone number and then the secret code that will come to Telegram. The session data will be saved to the database.
docker exec -it <container_name> /bin/sh python /app/telethon_app.py input(Enter your phone number:) input(Enter the secret code:)
The Docker Compose setup includes services for PostgreSQL, the Telegram bot,
and the cron service for running the telethon_app.py script every 5 minutes.
- Create a Telegram bot and get the
TELEGRAM_BOT_TOKEN. Follow the instructions from Telegram's BotFather. - Obtain your
API_IDandAPI_HASHby following the instructions at my.telegram.org.
Thank you for checking out the Telegram_parser project! I hope this solution helps you efficiently monitor and interact with your Telegram messages. If you have any questions or suggestions, feel free to reach out. Happy coding!