bottenderjs/messaging-apis

Telegram API - 400 Bad Request: message is too long - Split message to send

leopucci opened this issue · 3 comments

Telegram Api has a limit of 4096 to messages and is not sending big messages

Here there is a solution to split the message that i am using today
https://stackoverflow.com/a/70219218/3156756

function messagePadder(str, length) {
    var padding = (new Array(Math.max(length - str.length + 1, 0))).join(" TESTE ");
    return str + padding;
};
function messageSplitter(message) {
    var chunks = message.match(/[^]{1,4096}/g);
    // This is kinda' crazy too. Basically, Telegram delivers smaller length
    // messages first, thus it helps to keep them the same length
    for (var i = 0; i < chunks.length; i++) {
        if (chunks[i].length < 4096) {
            var pad_needed = 4096 - chunks[i].length;
            var padding = messagePadder(chunks[i], pad_needed);
            chunks[i] += padding;
        }
    }
    return chunks;
};

unsing: 
  var parts = messageSplitter(message);
            parts.forEach((messagePart) => 
                      send(messagePart)
}