DEV Community

David García
David García

Posted on

Build a Telegram bot that monitors your website uptime — free

```html

TL;DR: You can build a Telegram bot that automatically tells you if your website is down, saving you the hassle of manually checking.

Build a Telegram Bot that Monitors Your Website Uptime – Free

Let's be honest, nobody likes getting a notification that their website is down. It’s bad for business, bad for your reputation, and frankly, a pain to troubleshoot. Instead of relying on sporadic checks, we’re going to build a simple Telegram bot that does this for you – completely free.

The Insight: Simple HTTP Checks with Python

The core idea is pretty straightforward: we’ll use Python to periodically check if your website is reachable. If it’s not, we’ll send a message to your Telegram channel. This isn't about complex monitoring systems; it's about a reliable, automated way to know when something goes wrong.


import requests

import telegram

import time

Replace with your bot token and chat ID

BOT_TOKEN = "YOUR_BOT_TOKEN"

CHAT_ID = "YOUR_CHAT_ID"

WEBSITE_URL = "https://www.example.com" Replace with your website

bot = telegram.Bot(token=BOT_TOKEN)

def check_website():

try:

response = requests.get(WEBSITE_URL, timeout=5) 5 second timeout

response.raise_for_status() Raise HTTPError for bad responses (4xx or 5xx)

bot.send_message(chat_id=CHAT_ID, text=f"Website {WEBSITE_URL} is UP!")

except requests.exceptions.RequestException as e:

bot.send_message(chat_id=CHAT_ID, text=f"Website {WEBSITE_URL} is DOWN! Error: {e}")

while True:

check_website()

time.sleep(60) Check every 60 seconds

This code uses the `requests` library to make an HTTP GET request to your website. The `timeout` parameter prevents the script from hanging indefinitely if the website is unresponsive. `response.raise_for_status()` is crucial – it automatically throws an exception if the website returns an error code (like 404 or 500), which we then use to trigger the "website down" notification.

Practical Tip: Using `cron` or a similar scheduler

Running this script directly from your terminal isn't ideal for continuous monitoring. To keep it running reliably, you’ll want to schedule it using a tool like `cron` (on Linux/macOS) or Task Scheduler (on Windows). This will automatically run the Python script at regular intervals without you having to manually start it.

Conclusion

Building this Telegram bot is a fantastic way to quickly and effectively monitor your website's uptime. It’s a small investment of time and effort that can save you significant headaches. It’s a great exercise in automation and demonstrates the power of combining simple tools to solve a common problem.

If you're looking for help with similar automation projects, or need assistance with website monitoring solutions, feel free to contact me. I specialize in building custom automation tools to improve your workflow and reduce operational overhead.

```


Itelnet Consulting

Top comments (0)