in Education by
I found this example on the official site of DialogFlow using Node.js and it is working fine, but I dont know how do I integrate this into my web application. Is it possible that I can integrate this into my other javascript jquery code? and here I need to run node index.js but do I still need to do this if I integrate with my code? const projectId = 'xxx'; //https://dialogflow.com/docs/agents#settings const sessionId = 'xxxxx'; const query = 'Hello'; const languageCode = 'en-US'; // Instantiate a DialogFlow client. const dialogflow = require('dialogflow'); const sessionClient = new dialogflow.SessionsClient(); // Define session path const sessionPath = sessionClient.sessionPath(projectId, sessionId); console.log(sessionPath); // The text query request. const request = { session: sessionPath, queryInput: { text: { text: query, languageCode: languageCode, }, }, }; // Send request and log 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); }); Are there any alternative that we can use DialogFlow v2 using normal javascript jquery, ajax without me having to do node index.js everytime I want to use dialogflow. DialogFlow v1 was quite simple to use. I had it 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 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
As others have said here before, the access token has a duration of one hour, after that time it becomes useless. So it's necessary to make a call (http call in my case) to the API in order to request an access token, one time each hour, and use it as explained by Satheesh thereafter. Instructions on how to generate the signature to make the call and use it later are given in https://developers.google.com/identity/protocols/OAuth2ServiceAccount. Once you obtain the json file from the service account with the private key and the email you have to use (not your email, but the one generated by the service account), you can use the jsrsasign library (in pure javascript), that you can find in https://github.com/kjur/jsrsasign, to generate the JSON Web Signature (JWS) and thus the JSON Web Token (JWT), that will be needed to make the http call to get the access token. Then you use it as described above by Satheesh to make the call to Dialogflow V2 via jQuery. The code I have used to achieve this is the next one: To generate the JWT (using the related library): function _genJWS() { var header = '{"alg":"RS256","typ":"JWT"}'; var claimSet = jwtClaimSet(); var privateKey = jwtPrivateKey(); var sHead = newline_toDos(header); var head = KJUR.jws.JWS.readSafeJSONString(sHead); var sPayload = newline_toDos(claimSet); var sPemPrvKey = privateKey; var jws = new KJUR.jws.JWS(); var sResult = null; try { prv = KEYUTIL.getKey(sPemPrvKey); sResult = KJUR.jws.JWS.sign(head.alg, sHead, sPayload, prv); } catch (ex) { alert("Error: " + ex); } return sResult; } To request the access token: function _requestAccessToken() { var access_token = accessToken; var assertion = _genJWS(); console.log('Assertion: ' + assertion); jQuery.ajax({ type: "POST", url: "https://www.googleapis.com/oauth2/v4/token", contentType: "application/x-www-form-urlencoded", dataType: "json", data: "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=" + assertion, success: function(response) { console.log("success"); console.log(response); access_token = response.access_token; console.log(access_token); }, error: function() { console.log("Error"); } }); return access_token; } Then use that access token to make the HTTP call to Dialogflow. Hope it helps.

Related questions

0 votes
    How to pass json format data on ajax call?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    I have an html select on my page $query = mysql_query("select * from results"); echo " "; while ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I have an html select on my page $query = mysql_query("select * from results"); echo " "; while ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I have an html select on my page $query = mysql_query("select * from results"); echo " "; while ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I have following code and I want to get data from service. I have set everything from what I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I have following code and I want to get data from service. I have set everything from what I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    My question is a variant of the question here However, there are two differences: I don't know how ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    My question is a variant of the question here However, there are two differences: I don't know how ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I'm having this little problem with internet explorer and ajax. So first I used just php, and everything ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 16, 2022 in Education by JackTerrance
0 votes
    Asynchronous Javascript and XML (Ajax) is technique for creating better, faster, and more interactive web applications. In addition to ... Perl, and C++ D. Java, ASP, and C#...
asked Mar 10, 2023 in Technology by JackTerrance
0 votes
    $(document).ready(function(){ alert("funciton"); $(function(){ $.fn.gotof(){ alert("I am ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    $(document).ready(function(){ alert("funciton"); $(function(){ $.fn.gotof(){ alert("I am ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    $(document).ready(function(){ alert("funciton"); $(function(){ $.fn.gotof(){ alert("I am ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    How can you call a controller method from JavaScript?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    I need to add a confirmation entity so I get a 'Yes' or a 'Cancel' in the parameters of a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
...