Telegram and Real-time Notification Channels

Telegram and Real-time Notification Channels

Daily short news for you
  • Not to be outdone, Google recently introduced Gemini CLI - a type of AI agent similar to Codex or Claude Code.

    What’s notable is that they offer up to... 1000 queries for free each day. That’s quite a lot. Additionally, they have open-sourced this project to ensure transparency, learning, and research 🤓

    » Read more
  • There’s another tool to help quickly search command history: atuinsh/atuin.

    What’s interesting is that it uses SQLite for storage. It also provides the feature of completely synchronizing (encrypting) history between machines. How cool is that 🤓

    » Read more
  • I am really impressed with the gemma-3n-E4B model from Google. This is one of the promising models that aims to bring large language models to run on mobile devices or the web or embedded...

    It feels like it understands the prompts better, because I’ve tried many low-parameter models that often overlook my prompts. For example, when I say, "Just return the answer, no need for further explanation," many still tend to add introductory phrases or explanations... but with gemma-3n, the responses are very much to the point.

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