How I created a Telegram Bot to tell me how my SaaS was going and started relaxing.đ€
Some months ago, I launched Stori.
Lots of people started to register and add Instagram Accounts to view and save their storiesâ performance. After 2 days, more than 200 people signed up and somebody bought a subscription. Everything was great! đ
But after some times, I started to think that I havenât full control of everything: was everything really ok? Were the background scripts working? I realized that I havenât a way to be warned if something wasnât right. I started to visit my db more often (ehmm⊠maybe every 10 minutes?) than necessary, just to see if everything was running smoothly.
Yeah, I was in paranoia mode.
So I created a Telegram Bot.
One night I saw this tweet by Pieter Levels
And it just got me that I could be notified when something good (or bad) happened on Stori. I use Telegram every day and I found out that itâs really easy to send a message as a Bot.
Here are the 16 lines of code that currently powers my notification bot:
const axios = require(âaxiosâ);
const token = process.env.TELEGRAM_TOKEN;
const base_URL = `https://api.telegram.org/bot`;
const SEND_MESSAGE_URL = `${base_URL}${token}/sendMessage`;
const default_options = {
chat_id: process.env.TELEGRAM_CHAT_ID,
parse_mode: âHTMLâ,
};
const sendMessage = (opts = { text: âno textâ, json: undefined }) => {
opts = Object.assign(opts, default_options);
if(opts.json){
opts.text = `${opts.text}
<pre>${JSON.stringify(opts.json, null, 2)}</pre>`;
}
return axios.post(SEND_MESSAGE_URL, opts)
}
Et voilĂ ! đ§
I also created some utils function to permit to extract more easily the data that I want (notice the âjsonâ option: in this way I can put whatever data I need to see in a pre
html element đȘ
Now I can be notified when⊠A new user registers
And also (and more important) when there are some problems:
Thatâs it. Now I know exactly whatâs going on on Stori and also itâs super cool to have a Robot that warns you whenever something new happens! đ€