Telegram and Real-time Notification Channels

Telegram and Real-time Notification Channels

Daily short news for you
  • Previously, there was a mention of openai/codex - a type of agent that runs conveniently in the Terminal from OpenAI, especially since it is open source and they have now added support for other providers instead of just using the chatgpt model as before.

    Recently, Anthropic also introduced Claude Code which is quite similar to Codex, except it is not open source and you are required to use their API. Since I don't have money to experiment, I've only heard that people in the programming community praise it a lot, and it might even be better than Cursor. On the flip side, there's the risk of burning a hole in your wallet at any moment 😨

    » Read more
  • For a long time, I have been thinking about how to increase brand presence, as well as users for the blog. After much contemplation, it seems the only way is to share on social media or hope they seek it out, until...

    Wearing this shirt means no more worries about traffic jams, the more crowded it gets, the more fun it is because hundreds of eyes are watching 🤓

    (It really works, you know 🤭)

    » Read more
  • A cycle of developing many projects is quite interesting. Summarized in 3 steps: See something complex -> Simplify it -> Add features until it becomes complex again... -> Back to a new loop.

    Why is that? Let me give you 2 examples to illustrate.

    Markdown was created with the aim of producing a plain text format that is "easy to write, easy to read, and easy to convert into something like HTML." At that time, no one had the patience to sit and write while also adding formatting for how the text displayed on the web. Yet now, people are "stuffing" or creating variations based on markdown to add so many new formats that… they can’t even remember all the syntax.

    React is also an example. Since the time of PHP, there has been a desire to create something that clearly separates the user interface from the core logic processing of applications into two distinct parts for better readability and writing. The result is that UI/UX libraries have developed very robustly, providing excellent user interaction, while the application logic resides on a separate server. The duo of Front-end and Back-end emerged from this, with the indispensable REST API waiter. Yet now, React doesn’t look much different from PHP, leading to Vue, Svelte... all converging back to a single point.

    However, the loop is not bad; on the contrary, this loop is more about evolution than "regression." Sometimes, it creates something good from something old, and people rely on that goodness to continue the loop. In other words, it’s about distilling the essence little by little 😁

    » Read more

Issues

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.

Channel and BOT in Telegram

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.

Steps to Implement

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.

Telegram BotFather

After successfully creating the BOT, you will obtain the BOT's token, copy it, and store it somewhere.

Telegram BOT token

Next, create a new channel.

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

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

Get Channel ID

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.

First message 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",  
    }  
  });  
}  

API Limitations

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.

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 😄