windkh / node-red-contrib-telegrambot

Telegram bot nodes for node-red.
Other
264 stars 117 forks source link

Send message size over 4096 #239

Closed sanyafifa closed 2 years ago

sanyafifa commented 2 years ago

Hey! when you try to send a message to the bot more than 4096 characters - an error appears. Please help me get around this problem. How to break a large message into parts and send it to the bot one by one?

windkh commented 2 years ago

Hm normally the sender node would create chunks of 4096 and send them one after the other as separate messages. What error appears?

windkh commented 2 years ago

// the maximum message size is 4096 so we must split the message into smaller chunks. let chunkSize = 4000; let message = msg.payload.content;

                            let done = false;
                            do {
                                let messageToSend;
                                if (message.length > chunkSize) {
                                    messageToSend = message.substr(0, chunkSize);
                                    message = message.substr(chunkSize);
                                } else {
                                    messageToSend = message;
                                    done = true;
                                }

                                node.telegramBot
                                    .sendMessage(chatId, messageToSend, msg.payload.options)
                                    .then(function (result) {
                                        node.processResult(result, msg, nodeSend, nodeDone);
                                    })
                                    .catch(function (err) {
                                        // markdown error? try plain mode

                                        // TODO: MarkdownV2 issues error "Error: ETELEGRAM: 400 Bad Request: can't parse entities:"
                                        // adapt the following if so that MarkdownV2 also works.
                                        if (
                                            String(err).includes(
                                                // eslint-disable-next-line quotes
                                                "can't parse entities in message text:"
                                            ) &&
                                            msg.payload.options &&
                                            msg.payload.options.parse_mode === 'Markdown'
                                        ) {
                                            delete msg.payload.options.parse_mode;
                                            node.telegramBot
                                                .sendMessage(chatId, messageToSend, msg.payload.options)
                                                .catch(function (ex) {
                                                    node.processError(ex, msg, nodeSend, nodeDone);
                                                })
                                                .then(function (result) {
                                                    node.processResult(result, msg, nodeSend, nodeDone);
                                                });
                                            return;
                                        } else {
                                            node.processError(err, msg, nodeSend, nodeDone);
                                        }
                                    });
                            } while (!done);
sanyafifa commented 2 years ago

Hm normally the sender node would create chunks of 4096 and send them one after the other as separate messages. What error appears?

Unhandled rejection Error: ETELEGRAM: 400 Bad Request: can't parse entities: Unexpected end tag at byte offset 81
    at C:\Users\adm-user\.node-red\node_modules\node-telegram-bot-api\src\telegram.js:291:15
    at tryCatcher (C:\Users\adm-user\.node-red\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\Users\user\.node-red\node_modules\bluebird\js\release\promise.js:547:31)
    at Promise._settlePromise (C:\Users\adm-user\.node-red\node_modules\bluebird\js\release\promise.js:604:18)
    at Promise._settlePromise0 (C:\Users\adm-user\.node-red\node_modules\bluebird\js\release\promise.js:649:10)
    at Promise._settlePromises (C:\Users\adm-user\.node-red\node_modules\bluebird\js\release\promise.js:729:18)
    at _drainQueueStep (C:\Users\adm-user\.node-red\node_modules\bluebird\js\release\async.js:93:12)
    at _drainQueue (C:\Users\adm-user\.node-red\node_modules\bluebird\js\release\async.js:86:9)
    at Async._drainQueues (C:\Users\adm-user\.node-red\node_modules\bluebird\js\release\async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\adm-user\.node-red\node_modules\bluebird\js\release\async.js:15:14)
    at processImmediate (node:internal/timers:464:21)

node-red-contrib-telegrambot 11.2.1

node-red v2.1.4

windkh commented 2 years ago

Thanks, it seem you are using markdown in your message. Unfortunately one entity is obviously split at 4000. this leads to the exception.

Either you turn off markdown or you try to split your message at the correct location before sending. The node could only turn off markdown and resend the message

sanyafifa commented 2 years ago

@windkh Thanks for the answer. By removing the markdown there is no error! But the messages are out of order. Tell me how to split the message into parts?

windkh commented 2 years ago

@sanyafifa well, what is so large that it exceeds 4096 characters? Do you send newspaper articles?

It depends on your message to decide what is the smartest way to split messages. But simply google for how to split long strings using javascript. Then create a function node before the sender and take care of splitting your message to smaller parts.