What it Takes to Building a High-Performance Telegram Bot
Licensed image by Penderev
Telegram is an instant messaging service just like WhatsApp, Facebook Messenger, and WeChat. It has gained popularity in recent years for various reasons: its non-profit nature, cross-platform support, promises of security, and its open APIs to build any integration or bot tailored towards your needs.
Especially in the world of crypto, Telegram bots are a common thing to regulate ICO signup, airdrop campaigns, or any sort of verification. The GroupButler bot is used often to verify users via a captcha, remove links in chats, or prevent the chat from flooding. Let’s take a look!
Telegram API Performance Limitations
Currently, Telegram is limited to sending a maximum of 100 requests per second to your backend. The sooner you are able to process this request and reply, the sooner users will see the changes in the bot client.
However, the problem starts when the backend is unable to process the user’s request or puts it in a queue to be processed. Whenever the Telegram API detects a request has not been processed, it will start to repeatedly resend the request, flooding the backend on its own. Only one minute of downtime is enough for a high-performance bot to receive a backlog of 5 to 15 minutes because of these repeated requests called “REQUIREMENTS OF PROVIDED CALLS”.
First of all, the programming language matters a lot. It is wise to choose any high-performance language like Rust, Golang, or even Scala.
Another aid is the implementation of a load balancer. You would think it mainly helps to increase speed, but it is not. When a user does not receive a response in less than one or two seconds, they tend to resubmit the request or try other things that can possibly break the flow. Therefore, a load balancer is much needed to filter out redundant requests or wrong actions to guarantee fault tolerance.
Besides that, the BUTTON wallet team recommends using a SQL database as it’s a more consistent solution than MongoDB for example. Also, it allows you to assign responsibility for the consistency of the data to the relational database itself, using Merge or Upset operation instead of Insert for each query.
At last, Redis is a great solution for storing all queries in it for a short amount of time and slowly process them. Redis will also allow you to easily scale your bot if one instance of the application fails. Through Redis, you can organize a queue of messages, for example. Thus, you can eliminate errors when you receive repeated requests from Telegram.
If you have complex navigation with a lot of business logic, you need to think about a pattern like MVC or MVVM (View as a layer to display info to the user via abstractions in bot).
We have seen a lot of users who don’t have any normal handling of double responses and navigation logic. All of them are absolutely not user-friendly and are looking dump.
Therefore, the BUTTON wallet team came to the conclusion that it is rather stupid to write a lot of ifelse to different variants of answering and processing user input and pollutes the code. That’s why we decided to abstract from the fact that the user interacts through bots and generalize it so that the user sees some “pages”.
UI Tips For Building Bots
It is highly recommended to use a combination of text and emojis to make things stand out or add additional information about what the button is capable of doing.
A keyboard (view with buttons) provided by a bot can be hard to understand as there is limited space for text, therefore adding emojis is a smart move.
In general, keep things simple and do not add too many buttons to your keyboard view at once. It will only lead to confusion and wrong user input. Limit the number of options to maximum two or three.
As it is fun to code your own Telegram bot, there are definitely a lot of important decisions to be made when scaling your bot to a more high-performance level. As you can read, Telegram has it’s limitations, however, it is possible to avoid these using the right tools like a load balancer, caching, queuing or even changing the programming language. Good luck!
Experiences Building a High-Performance Telegram Bot was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.