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 manual checks and frustration.

Build a Telegram Bot that Monitors Your Website Uptime — Free

Let's be honest, as developers, we spend way too much time firefighting. A website outage is a classic example – suddenly, your carefully crafted production environment is unresponsive, and you're scrambling to figure out what went wrong. Manually checking uptime with a service like UptimeRobot or Pingdom can be expensive, and let’s face it, you probably forget to check it regularly.

This article shows you how to build a simple Telegram bot that does exactly that – monitors your website uptime and sends you a notification directly to your Telegram chat. It’s free, uses Python, and requires minimal setup. No fancy infrastructure needed.

The Core Insight: Periodic Checks and Telegram Integration

The fundamental idea is this: we’ll use a Python script to periodically ping your website. If the ping fails (meaning the website is unreachable), we’ll send a message to your Telegram bot using the Telegram Bot API. This creates a feedback loop – the bot constantly checks, and you get instant notifications.


import requests

import telegram

import time

Replace with your bot token and chat ID

BOT_TOKEN = "YOUR_BOT_TOKEN"

CHAT_ID = "YOUR_CHAT_ID"

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 unreachable: {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":

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

while True:

if not check_website(website_url):

send_telegram_message(f"⚠️ Website {website_url} is DOWN!")

time.sleep(60) Check every 60 seconds

```

This code snippet demonstrates the core functionality. It uses the `requests` library to make a GET request to your website, and the `telegram` library to send a message to your Telegram bot. The `time.sleep()` function pauses the script for a specified interval (60 seconds in this example) before checking again.

Practical Tip: Using `schedule` for Scheduling

Instead of a `while True` loop, consider using the `schedule` library (https://pypi.org/project/schedule/). It provides a more robust and readable way to schedule tasks. It handles things like gracefully exiting the script if needed, which can be helpful for more complex setups.

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 significant headaches and downtime. This simple example provides a solid foundation for building more sophisticated monitoring solutions. If you're looking for help with automation or custom development projects, check out https://itelnetconsulting.com/ for tailored solutions.

```


Itelnet Consulting

Top comments (0)