Unfortunately, drone attacks on Ukrainian territory occur daily. In my experience, Telegram monitoring channels are the most popular sources of timely information during such attacks.
Having immediate access to a proper shelter is a luxury—most people simply seek safety in the most secure area of their homes. However, drone attacks frequently last hours, often throughout the night, making it exhausting to manually monitor these Telegram channels continuously.
To help my family and myself manage this stress and maintain alertness efficiently, I built this Telegram bot.
It won't create an impenetrable shield, but it does remove the need to constantly watch monitoring channels personally. When your selected locations appear in monitored Telegram channels, the bot sends an immediate alert—letting you know that danger is close by.
If you'd like to create a similar bot for yourself, feel free to do so. I hope my code provides a helpful starting point.
Stay safe. 🌻🇺🇦
This bot tracks real-time messages from selected Telegram channels, scanning them for mentions of user-specified locations. Upon detecting predefined keywords, it immediately sends alerts to users, keeping them promptly informed of relevant drone activity.
- Real-time monitoring of Telegram channels via Telethon.
- Webhook-based notifications. Tested using polling too.
- Dynamic user preferences with customizable location monitoring (persisted in JSON format).
- Designed for deployment: Tested locally and deployed on AWS Lightsail with HTTPS webhook enabled (using a reverse proxy via Caddy and Let’s Encrypt SSL certificates).
telethonfor interacting with Telegram API.python-telegram-botfor bot interaction handling.- FastAPI + Uvicorn for lightweight webhook server.
- Caddy as a reverse proxy and automated TLS certificate management.
Deployed on AWS Lightsail instance (Ubuntu-based), using Cloudflare for domain management and Caddy for automatic HTTPS and reverse proxy handling.
Realized that monitoring multiple Telegram channels continuously is impractical.
Creating an automated Telegram bot to monitor selected channels is a solution.
How? By alerting users immediately if certain keywords (e.g., names of local towns or districts) appear, signaling potential danger nearby.
Researched Telegram API and relevant libraries. Decided on the following tech stack:
- Python
telethon(to be able to read messages from monitoring channels)python-telegram-botlibrary for sending messages and managing user interaction.
Received api_id and api_hash for Telegram client.
Via @BotFather created my bot and received bot_token as its unique identifier.
Note
Here is my bot, by the way, @irpinalert_bot. I start it almost every evening only for the nighttime (GMT+2).
Developed the bot initially using polling (simpler implementation, more information).
Set up Telegram handlers for bot command (/start command).
Created my own Telegram channel for testing notification.
Bot was able to handle only one user for now.
Polling approach worked well, but the bot asked for updates so frequently, that it looked like spamming Telegram API with requests.
getUpdates is a pull mechanism
setWebhook is push
I also had in mind, that my local computer will not do as a server for bot working at night (or if there is a problem with electricity in my region due to russian attacks). So I choosed to relocate to cloud - AWS Lightsail.
Setting a webhook on a new server was quite challenging, as it requires a lot of details to consider. But I've managed to do it anyway 😄
Official webhook guide helped for the start.
Tech stack:
- Ngrok for testing
- FastAPI + Uvicorn for webserver setup
- Caddy for TLS certificates and reverse proxy
Researched and tested webhook setup using Ngrok locally to understand webhook mechanics.
Created an Ubuntu instance on AWS Lightsail for reliable 24/7 deployment. Configured a static IPv4 address (IPv6 is not supported for Telegram webhooks).
Installed Python packages in a virtual environment to maintain clean dependency management.
Registered a domain name via Cloudflare and used it as DNS provider.
Installed and configured Caddy to automatically manage SSL certificates through Let's Encrypt and work as reverse proxy.
Integrated FastAPI server on Lightsail to receive webhook updates.
Debugged webhook communication between Telegram, Cloudflare (DNS), and AWS Lightsail server. Enabled 'Full' encryption mode in CloudFlare (not 'Flexible').
- Finally, ensured stable HTTPS connection end-to-end.
Added command (/location) to let users select their desired monitoring locations.
Stored users preferences and chats ids in a JSON file, ensuring persistence between server restarts. Now bot supports multiple users and remembers chosen locations.
New features were implemented firstly with polling mechanism for ease of debugging.
New features added to webhook version.
Managed bot lifecycle with tmux on AWS Lightsail to keep bot running after SSH logout.
Monitored memory and resource usage on AWS Lightsail to ensure stable performance. Used SWAP apace as Lightsails free instance has little RAM capacity.
- Confirmed successful notification delivery when target words appeared in monitored channels.
| Step | Description |
|---|---|
| Initial bot script (polling) | 1_dronealertbot_polling.py |
| Initial bot script (webhook) | 2_dronealertbot_webhook.py |
| Multiple users and location choice added (polling) | 3_dronealertbot_polling_new_features.py |
| Multiple users and location choice added (webhook) | 4_dronealertbot_webhook_new_features.py |