🛡
Handling “Message too long” Error
✏️ Telegram allows sending a Text message of max 4096 characters by bot
💡
Send Message in multiple messages chunks.
Here is the BJS code to do that:
let chatId = chat.chatid; // Replace with chat id
let message = 'Long Message to send…'; // Replace with the long message
// Function to split a long message into smaller pieces/chunks
function splitMessage(message, maxLength = 4096) {
let chunks = []
// Split the message in multiple chunks each of max 4096 characters
for (let i = 0; i < message.length; i += maxLength) {
chunks.push(message.substring(i, Math.min(message.length, i + maxLength)))
}
return chunks;
}
// Function to send a long message in smaller pieces
function sendMessageInChunks(chatId, text) {
// split the message into chunks
var chunks = splitMessage(text)
// Send the pieces of chunks one after other
for (let i = 0; i < chunks.length; i++) {
Api.sendMessage({ chat_id: chatId, text: chunks[i] })
}
}
// Send the message in pieces
sendMessageInChunks(chatId, message)
🧑💻
Coded by:
@devendra
© Copyright:
@BjsCodes❗️ Only use in state of high requirement, else BB server may get load.