Search code examples
node.jstelegram-bottelegraftelegraf.js

How to handle long messages in Telegraf


I'm trying to send a super long text as a message after clicking the Telegraf Markup callback button. But I got Error: 400: Bad Request: message is too long

I am new to Telegraf. Is there any way how to handle it? I will be glad to any advice.


Solution

  • I'm not sure if there's a function to split up messages in Telegraf. It exists though and it is called Text Paging.

    Another library used this: https://github.com/GochoMugo/tgfancy#text-paging It's not updated unfortunately though.

    The maximum limit of a message is 4096 characters. So what you could do is something like this:

      const max_size = 4096
      var messageString = "Your Text HERE" 
    
      var amount_sliced = messageString.length / max_size
      var start = 0
      var end = max_size
      var message
      var messagesArray = []
      for (let i = 0; i < amount_sliced; i++) {
        message = messageString.slice(start, end) 
        messages.push(message)
        start = start + max_size
        end = end + max_size
      }
      console.log(messagesArray)