Dialogflow fulfillment 在代码中访问实体

Dialogflow fulfilment accessing entities in code

我正在使用 Dialogflow 开发一个聊天机器人,想验证某人的年龄。简要介绍一下背景:我正在创建一个聊天机器人来识别护理需求,例如住宅或痴呆症护理。在最初的查询中,我希望能够通过在 Dialogflow 的实现代码中执行快速 IF 语句来确保用户年满 65 岁!

这是我目前的意图: Current Intents

这是 getUserInfo 意图: getUserInfo intent

这是实现代码:

'use strict';

// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');

// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');

// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});

app.intent('careEqnuiry - yourself - getUserInfo', (conv, {age}) => {
    const userAge = age;

    if (userAge < 65) {
        conv.add("You're not old enough to recieve care!");
    }

});

// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

这对我来说是全新的。

意图处理程序回调的第二个参数是一个包含 Dialogflow 中所有参数(实体)的对象。

在您当前的代码中,您正在为年龄参数解构此对象(即:{age})。

我注意到你有两个不同的参数,年龄和名字。

您可以通过执行以下操作获取这些值:

'use strict';

// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');

// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');

// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});

app.intent('careEqnuiry - yourself - getUserInfo', (conv, params) => {
    const name = params['given-name'];
    const age = params['age'];
    if (age.amount < 65) {
        conv.ask("You're not old enough to receive care!");
    }

});

// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

此外,return 对话响应的正确方法是对对话对象使用 ask()close() 方法。 ask() 发送响应并允许对话继续,close 发送响应并结束对话。