Async function for sending messages (CHATBOT)

hi there!
I'm doing a chatbot for instagram, and I came across a situation. I made a script to put in an array each messages that the bot returns, but it returns with some messages with order exchanged/shuffled. So I applied a delay node. But I wanted to put an async function to improve performance.
Would there be a node that does this function?

below the flow diagram

[{"id":"38728f55c6274ca7","type":"function","z":"a356a8194128f9f2","name":"main function// async..await","func":"//tipo da mensagem\nvar typemsg = msg.payload.type;\n\n//id da mensagem\nconst chatId = msg.payload.chatId;\n\n//opções criadas no nó anterior\nvar options = msg.payload.options;\n\n//id da sessão\nvar sessionId = msg.payload.session_id;\n\n//conteúdo da mensagem\nvar msgMsg = msg.payload.content;\n\n//tamanho do array da mensagem\nvar tamanho = msg.payload.content.length;\n\n//array que receberá as novas mensagens\nvar newMsg = await [];\nconsole.log(newMsg);\n//refatorar loop para aplicar função assincrona\n\nlet controleMsg = async () => {\nfor (const lines of msgMsg){\n    if(lines.response_type == \"text\"){\n         newMsg.push({\n        payload:{\n            type:typemsg,\n            chatId:chatId,\n            session_id: sessionId,\n            content:lines.text,\n            response_type: lines.response_type,\n            \n        }\n    })}else{\n         newMsg.push({\n        payload:{\n            type:typemsg,\n            chatId:chatId,\n            session_id: sessionId,\n            content:lines.title,\n            response_type: lines.response_type,\n            options: options\n            \n        }\n    })}\n}\nreturn [newMsg];\n}\n\n\n\n\n\n\n//retorna as mensagens para o novo array\n\n\n\n//futuramente será incerido função assincrona.\n//ver função for...of JavaScript","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":220,"y":740,"wires":[[]]}]

but to advance, the function that is checking is this:
I tried to apply an async function and return the contents to the array, but it doesn't debug.

//type of msg
var typemsg = msg.payload.type;

//id of msg
const chatId = msg.payload.chatId;

//menu options
var options = msg.payload.options;

//id da sessão
var sessionId = msg.payload.session_id;

//content of mensagem
var msgMsg = msg.payload.content;

//array that will receive as new messages
var newMsg = await [];

let controleMsg = async () => {
for (const lines of msgMsg){
    if(lines.response_type == "text"){
         newMsg.push({
        payload:{
            type:typemsg,
            chatId:chatId,
            session_id: sessionId,
            content:lines.text,
            response_type: lines.response_type,
            
        }
    })}else{
         newMsg.push({
        payload:{
            type:typemsg,
            chatId:chatId,
            session_id: sessionId,
            content:lines.title,
            response_type: lines.response_type,
            options: options
            
        }
    })}
}

}

return [newMsg];

You cannot use return due to async nature.

Use node.send instead..

example...
image


var data = [
    "1", "2", "3"
];

let asyncSender = async () => {
    const messages = [];
    data.forEach(e => {
        messages.push({ payload:  e } )
    })
    node.send([messages]);
}
node.warn("before asyncSender")
asyncSender()
node.warn("after asyncSender")


I think you have the logic confused I'm afraid.

The controleMsg isn't going to be created until after you have returned. You need instead to have a node.send() inside the async.

Also, I don't think that var newMsg = await []; is doing anything with the await since [] is not a promise.

had put it to test if it had any impact, forgetting to take

it's a great approach, I really didn't know there was this method to return the function, however, in the console it prints the logs all in the sequence, however, when the bot sends the message, the messages are still disordered.

I applied await in node.send, but the same effect happens

Can you explain - with perhaps a screenshot showing what you get, what you expected?

OK

when you send a "hi" that would be a greeting, it answers these two messages, one for reply and another with options, in the console (left side) it prints the message correctly. But when sending to chat (right side), messages are sent randomly

Try adding a delay node(set to rate limit mode of 2 msg per 1 sec*) immediately before the telegram node.

Does that solve the problem?


*(2 msg per 1 sec will essentially space out message by 500ms)

Also, be sure to set the polling to < 500ms in the bot config

very likely it will work.
before trying to do it by async, I set some delays that checked what messages were going through and delayed sending. but I found a plastered method.
I will try to delay the flow of messages through node, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.