Telegram and Real-time Notification Channels

Telegram and Real-time Notification Channels

Daily short news for you
  • Today, I accidentally came across the website notes.andymatuschak.org which has a very interesting note-taking method. Clicking on a link opens a new tab next to it. Each time you click, it continues to open. Just like filing cabinets.

    This presentation style is not only easy to follow but also aligns with the flow of thought. It's a pity that I can't find the author of this open-source project. I wonder if there's anything similar out there 🤔

    » Read more
  • Instagram has just introduced a video editing app for content creators called Edits. This is a direct competition with popular apps on the market. However, it might take some time because looking at the features, they are still quite basic, just enough to use.

    By the way, I've been "playing" on IG for over 10 years now. Anyone with IG, please leave your account for me to check out 🥳.

    My IG is hoaitx_

    » Read more
  • I signed up for Cursor for the second month now and just realized I haven't used all 500 premium requests per month 😳

    Specifically, just over 100 🙏

    » 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

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

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

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 😄