Back

Francesco Marassi

Full-Stack developer & UI/UX designer.

How 16 lines of code helped me be more relaxed

April 26, 2018

How I created a Telegram Bot to tell me how my SaaS was going and started relaxing.🤖

intro

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 new user

Somebody subscribe a plan subscribe

A new account is created new account

And also (and more important) when there are some problems: account disabled

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! 🤖