Telegram Bot: Monitoring Jobs On EC2 With Python
Hey guys! Today, I’m super excited to share a fun project I’ve been working on. Instead of using a full-fledged automation platform like N8N, I decided to roll up my sleeves and build a custom Telegram bot to monitor my computational jobs. This bot keeps an eye on things and alerts me the moment any job fails. I built it using Python, webhooks, and an EC2 instance for self-hosting. Let’s dive into the details!
Why Build a Custom Bot?
Before we get into the how-to, let’s chat about why I went this route. Sure, N8N and similar tools are fantastic, but sometimes you need something tailored precisely to your needs. I wanted a lightweight solution that gave me complete control without the overhead of a larger platform. Plus, it was a great learning experience to build it from scratch!
- Customization: I could define exactly what metrics to monitor and how to report them.
- Cost: Hosting a small Python script on an EC2 instance can be cheaper than using a premium service.
- Learning: Building it myself meant I understood every piece of the puzzle.
Tech Stack
Here’s the lineup of technologies I used for this project:
- Python: The heart of the bot, handling the logic and communication.
- Telegram Bot API: To interact with Telegram and send notifications.
- Webhooks: To receive real-time updates from my computational jobs.
- AWS EC2: For hosting the bot and ensuring it runs 24/7.
Setting Up the Telegram Bot
First things first, you need to create a Telegram bot. It’s surprisingly easy. Just talk to the BotFather on Telegram – it's their official bot for creating and managing other bots. Here’s how:
- Start a chat with BotFather: Search for “BotFather” in Telegram and start a chat.
- Create a new bot: Type
/newbot
and follow the instructions. You’ll need to choose a name and a unique username for your bot. - Get the API token: BotFather will give you an API token. Keep this safe – it’s like the password to your bot. Store it securely; you'll need it later in your Python script.
Writing the Python Script
Now for the fun part – writing the Python script that does all the heavy lifting. Here’s a basic outline of what the script needs to do:
- Set up the environment: Install the necessary libraries, like
requests
for making HTTP requests andpython-telegram-bot
for interacting with the Telegram API. - Define the bot logic: Write functions to handle incoming webhooks, check the status of computational jobs, and send messages to your Telegram account.
- Implement error handling: Make sure the script can gracefully handle errors and unexpected situations.
- Set up the webhook: Register your script with Telegram so it can receive updates.
Sample Python Code
Here’s a simplified version of the Python script:
import requests
import telegram
from flask import Flask, request
# Telegram Bot API token
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'
# Your Telegram chat ID
CHAT_ID = 'YOUR_TELEGRAM_CHAT_ID'
# Initialize the Telegram bot
bot = telegram.Bot(token=TOKEN)
# Initialize Flask app
app = Flask(__name__)
@app.route('/', methods=['POST'])
def webhook_handler():
if request.method == "POST":
data = request.get_json()
# Process the data from the webhook
job_status = data.get('status')
job_id = data.get('job_id')
if job_status == 'failed':
message = f'Job {job_id} failed! Check it out ASAP!'
bot.send_message(chat_id=CHAT_ID, text=message)
return 'OK', 200
if __name__ == '__main__':
app.run(debug=True)
Replace 'YOUR_TELEGRAM_BOT_TOKEN'
and 'YOUR_TELEGRAM_CHAT_ID'
with your actual token and chat ID. This script uses Flask to create a simple web server that listens for incoming webhooks. When a webhook with a failed
status is received, it sends a message to your Telegram account.
Setting Up the EC2 Instance
Next, you’ll need an EC2 instance to host your bot. Here’s a quick rundown:
-
Launch an EC2 instance: Choose an appropriate AMI (Amazon Machine Image) – I recommend Ubuntu or Amazon Linux. Select an instance size that fits your needs; a
t2.micro
is often sufficient for small projects. -
Configure security groups: Make sure your security group allows inbound traffic on port 80 or 443 (for HTTPS) and port 22 (for SSH).
-
Install Python and dependencies: SSH into your instance and install Python and the required libraries using
pip
.sudo apt update sudo apt install python3 python3-pip pip3 install requests python-telegram-bot flask
-
Transfer the script: Copy your Python script to the EC2 instance.
-
Run the script: Start the script using
python3 your_script_name.py
. I recommend using a process manager likescreen
ortmux
to keep the script running in the background.
Configuring Webhooks
To receive real-time updates, you need to configure webhooks for your computational jobs. The exact steps depend on how your jobs are set up, but the basic idea is this:
- Identify the events: Determine which events you want to be notified about (e.g., job completion, failure, etc.).
- Set up the webhook URL: This is the URL of your Python script running on the EC2 instance (e.g.,
http://your_ec2_instance_public_ip/
). - Send a POST request: When an event occurs, send a POST request to the webhook URL with the relevant data in JSON format.
For example, if you're using a job scheduler like Celery, you can configure it to send webhooks when tasks complete or fail. If you're using custom scripts, you can add code to send a POST request using the requests
library in Python.
Securing Your Bot
Security is crucial, especially when dealing with external APIs. Here are some tips to keep your bot secure:
- Use HTTPS: Always use HTTPS for your webhook URL to encrypt the data in transit. You can set up SSL certificates using Let's Encrypt.
- Verify webhook requests: Check the source of incoming webhook requests to ensure they’re coming from your trusted systems.
- Store API tokens securely: Never hardcode API tokens directly in your script. Use environment variables or a secrets management service.
- Rate limiting: Implement rate limiting to prevent abuse and DoS attacks.
Monitoring and Maintenance
Once your bot is up and running, keep an eye on it to make sure it’s working correctly. Here are some things to monitor:
- Error logs: Check the script’s error logs regularly to identify and fix any issues.
- Resource usage: Monitor the EC2 instance’s CPU and memory usage to ensure it’s not being overloaded.
- Telegram API limits: Be aware of Telegram’s API limits and implement appropriate rate limiting to avoid being throttled.
Conclusion
So there you have it! Building a custom Telegram bot to monitor computational jobs is a fun and effective way to stay on top of things. While it requires a bit more effort than using a pre-built solution like N8N, it gives you complete control and a deeper understanding of the underlying technology. Plus, it’s a great excuse to flex those Python skills!
I hope this guide has been helpful. Happy bot-building, and may your computational jobs always run smoothly!