Telegram and Real-time Notification Channels

Telegram and Real-time Notification Channels

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » 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.

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 😄