Telegram - a popular messaging application known by many. Personally, I have been using Telegram for many years; from the first time I used it, Telegram captured my affection due to its simple interface, focusing on calls, messaging, speed, and multi-platform support.
Besides making calls and exchanging messages, Telegram also has the function of chatting through Groups or Channels. Groups are quite familiar; you can add many people to the conversation to chat together. Meanwhile, Channels are where one person speaks, and many listen, joining a channel to continuously update the latest information from the channel owner.
One special thing about Telegram that many other messaging applications do not have is API support. This means that instead of chatting directly through the app, you can call the API to interact with the conversation, even with Groups or Channels. It's not surprising that Telegram has produced many "BOTS" - a concept referring to users that are not real people but are programmed to perform specific tasks.
As a free messaging application, with API support, speed, and unlimited capacity. If we could "transform" Telegram into an error notification channel, system monitoring, or any information we want to send immediately to many people, it would be very useful. Indeed, since my early programming days, I have participated in many systems that use alerts via Telegram. So today, let's explore how to leverage Telegram as a channel for sending and receiving instant notifications.
As mentioned, Telegram has built-in API and documentation for integration. Readers can refer to Telegram APIs. Notably, the section on Telegram Bot API is important. Because using a BOT is the fastest way to interact with Groups or Channels. Essentially, you can control the BOT through the API to send messages. But for the BOT to send messages, you must first add it to a chat group or channel.
The BOT acts like a regular person; it is created from a user account. When added to a Channel, the BOT can send messages to the Channel via the API. Typically, in an alert system, only one person speaks and many listen, which is very suitable for the nature of Channels in Telegram. You just need to create a channel, name it, add all the people you want to receive notifications, add the BOT, grant it permission to send messages, and start coding to have the BOT automatically send notifications to the channel.
The implementation steps are very simple. You need a Telegram account.
First, you need to create a BOT. Click on @BotFather, select "Start," then enter /newbot
in the chat box and follow the instructions to complete the BOT creation.
After successfully creating the BOT, you will obtain the BOT's token
, copy it, and store it somewhere.
Next, create a new channel.
Add the BOT to the channel by clicking on Channel Name > Subscribers > Add Subscribers > Select the BOT you just created.
Next, you need to obtain the id
of the channel by going to Channel > Edit > Invite Links > Copy the Invite Link.
Send the copied address to @username_to_id_bot to view the Channel's id
. We need the channel's id
to use in the send message API.
The id
is -1001828347283
.
That's it. Now let's try calling the sendMessage
API to see.
$ curl --location --request POST 'https://api.telegram.org/bot5907053822:AAHUed6Tp_XUqnweDYc8ghuZJihf6mtG_X0/sendMessage' \
--header 'Content-Type: application/json' \
--data-raw '{
"chat_id": "-1001828347283",
"text": "Hello World",
"parse_mode": "html"
}'
{"ok":true,"result":{"message_id":3,"sender_chat":{"id":-1001828347283,"title":"2COFFEE DEMO","type":"channel"},"chat":{"id":-1001828347283,"title":"2COFFEE DEMO","type":"channel"},"date":1669134411,"text":"Hello World"}}
Immediately, you will see the message sent to the channel.
Create a sendMessage
function to use in the project.
sendMessage(token: string, chatId: string, message: string) {
return this.http.post({
path: `/bot${token}/sendMessage`,
data: {
chat_id: chatId,
text: message,
parse_mode: "html",
}
});
}
As a messaging service, Telegram has some limits regarding BOTS and the API. For example, each user account cannot create more than 20 BOTS. Avoid sending more than 1 message within 1 second, avoid sending more than 20 messages per minute to the same group or channel...
For more detailed information, readers can refer to Bots FAQ or Telegram Limits.
5 profound lessons
Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!
Subscribe to receive new article notifications
Comments (2)
Oh cảm ơn đã chia sẻ một cách hay
Oh cảm ơn đã chia sẻ một cách hay
Đọc các bài viết của bạn từ hồi viết về EventLoop. Cảm ơn bạn về những kiến thức đã chia sẻ. Keep it up and enjoy your coffee!
Đọc các bài viết của bạn từ hồi viết về EventLoop. Cảm ơn bạn về những kiến thức đã chia sẻ. Keep it up and enjoy your coffee!
Cảm ơn, đọc đc bình luận của bạn mình rất vui. Hy vọng bạn tiếp tục ủng hộ mình trong tương lai 😄