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 hours of manual checks.

Build a Telegram bot that monitors your website uptime – free

Let’s be honest, as developers, we’ve all been there. You’re debugging a complex issue, and suddenly you realize your website is down. You frantically check status pages, ping your server, and hope for the best. It’s a frustrating waste of time. What if there was a way to get notified automatically?

This tutorial shows you how to build a Telegram bot that monitors your website uptime – completely free. We’ll use Python and a few simple libraries to achieve this. It’s a surprisingly effective automation tool, and it’s a great way to learn a bit about bots and uptime monitoring.

The Insight: Simple Checks, Powerful Notifications

The core idea is to periodically check if your website is accessible. If it's not, we send a message to your Telegram channel. The beauty of this is its simplicity. We’re not relying on complex uptime monitoring services; we're building a basic check that’s tailored to our needs. Let's illustrate with a quick example: Imagine you run a small e-commerce site. A downtime alert via Telegram could immediately alert you to a problem that’s preventing customers from purchasing, potentially saving you a significant amount of revenue.

Practical Tip: Using `requests` for HTTP Checks

We'll use the `requests` library to make HTTP requests. It’s a straightforward and reliable way to check if a website is reachable. Here's a simplified example of the Python code:


import requests

import telebot

Replace with your Telegram bot token and website URL

BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"

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

bot = telebot.TeleBot(BOT_TOKEN)

def check_uptime():

try:

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

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

return True

except requests.exceptions.RequestException as e:

return False

if name == 'main':

while True:

if not check_uptime():

bot.send_message("Your website is down!", chat_id="YOUR_TELEGRAM_CHAT_ID") Replace with your chat ID

Add a delay to avoid excessive polling

import time

time.sleep(60) Check every 60 seconds

```

This code checks the website every 60 seconds. If the website is reachable (status code 200), it does nothing. If not, it sends a message to your Telegram channel. Note the `timeout` parameter – this prevents the script from hanging indefinitely if the website is unresponsive.

Tool Recommendation: Telethon for Telegram Interaction

We're using `telebot` for simplicity, but for more complex Telegram interactions (like handling commands or managing multiple channels), consider Telethon. It provides a lower-level interface and gives you much finer control over your bot's functionality. You can find it here: https://telethon.dev/

Conclusion

Building a Telegram bot for website uptime monitoring is a surprisingly effective and free way to proactively manage your online presence. It’s a small investment of time that can save you a huge amount of frustration. This simple automation technique demonstrates the power of combining readily available tools to solve real-world problems.

If you're looking for help with building custom automation tools or need assistance with your website's infrastructure, https://itelnetconsulting.com/ can provide tailored solutions.

```


Itelnet Consulting

Top comments (0)