简体   繁体   English

如何在 dialogflow-fulfillment 的 API V2 中读取/查询响应正文?

[英]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 -在 v1 中,请求和响应是专门定义的,并通过以下方式读取 -

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

In the v2, the request and response, both are wrapped inside the 'app'.在 v2 中,请求和响应都包含在“应用程序”中。 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?我是否需要在类似于 V1 中的 WebhookClient({request, response}) 的任何地方特别提及请求和响应?

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.但我注意到你在 conv.body 上做了一个 console.log()。 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.假设这个 conv 变量是您从 Dialogflow 收到的 JSON 响应对象,我建议您执行 console.log(JSON.stringify(conv)),这在控制台中将完整的 JSON 对象打印为字符串也就不足为奇了。 This has saved me much time while trying to figure out the many JSON formats.这在我试图找出许多 JSON 格式时为我节省了很多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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