如何在Dialogflow中的实现中使用意向参数



我是Dialogflow的新手,对Javascript没有太多经验,如果这个问题听起来很基本,我很抱歉!我一直在尝试构建一个计算器,使用用户指定的半径来查找圆的面积,然而,我很难在计算中使用在实现中保持半径的参数。每当我查看履行响应时,我都会看到以下错误:

{
"error": "conv.parameters is not a function"
}

我真的很感激任何对我的代码的修改建议,这可以帮助我实现这一点。谢谢

我的履行代码:

const functions = require('firebase-functions');
const {dialogflow} = require('actions-on-google');
const WELCOME_INTENT = 'Default Welcome Intent';
const FALLBACK_INTENT = 'Default Fallback Intent';
const CIRCLE_AREA_RADIUS_INTENT = 'CircleAreaRadius';
const UNIT_LENGTH_RADIUS = 'unit-length';
const app = dialogflow();
app.intent(WELCOME_INTENT, (conv) => {
conv.ask("Welcome to Circle Calculator! What can I help you with?");
});
app.intent(FALLBACK_INTENT, (conv) => {
conv.ask("Sorry, I didn't understand. What would you like me to do?");
});
app.intent(CIRCLE_AREA_RADIUS_INTENT, (conv) => {
const radius = conv.parameters(UNIT_LENGTH_RADIUS);
var area = Math.pow(radius, 2) * Math.PI;
conv.ask(`The area of the circle is ${area}.`);
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

Nick的回答很准确,但我想再补充一点。

如果您的参数在Dialogflow中的名称是UNIT_LENGTH_RADIUS,那么您可以使用在代码中访问它

const radius = conv.parameters['UNIT_LENGTH_RADIUS'];

使用括号代替圆括号。

intent参数是函数中的第二个参数:

app.intent(CIRCLE_AREA_RADIUS_INTENT, (conv, {unit-length} ) => {
const radius = params[UNIT_LENGTH_RADIUS]
const area = Math.pow(radius, 2) * Math.PI
conv.ask(`The area of the circle is ${area}.`)
})

你也可以通过对象破坏来简化这一点。如果您在Dialogflow:中将参数重命名为半径

app.intent(CIRCLE_AREA_RADIUS_INTENT, (conv, {radius}) => {
const area = Math.pow(radius, 2) * Math.PI
conv.ask(`The area of the circle is ${area}.`)
})

Dialogflow中的实现内联编辑器使用API v2为我提供了上面的代码问题。

我不得不用一句话来表达我的意图:

app.intent('CIRCLE_AREA_RADIUS_INTENT', (conv, {radius}) => {
const area = Math.pow(radius, 2) * Math.PI
conv.ask(`The area of the circle is ${area}.`)
}) 

最新更新