Telegram and Real-time Notification Channels

Telegram and Real-time Notification Channels

Daily short news for you
  • A software that converts text to speech created by a Vietnamese programmer - J2TEAM - Text to Speech (Free). You can convert dozens of languages into dozens of different natural voices. The special thing is that it is free.

    In preliminary evaluation, the conversion of long texts or texts in pure Vietnamese is very good. However, when it includes English words, it sounds a bit funny 😅

    » Read more
  • How terrifying, Codeium - known as a competitor to Github Copilot, as it allows users to use it for free without limits. Recently, they introduced the Windsurf Editor - no longer just a VSCode Extension but a full Editor now - directly competing with Cursor. And the main point is that it... is completely free 🫣.

    » Read more
  • There is a rather interesting study that I came across: "Users never bother to read things they don't want to." (That's a bold statement, but it's more true than not. 😅)

    Don't believe it? I bet you've encountered situations where you've clicked on a button repeatedly and it doesn't respond, but in reality, it has displayed an error message somewhere. Or you've filled out everything and then when you hit the submit button, it doesn't go through. Frustrated, you scroll up or down to read and find out... oh, it turns out there's an extra step or two you need to take, right?

    It’s not far from the blog here. I thought that anyone who cares about the blog would click on the "Allow notifications" button just below the post. But the truth is, no one bothers to click it. Is it because they don't want to receive notifications? Probably not! I think it's because they just didn’t read that line.

    The evidence is that only when a notification pops up and takes up half the screen, or suddenly appears to grab attention, do they actually read it—and of course, it attracts a few more subscribers—something that was never achieved before.

    » Read more

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.

Telegram BotFather

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

Telegram BOT token

Next, click the button to create a new channel.

Create a new channel

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.

Get Invite Link

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

Get Channel ID

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.

First message sent to channel

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.

Premium
Hello

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!

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!

View all

Subscribe to receive new article notifications

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)

Leave a comment...
Avatar
Trần Huy Hoàng2 years ago
Oh cảm ơn đã chia sẻ một cách hay
Reply
Avatar
Tuan Nguyen2 years 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ống2 years 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 😄