💾 Archived View for wilw.capsule.town › log › 2021-09-09-telegram-notifications.gmi captured on 2023-04-19 at 22:56:10. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
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 [1]). 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.
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 [2] and Healthchecks [3]. 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 [4] 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.
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.
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 [5].
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).
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.
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.
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.
In this post I've briefly covered how to create a simple bot for providing event notifications.
I hope you find it useful!