Why I monitor my server with a Telegram Bot (and how I built it)
It’s been several years that I’m frustrated as fuck with the way I monitor my services and proxies on my main server.
To make it short :
When there is a problem, I receive an email.
And it’s just freaking shitty because I don’t have my gmail always opened and I don’t want to have notifications for every fucking email I receive.
Worst, gmail (this bitch) keep putting certain of my emails in spam randomly (certainly because they don’t come from a recognized domain), so I miss them.
And in a lot of other ways… It’s just… Crap.
And I tried a lot solutions to move away from this :
- creating custom logs on the server (but It does not tell you when stuffs break - but it can help you with the why)
- use a custom PWA with notifications on my phone (but actually the notifications services of the PWA only works when Google Chrome/Firefox is opened in background - so if you’re not a maniac on your phone you could miss it for an entire day - and it arrived several times)
And the conclusion became that I need to build my own little Android App in Java just for that… Receive Notifications.
It felt over-engineered as fuck.
And so I stayed with the email system for years.
With each time, feeling the growing anger when I missed an email
telling me a service was down.
And one time, something insane happened !
One of my student shown me a little project that he did that send him
a custom automatic message through its Telegram.
First : it was cool, so shout out to him !
And second : Euré-fucking-ka !
I could just send me a Telegram Message instead of a mail when something breaks.
So that’s what I did and I got to say, the
Telegram API and process to create a Bot is
certainly the easiest and most straight forward I used in my life.
[!note]
I tried facebook, Instagram even Discord - and they were a nightmare compared to Telegram.
So, how do you create a Telegram Bot exactly ?
Super simple !
The first thing you need to do is to create him.
To do that, you just send /start to the recipient
@BotFather directly on the Telegram app.
It will gave you a <token>.
Done.
After, you just have to call the Telegram REST API
through http like this :
https://api.telegram.org/bot<token>/<yourCommand>
Where <yourCommand> is one of the many end
points Telegram gives you.
For example to send a message :
curl -d '{"chat_id" : "the-chat-id-you-want-to-send-the-msg-to", "text": "The text of your message"}' https://api.telegram.org/bot<token>/sendMessageThat’s it !
I even created a small Python API so it’s faster than
using curl directly :
import socket
import requests
import time
class Bot :
def __init__(self, token) :
self.token = token.split("\n")[0]
def route(self) :
return "https://api.telegram.org/bot" + self.token + "/"
#data is in dictionnary format here - json like
def request(self, function, data):
url = self.route() + function
r = requests.post(url, json = data)
return r.json()Like I said, easy. as. fuck.
[!note]
The code above is open source, just copy/past it and live your best life with it.
So now, like I don’t use Telegram in my day-to-day
life, I know that when I receive a Notification from it, it’s
forcefully my server.
Here it is in action :
And the possibilities from this seam endless !
For now, it’s just monitoring, but we could imagine :
- Automatic stats report
- Alert on resources usage
- Alert on clients behavior
- Send command to manage the server directly through Telegram (using
the
/prefix in your messages) - Even piping a LLM to it ! Why the fuck not ??
OpenClawdid it ! - And so many other stuffs…
[!note]
It works a lot it like aDiscord Botactually but is easier to set up. (I did made one Discord Bot too for managing my school, but it’s a story for another time)
So yeah…
Go create your Telegram bot now !