简体   繁体   中英

How do you read/query the response body in API V2 of dialogflow-fulfillment?

In the v1, the request and response were specifically defined and read via -

console.log(request.body);
var input = request.body.queryResult;

In the v2, the request and response, both are wrapped inside the 'app'. My declarations of app are as below -

const {dialogflow} = require('actions-on-google');
const app = dialogflow({clientId: 'projectId'});

I have tried using the following but understood that it's not exactly the right way -

console.log(conv.request.body); //Getting undefined in console
//OR
console.log(app.request.body); //Getting undefined in console
var input = conv.request.body.queryResult; 

Do I need to specifically mention request and response anywhere similar to the WebhookClient({request, response}) in V1?

Thanks in advance

After million trial and error, I finally found it and it's terribly simple

console.log(conv.body);
var input = conv.body.queryResult.queryText;

I know this seems to already be resolved. But I noticed that you did a console.log() on conv.body. Assuming this conv variable is the JSON response object you received from Dialogflow, I'd recommend doing console.log(JSON.stringify(conv)), which, not surprisingly prints the full JSON object to string in the console. This has saved me much time while trying to figure out the many JSON formats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM