---
date: "2021-09-09T10:17:00Z"
title: "Using Telegram bots to receive updates from your automated systems"
description: "How to create a Telegram bot to alert you about important updates from your servers or applications."
tags: [100daystooffload, technology]
---

## 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](/blog/2021/05/18/b2-backups)). 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](https://pushover.net) and [Healthchecks](https://healthchecks.io). 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](https://element.io) 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](https://t.me/botfather).

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!
