skip to Main Content

I am building a telegram bot.

This means I am managing several chats at once (each with a unique chat id).

When a user sends a message to the bot replies with an answer.

I am using the telegram API which is over HTTP.

However, when I want to send a few messages sometimes they arrive in the wrong order as it is uncertain which HTTP request will be accepted first.

Is there any undocumented way to send a sequence parameter to the telegram API or must I handle the HTTP requests in a queue ?

4

Answers


  1. I built an bot with telegram-cli and got the same issue.
    So I think it is Telegram specific to send messages with a short time delta as a bundle

    Login or Signup to reply.
  2. Telegram bots are supposed to have a human-like interaction. So my advise to avoid your problem would be to define a sort TYPINGSPEED constant and combine it with the sendChatAction() bot api call so that if “feels” like your messages are actually being written by a ‘fast-typing’ human being.

    In ruby, it would give something like this :

    your_msg.split("n").each do |line|
        writing_time=line.length/TYPINGSPEED
        TelegramBot.client.api.sendChatAction(chat_id: chat_id, action: "typing")
        sleep(writing_time)
        TelegramBot.client.api.sendMessage(your_options)
    end
    

    sendChatAction nicely displays “nameofyourbot is writing…” on the user screen.

    Of course, the drawback with this code is that the process handling the answer won’t return before several seconds if your message is long (because of the use of the sleep function). So depending of the number of simultaneous users using your bot, you’d better do some capacity planning and spawn as many process as you need to make sure your bot can handle the number of parallel discussions required.

    I am doing this myself and I never experienced the issue you are describing.

    Login or Signup to reply.
  3. You must take care of sending them in the right order for yourself and therefor queue them if you don’t want them to get out of order.
    The official HTTP Bot API does not ensure a correct message order.

    Login or Signup to reply.
  4. Currently, Telegram Bot API does not provide such parameter.
    That being, you need to wait for the server’s response and check if it went OK before sending the next message.

    If you use NodeJs, there’s a module called tgfancy that handles ordered message sending (along with polling and so many other things) for you.

    Thibauld’s answer adds a really nice suggestion of using the sendChatAction method in order to make the interaction look more conversational. My only point is that a bot is not supposed to pretend it is a human, so there’s no need to make it look like there’s one typing. That being, sendChatAction would serve to the purpose of indicating that your bot has received the user’s message and is processing it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search