Creating a Personal Notification System for free with Telegram API
Introduction to Telegram
Telegram is a messaging app, similar to WhatsApp and many others. It's a well-maintained app, powered by many features that give it an edge over its competitors. Today, we are exploring Telegram's API. Telegram features an incredibly simple API to automate almost anything–you can even use it to send messages to youself.
Why do you need a personal notification system?
As developers, our work is constantly running, even when we are away from the keyboard. Being quickly notified if any significant events arise is extremely valuable. For example, it's important to know as soon as possible if a critical error occurrs in our code, or an API is down, or maybe a customer makes a purchase.
Whatever you want to receive notifications for, with Telegram it takes only one HTTP call!
Requirements for setting up Telegram
To begin, first you need to create a Telegram account. Next, you can download the app to your mobile device and set it up there. Once your device is ready, I recommend setting up the web version. You can go to web.telegram.org and follow the instructions. (Note: This is not a requirement since you need to write very little in the app.)
Create a telegram bot
Now that you have a Telegram account, we want to send messages to it. In order to accomplish this, the messages need to be sent from a bot to you.
Creating a bot is achieved by issuing commands to an already existing Bot called BotFather.
First, go to telegram (either on mobile or web), start a chat with @BotFather, and write a new message /new_bot
then send.
BotFather will reply asking you for a name for your new bot. The name can be anything you want; I would suggest choosing a name that makes sense in your use-case. Type your chosen name in a reply and send.
BotFather will now ask for a username. This should be a single word that ends in _bot
. For example test_bot
. Type it in a reply and send.
You will now receive a message with a token. The token looks like this:
1112223333:ME5E7ZH4ux5AiJqoXgzaoSUSlJ8ji1lM
Keep this token safe and don't share it with anyone. This token allows you to control your new bot!
Retrieve the chat ID
Even though the bot is ready for interactions, it can't start chats on its own. Instead, it reacts to chats that are started with it.
To allow your bot to send messages to you, you need to first start a chat with it and retrieve the chat_id
to which the bot will send messages.
You need to make the initial move in this case, so start interacting with your bot. Search for the bot and start a new chat.
This will send a /start
message to the bot, but in order to get the chat_id
you need to make your first API call!
Here you can either use curl or whatever client you want; you just need to make a GET request to this url:
https://api.telegram.org/bot[TOKEN]/getUpdates
Replace [TOKEN]
with the token obtained before from the BotFather–but remember to keep the bot
part in the URL just before the token.
The URL, with the previous token, would look like this:
https://api.telegram.org/bot1112223333:ME5E7ZH4ux5AiJqoXgzaoSUSlJ8ji1lM/getUpdates
You will get a JSON response like this:
{
"ok": true,
"result": [
{
"update_id": 116206660,
"message": {
"message_id": 5,
"from": {
"id": 921535000,
"is_bot": false,
"first_name": "Simone",
"username": "Duiker101",
"language_code": "en"
},
"chat": {
"id": 921535000,
"first_name": "Simone",
"username": "Duiker101",
"type": "private"
},
"date": 1595283175,
"text": "/start",
"entities": [
{
"offset": 0,
"length": 6,
"type": "bot_command"
}
]
}
}
]
}
The key here is results[0].message.chat.id
(here it's 921535000), that's our chat_id
.
Sending messages with a GET request
Ultimately, sending a message to your chat is as easy as doing a GET request to the following URL:
https://api.telegram.org/bot[TOKEN]/sendMessage?chat_id=[CHAT_ID]&text=[YOUR_TEXT]
Just replace [TOKEN]
, [CHAT_ID]
and [YOUR_TEXT]
with the corresponding data and you are good to go!
That's literally all it takes to set up your personal notification system! Now, you can call this url and get a message with notifications directly to your phone.
Cool! But why not just send an email?
Emails are... OK. Emails have a very specific role for me in that they are my main method of communication for both personal and business matters, so I don't want to clutter my inbox or have a separate email address. On top of that, most email APIs have rather strict limits, whereas Telegram has none! Of course, you could set up your own server etc... but then this guide would be ten times as long.
On top of that, there are quite a few advanced possibilities that are definitely made easier with the TelegramAPI as opposed to email, such as setting up a webhook to handle responses.
What can you do with a personal notification system?
As we mentioned before, there are a lot of possible uses that can be applied:
- Notification when a sale has been made
- Customer support requests
- Alerts about critical failures in a system
- Daily statistic reports
And if you want a nice challenge, you can take it even further! You can setup a webhook to send commands to your bot and make it respond to you in real time. If you accomplish this, you can specify exactly what you want it to do!