Javascript 如何在Dialogflow中的实现中使用意图参数?

Javascript 如何在Dialogflow中的实现中使用意图参数?,javascript,actions-on-google,dialogflow-es,Javascript,Actions On Google,Dialogflow Es,我是Dialogflow新手,对Javascript没有太多的经验,如果这个问题听起来很基本,我很抱歉!我一直在尝试构建一个计算器,以使用用户指定的半径来查找圆的面积,但是,我很难在计算中使用包含半径的参数。每当我查看履行响应时,都会看到以下错误: { "error": "conv.parameters is not a function" } 我真的很感激对我的代码所做的任何建议性的修改,这些修改可以帮助我实现这一目标。谢谢 我的履行代码: const functions = requi

我是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);

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中将参数重命名为radius:

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}.`)
})

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中将参数重命名为radius:

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中的参数名称为UNIT_LENGTH_RADIUS,则可以在代码中使用以下选项访问它:

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

用括号代替括号。

尼克的答案很正确,但我想补充一点

如果Dialogflow中的参数名称为UNIT_LENGTH_RADIUS,则可以在代码中使用以下选项访问它:

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

通过使用括号而不是括号。

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}.`)
}) 

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}.`)
}) 

谢谢你的额外信息!谢谢你的额外信息!