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 checks your website's uptime and notifies you instantly if it goes down – all for free using Python and a little bit of Telegram Bot API knowledge.

Build a Telegram Bot that Monitors Your Website Uptime – Free

Let's be honest, uptime is critical. A website outage can mean lost revenue, damaged reputation, and frustrated users. Traditional uptime monitoring services are great, but they often come with subscription costs. What if you could get a simple, instant notification directly to your Telegram app? That's exactly what we’re going to build.

The Insight: Automation is Your Friend

The core idea here is simple automation. We'll use Python to periodically check the status of your website and, if it’s down, send a message to a Telegram bot. This avoids constantly checking manually, saving you time and ensuring you're alerted immediately.

Here’s a basic example of how the Python code might look (simplified for clarity):


import requests

import telegram

import time

Replace with your Telegram bot token and website URL

BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"

CHAT_ID = "YOUR_TELEGRAM_CHAT_ID"

WEBSITE_URL = "https://www.example.com"

def check_website(url):

try:

response = requests.get(url, timeout=5)

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

return True

except requests.exceptions.RequestException as e:

print(f"Website unavailable: {e}")

return False

def send_telegram_message(message):

bot = telegram.Bot(token=BOT_TOKEN)

bot.send_message(chat_id=CHAT_ID, text=message)

if name == "main":

while True:

if not check_website(WEBSITE_URL):

message = f"⚠️ Website {WEBSITE_URL} is DOWN!"

send_telegram_message(message)

print(message)

else:

print(f"Website {WEBSITE_URL} is UP!")

time.sleep(60) Check every 60 seconds

```

Practical Tip: Use `requests` for Easy HTTP Requests

The `requests` library is your best friend for making HTTP requests in Python. It’s incredibly easy to use and handles a lot of the complexity for you. If you're not familiar with it, check out the documentation: https://requests.readthedocs.io/en/latest/

For more robust error handling, consider adding more specific exception handling beyond just `requests.exceptions.RequestException`. You might want to handle specific HTTP status codes (e.g., 404 Not Found) differently.

Conclusion

Building this Telegram bot is a fantastic way to proactively monitor your website’s health. It’s a small investment of time that can save you a lot of headaches. This tutorial provides a solid foundation – feel free to expand on it with more sophisticated monitoring features, logging, and alerts.

Need help with complex automation projects or website performance optimization? I specialize in helping businesses streamline their operations. Learn more about my services at https://itelnetconsulting.com/.

```


Itelnet Consulting

Top comments (0)