Make your Telegram Bot faster
Have you ever wondered how you could write a fast Telegram Bot in any language?
I did and I’ll give you some tips to make your bot run faster!
There is no code in this article, I’ll make code examples in next ones.
Server location
Never tought about it?
The server where your bot is located impacts on the overall speed of your bot. Hosting your bot in a location far away from Telegram servers can really slow down everything. Telegram bot servers are located in Central Europe, so make sure to rent a machine there.
I tried to ping Telegram bot servers from two different locations:
Italy:
Germany:
Look at the ping, there is a difference of 30ms!
What does this mean?
Server location is really important if you want a really fast bot! The distance between your server and Telegram servers adds a delay in your responce time. The closer your server is to Telegram ones the ligher the lighter the delay becomes!
The bot framework
Sometimes using a bot framework is a bad idea because most of them make “easier to write bots”, hiding some bad and awful design decisions!
How most framework send replies
The most used way to retrive updates is to set a webhook so you can process request as they come without worring to keep doing requests to Telegram APIs.
In the typical bot framework you set the webhook to your server. Then there is always a function like sendMessage(…)
. Most of the time this function (as every other send function) is a wrapper to a new HTTP request to Telegram APIs!
Bad! Why you need to open a new connection to Telegram when you have already one? Creating a new HTTPS connection to a server is a slow and expensive operation to avoid! You have to wait to the OS that need to open a new socket, then the library does the tls handshake and only then you are ready to transmit your message! You have just wasted a lot of time.
As Telegram APIs doc says:
If you’re using webhooks, you can perform a request to the Bot API while sending an answer to the webhook. Use either application/json or application/x-www-form-urlencoded or multipart/form-data response content type for passing parameters. Specify the method to be invoked in the method parameter of the request. It’s not possible to know that such a request was successful or get its result.
you can send back a reply to the connection that Telegram server used to send you the webhook!
Formatting your reply
So how do I format my “direct reply” to send back?
It’s super easy, you just need to send back a json formatted as:
{
“method”: “api_method”,
“paramter”: “…”
}
For example if we want to send a text message:
{
"method": "sendMessage",
"chat_id": <your chat id>,
"text": "Your message text!"
}
Blocking operations
Blocking operations are all the inscructions that block your thread exceution, like a database request, a socket read, ecc.
You should always avoid blocking operations while processing your requests! This slow down the responce time of every request because your blocked thread won’t be able to process a new request while is wating (for example) for a database query to complete!
Every blocking operation that is NOT necessary to your reply SHOULD be run in a diffrent context, maybe on a dedicated thread!
To wrap up this article we saw three common things that slow down your bot. Now that you know them, you can optimize your bot!