Telegram and Real-time Notification Channels

Telegram and Real-time Notification Channels

Issue

You may already be familiar with the messaging app Telegram, which allows us to make calls and send messages over the internet quickly and without any limitations that affect basic communication functions. Personally, I find that Telegram focuses well on messaging. The message sending speed is fast, and I have never experienced missing any messages or notifications.

In addition to making calls and messaging back and forth, Telegram also supports communication through Groups and Channels. Groups are already familiar to us, where we can add multiple people at once to have a conversation. Channels, on the other hand, work a little differently. They are places where people can subscribe to receive messages whenever the Channel's administrator sends a message.

Combined with the excellent performance of the application, Channels bring many conveniences. If we can utilize Telegram to send "alerts" such as API errors or system downtime… any information that needs immediate alerting in an information system/software when an issue occurs, it would be great.

Introduction to Channels and Telegram BOT

Telegram provides APIs and documentation for integrating their APIs. You can find more information at Telegram APIs. In this section, noteworthy is the Telegram Bot API. Because in this article, I will guide you on how to use Telegram's BOT to send messages to Channels.

A BOT in Telegram plays a role similar to that of a regular user. It is created from a user account. When added to a Channel, the BOT can send messages into the Channel through the API.

The simulation of the notification process is very simple. You create a Channel as a Private channel for the purpose of sending/receiving notifications, add the accounts that want to receive notifications, and finally, add the BOT you just created to serve the purpose of sending messages.

In reality, there are many cases where we want to receive notifications immediately. For example, you may want to receive a notification when calling a third-party API error, or you may want to monitor important notifications in a system in order to take timely actions… Of course, you can still implement tracking or logging services, but let's differentiate these two tasks from receiving real-time notifications. Logging is an action to record changes for traceability, while real-time notifications require receiving important information immediately.

Implementing a BOT to send messages to Channels

The implementation steps are very simple. You just need to have a Telegram account.

First, chat with @BotFather, click on "Start", then enter "/newbot" in the chat box and follow the instructions to complete the BOT creation.

banner

After successfully creating the BOT, you will get the token of the BOT. Copy it for later use when sending messages.

banner

Next, click the button to create a new channel.

banner

Add the BOT to the channel by clicking on the channel name > Subscribers > Add Subscribers > Add the recently created BOT to the Channel.

Next, get the link of the channel by going to Channel > Edit > Invite Links > Copy the Invite Link.

banner

Send the copied link to @usernametoid_bot to view the ID of the Channel. We need the ID of the channel to use in the API for sending messages.

banner

Now you can call the sendMessage API to have the BOT send messages to the channel.

$ curl --location --request POST 'https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMessage' \
--header 'Content-Type: application/json' \
--data-raw '{
    "chat_id": "<YOUR_CHANNEL_ID>",  
    "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"}}

You will immediately see the message sent to the channel.

banner

You can create a simple sendMessage function and use it in your 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",  
    }
  });
}

Limitations

As a messaging service, Telegram imposes certain limits on BOTs and when calling their APIs. For example, you cannot create more than 20 BOTs, avoid sending more than 1 message within 1 second, and avoid sending more than 20 messages per minute to the same group/channel.

You can refer to more information at Bots FAQ or Telegram Limits.

Conclusion

In this article, I hope to provide you with a way to receive real-time notifications to prompt immediate actions when managing internet services/servers. This is just a small trick and not a complete replacement for the need for professional tracking or logging, so use it wisely. Besides Telegram, there are still other platforms like Slack, Discord… where you can achieve similar results.

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.
Author

Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.

Did you find this article helpful?
NoYes

Comments (2)

Avatar
Trần Huy Hoàng1 year ago
Oh cảm ơn đã chia sẻ một cách hay
Reply
Avatar
Tuan Nguyen1 year ago
Đọ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!
Reply
Avatar
Xuân Hoài Tống1 year ago
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 😄