This is a Python-based automation tool utilizing Telethon to emulate and automate Telegram client activities, specifically monitoring channel subscriptions.
- Automated Monitoring: Regularly checks for user subscription and unsubscription events on a Telegram channel.
- Service-Oriented: Runs as a continuous service on Linux, fetching admin actions every 5 minutes.
- Efficient Storage: Actions are hashed based on the date and user ID. Only unique actions are saved to the Firebase Firestore database, preventing duplicates.
- Real-time Notifications: Sends detailed alerts of each action to another Telegram channel or group. These alerts include user ID, nickname, first and last name, action date, and the corresponding hash.
- Sentry Integration (Optional): Monitor application health and track potential issues with Sentry.
- OS: Linux-based machine.
- Permissions: User with
sudo
access. - Runtime: Python 3.x installed.
- Database Setup:
- Create a collection named
admin_actions
. - Insert an empty document with a random name within this collection.
- Create a collection named
- Private Key Configuration:
- Download your Firebase private key file from the Firebase console.
- Rename the key file to
serviceAccountKey.json
. - Place it in the root directory of the project (where
main.py
is located).
- API Credentials:
- Register on my.telegram.org to obtain
api_id
andapi_hash
.
- Register on my.telegram.org to obtain
- Channel to Monitor:
- Ensure you have a Telegram channel nickname you want to monitor (e.g.,
@channel_name
).
- Ensure you have a Telegram channel nickname you want to monitor (e.g.,
- Bot Token:
- Create a Telegram bot via t.me/BotFather and note down the token.
- Notification Receiver ID:
- Decide on a Telegram channel or group to receive notifications.
- Obtain the chat id for this destination (e.g.,
-1001234567890
). - If you're unfamiliar with this process, follow this guide to get the chat ID.
- Notification Receiver Invite Link:
- Create an invite link for the notification receiver channel or group.
- This will be used to check if all events were successfully sent to the notification receiver.
- Example for private channel:
https://t.me/+IybSNCg_1a2b3c4d5e
- Add bot to notification receiver:
- Add the bot to the notification receiver channel or group.
- Give the bot admin privileges to allow it to send messages.
- DSN: Obtain a DSN (Data Source Name) from your Sentry account. This will be used to send error logs and tracking info to Sentry for monitoring the application's health.
mkdir telegram-stats-monitor
cd telegram-stats-monitor
git clone git@github.com:preckrasno/telegram-channel-subs-monitor.git .
python3 -m venv venv
mac/linux
source venv/bin/activate
pip install -r requirements.txt
Create .env file in the root directory and add variables using .env.example file as a template
Ensure the Firebase private key file serviceAccountKey.json
is in the root directory from step 2 on Firebase Firestore setup. Execute the following command and find the file in the root directory
ls
Run the script manually for the first time to login into telegram and create session file
python3 main.py
After logging in, stop the script (ctrl+c)
and deactivate the virtual environment
deactivate
- Edit
telegram-stats-monitor.service
file and changeExecStart
andWorkingDirectory
to the correct path - Move the service file to the systemd directory:
(
telegram-stats-monitor.service
to/etc/systemd/system/
)
sudo cp telegram-stats-monitor.service /etc/systemd/system/
- Reload the systemd daemon to recognize your service:
sudo systemctl daemon-reload
- Enable the service to start on boot:
sudo systemctl enable telegram-stats-monitor.service
- Start the service:
sudo systemctl start telegram-stats-monitor.service
- Check the status of the service (optional):
sudo systemctl status telegram-stats-monitor.service
- To view logs, use the following command. Add the
-e
flag for real-time logs (optional):
sudo journalctl -u telegram-stats-monitor.service
or
sudo journalctl -u telegram-stats-monitor.service -e
- Stopping and restarting the service (optional)
sudo systemctl stop telegram-stats-monitor.service
sudo systemctl restart telegram-stats-monitor.service
- To disable the service from starting on boot (optional):
sudo systemctl disable telegram-stats-monitor.service