Telegram API - 400 Bad Request: message is too long - Split message to send
leopucci opened this issue · 3 comments
leopucci commented
Telegram Api has a limit of 4096 to messages and is not sending big messages
leopucci commented
leopucci commented
Here there is a solution to split the message that i am using today
https://stackoverflow.com/a/70219218/3156756
leopucci commented
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)
}