Will's avatar

⬅️ See more posts

Using Telegram bots to receive updates from your automated systems

9 September 2021 (4 minute read)

🔮 This post is also available via Gemini.

100daystooffload technology

💯 100 Days to Offload

This article is one of a series of posts I have written for the 100 Days to Offload challenge. Disclaimer: The challenge focuses on writing frequency rather than quality, and so posts may not always be fully planned out!

View other posts in this series.

The need for notifications

I self-host several services on various servers - for both some professional and personal uses.

I use automated backup scripts to periodically sync data to Backblaze (which I recently posted about). However, once they were setup I would often worry about whether they were working properly. To verify, I’d have to log into Backblaze and check when the latest backups came through.

Although I trusted the process, this became a bit of a pain and more and more of a constant worry. The script might crash, run out of storage space, or anything else, and I wouldn’t know about it unless I actually checked.

For peace of mind I wanted to be able to receive a notification each time the backups completed successfully.

Note: the same is true also for any automated system. For example, service crashes, key events, visitors, new registrations, etc. Such notifications are great for keeping in the loop without needing to actively check.

Choosing how to receive notifications

To start with, I needed to find a mechanism for receiving the notifications. I looked around for suitable methods and noticed a number of commercial offerings (many with free tiers), such as Pushover and Healthchecks. Some of these required installation of new apps or weren’t quite right for my needs.

Another option was to rely on Matrix, but although I have the Element app on my phone already I don’t use it a whole lot and I ideally just want to set something up for the long-term without needing to change it later.

So I finally settled on Telegram. It’s an app I use every day on all of my devices anyway, and it would fit nicely into my existing workflows.

Using Telegram to receive event notifications

Luckily, Telegram offers a great API around the notion of bots. I created a simple bot that would send me a short message each time the backups completed. I’ll explain the process below, which should take less five minutes to complete.

Creating the bot

Telegram bots are managed through an existing bot, called the BotFather. Begin a new chat with this bot by searching for it or by clicking this link directly.

Once you’ve started the chat, type /newbot to kickoff the bot-creation flow. BotFather will ask a few questions about your bot (e.g. a name and username), and will then create it for you. Given Telegram bots can be used by anyone, you’ll need a unique username for your bot.

After the bot has been created, BotFather will give you an API token you can use to interact with your bot (i.e. read and send messages).

Creating a chat with your new bot

The next stage is to begin a chat with your new bot. In Telegram, each chat has a unique ID, and we need to use this ID in order for your bot to send messages to the chat.

First of all, open a chat with your new bot by either following the link to it from BotFather or by searching for its username. When the chat opens, tap /start to begin the chat.

Once you have initiated the conversation, the chat will be created. The next step is to retrieve the chat ID from the Telegram API.

To do so, use cURL (or just your web browser) to make a GET request to https://api.telegram.org/bot<API TOKEN>/getUpdates, replacing <API TOKEN> with the token provided earlier by BotFather.

Some JSON will be displayed, describing the chats your bot is involved in. Given that no one knows about your bot yet, the results should contain just one entry, and will display your own username and details. Copy or make a note of the chat ID for your chat, as we’ll need that next.

Sending notifications

The final part is now to use these details to send notifications from your bot. This is done by making a request to the following URL: https://api.telegram.org/bot<API TOKEN>/sendMessage?chat_id=<CHAT ID>&text=<YOUR MESSAGE> (again, replacing the bits in < > with your own values).

Your chat with the bot should receive the message and you’ll get a notification.

You can now add a simple curl command to the end of your important scripts, or embed a call to the URL in your applications to get updates about key events.

Customising your bot

You can also customise your bot with an avatar image and other details. To do so, head back over to your chat with BotFather and use the /mybots command to get started.

Conclusion

In this post I’ve briefly covered how to create a simple bot for providing event notifications.

I hope you find it useful!

✉️ You can reply to this post via email.

📲 Subscribe to updates

If you would like to read more posts like this, then you can subscribe via RSS.