Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
leopucci opened this issue Feb 27, 2023 · 3 comments

Comments

@leopucci
Copy link

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

@leopucci
Copy link
Author

@leopucci
Copy link
Author

leopucci commented Mar 7, 2023

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

@leopucci
Copy link
Author

leopucci commented Mar 7, 2023

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant