A Python-based Telegram bot designed to monitor various Ethereum contract events, record them into a PostgreSQL database, and send statistics reports to a Telegram group. This application uses libraries such as web3.py
for Ethereum blockchain interaction, python-telegram-bot
for Telegram bot functionality, and SQLAlchemy
for database operations.
- Modular Event Monitoring: Configurable monitoring of multiple Ethereum contract events.
- Database Recording: Storing event data in a PostgreSQL database for historical tracking and analysis.
- Automated Reporting: Generating and sending detailed reports for each monitored event to a Telegram group at scheduled intervals.
- Python 3.x
- PostgreSQL
- Telegram Bot API
- An Ethereum node accessible via HTTPS
-
Clone the repository to your local machine.
-
Install
pip install virtualenv
and create a virtual environmentvirtualenv venv
-
Activate the virtual environment. Windows:
venv\Scripts\activate
Linux/macOS:source venv/bin/activate
-
Install required Python libraries with
pip install -r requirements.txt
(ensure you have pip installed). -
Set up your environment variables and update the
.env
file with your database URI, Telegram bot token, Ethereum node URL, and Telegram group ID. Ensure to also configure theEVENTS_CONFIG
insrc/config.py
with the events you want to monitor, specifying each event's name, contract address, ABI, whether it is active, and other necessary details as per the updated structure. -
You can start the application as:
python app.py
to fetch events from the last known block number in the DB for all active events.python app.py 154366
to start the application from a specific block number for all active events.python app.py 0
to rewrite all event history in the DB for all active events. -
To see program logs, check the
app.log
andtelegram.log
files in the project root directory ifFILE_LOGGING
is enabled.
The application automatically sends reports to the configured Telegram group at scheduled intervals for each active event. Ensure your .env
file is correctly set up with the TELEGRAM_BOT_TOKEN
and TELEGRAM_GROUP_ID
.
To schedulre report install if necessary crontab
and run crontab -e
and set cron * * * * * python /path/to/your/send_report_to_telegram.py
- To add or modify the events being monitored, update the
EVENTS_CONFIG
insrc/config.py
. For each event, you can specify the contract address, ABI, database table name, and other relevant settings. - Implement custom event parsing logic by creating subclasses of
EventParser
insrc/event_parser.py
for each event type. This allows for flexible handling of different event data structures. - Customize report generation by implementing subclasses of
ReportGenerator
insrc/report_generators.py
for each event type. This enables tailored reports for different events, including specific calculations and formatting.
For testing run pytest
from your terminal
Copyright (c) 2024.