Build a Website Uptime Monitor with Python & Telegram Bot (Free)

Is your website online right now? Are you sure? There is nothing worse for a SysAdmin than a client or a boss calling you to ask "Why is the website broken?" before you even know it's down. That is embarrassing and unprofessional.

Instead of manually refreshing your site or staring at your aaPanel logs all day, let's build a dedicated "Watchdog". We will write a simple Python script that monitors your server 24/7 and sends a Telegram notification instantly if something breaks.

Stop paying for uptime monitoring services. Write a simple Python script that pings your server every 5 minutes and sends a Telegram alert if down

The Logic Behind the Monitor

  1. The script sends a simple HTTP Request to your site.
  2. It checks the Status Code. If it is 200, everything is fine.
  3. If it receives 500 (Internal Error), 503 (Service Unavailable), or if the request Times Out, it triggers the alert.

The Script

import requests
import time

URL = "https://dailyinnovatetech.com"
BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
CHAT_ID = "YOUR_CHAT_ID"

def send_alert(msg):
    # Sends message via Telegram API
    send_url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?chat_id={CHAT_ID}&text={msg}"
    requests.get(send_url)

while True:
    try:
        # We set a 10-second timeout. If server is too slow, it counts as down.
        response = requests.get(URL, timeout=10)
        
        if response.status_code != 200:
            send_alert(f"⚠️ ALERT: Site is down! Status: {response.status_code}")
            
    except Exception as e:
        # Handles DNS errors or total connection failure
        send_alert(f"🚨 CRITICAL: Site is unreachable! Error: {e}")
    
    time.sleep(300) # Sleep for 5 minutes before checking again

Conclusion

Uptime monitoring doesn't have to be expensive. With this simple script, you get your own personalized alerting system. For best results, run this script on a different network (like a cheap $5 VPS or even your home PC) so it can monitor your server from the "outside".


Author: Marg | Daily Innovate Tech

Post a Comment for "Build a Website Uptime Monitor with Python & Telegram Bot (Free)"