I am working on a simple application that will go through DialogFlow (Google) and send me the intents that were picked up based on the users comment.
I have the following code.
let writeToExcel = [];
var promises = xlData.map(data => {
detectIntentText(data.QUESTION);
});
Promise.all(promises).then(function(results) {
console.log(results);
console.log(writeToExcel);
})
However, the array that prints is all undefined, and it prints before the function, detectIntentText
, finished everything in the map. There could be upwards of 1000 entries.
What is the best way to get this to work?
Here is the detectIntentText function
async function detectIntentText(userText) {
const sessionId = Math.random().toString(36).substring(7);
const sessionPath = client.projectLocationAgentSessionPath(
projectId,
location,
agentId,
sessionId
);
console.info(sessionPath);
const request = {
session: sessionPath,
queryInput: {
text: {
text: userText,
},
languageCode,
},
};
const [response] = await client.detectIntent(request);
// console.log(`User Query: ${userText}`);
var intent = "";
for (const message of response.queryResult.responseMessages) {
if (message.text) {
// console.log(`Agent Response: ${message.text.text}`);
}
}
if (response.queryResult.match.intent) {
// console.log(
// `Matched Intent: ${response.queryResult.match.intent.displayName}`
//);
intent = response.queryResult.match.intent.displayName;
}
// console.log(
// `Current Page: ${response.queryResult.currentPage.displayName}`
// );
let jsonToWrite = {
CX_QUESTION: userText,
INTENT: intent
}
// console.log(jsonToWrite);
writeToExcel.push(jsonToWrite);
}