I came across this example on the DialogFlow official site, demonstrating how to use Node.js. It seems to be working fine, but I'm unsure about integrating it into my web application.
Can I merge this with my existing JavaScript jQuery code? Also, if I integrate it, do I still need to run node index.js separately?
const projectId = 'xxx'; //https://dialogflow.com/docs/agents#settings
const sessionId = 'xxxxx';
const query = 'Hello';
const languageCode = 'en-US';
// Setting up DialogFlow client.
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient();
// Define session path.
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
console.log(sessionPath);
// Text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
};
// Sending request and displaying result.
sessionClient
.detectIntent(request)
.then(responses => {
console.log('Detected intent');
const result = responses[0].queryResult;
console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
})
.catch(err => {
console.error('ERROR:', err);
});
Is there an alternative way to use DialogFlow v2 with just normal JavaScript, jQuery, and AJAX without the need to run node index.js every time?
Using DialogFlow v1 was more straightforward for me. The setup looked something like this:
fetch(url, {
body: JSON.stringify(data),
// cache: 'no-cache',
// credentials: 'same-origin',
headers: {
'content-type': 'application/json',
"Authorization": "Bearer " + configs.accessToken,
},
method: 'POST',
mode: 'cors',
redirect: 'follow',
referrer: 'no-referrer',
})
.then(response => response.json()) // parses response to JSON