首页 > 解决方案 > 带有所需参数的 Dialogflow 插槽填充问题

问题描述

我正在使用 Dialogflow 为零食机创建一个机器人,但我遇到了问题。我有两种类型的实体:

我为这两个实体做了两个不同的意图,order.simple.element并且 order.complex.element.

order.complex.element我设置为所需参数元素的数量和元素类型本身,我想也设置复杂元素类型,但只有当复杂元素匹配时,这意味着问用户你想要水? 还是火花?只有当实体是时。

操作和参数 Dialogflow 控制台图像在这里

我也尝试了 webhook 调用,但它不起作用。这是我使用nodejs的代码:

const functions = require('firebase-functions');
const {WebhookClient, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug';

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    const agent = new WebhookClient({ request, response });
    console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
    console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
  
    function welcome (agent) {
      agent.add(`Welcome! What do you want?`);
    }
    function fallback (agent) {
      agent.add(`I didn't understand`);
      agent.add(`I'm sorry, can you try again?`);
    }

    function orderComplexElement(agent) {
        var complex_el = agent.parameters['complex-element'].toLowerCase();
        if (complex_el === 'water') {
            agent.add('How do you want it? Still or sparkling?');
            agent.add(new Suggestion('still'));
            agent.add(new Suggestion('sparkling'));
            
        } else if (complex_element === 'coffee') {
            agent.add('How do you want it? You can choose from americano, lungo and macchiato.');
        }
    }
    
    let intentMap = new Map();
    intentMap.set('Default Welcome Intent', welcome);
    intentMap.set('Default Fallback Intent', fallback);
    intentMap.set('order.complex.element.try', orderComplexElement);
    agent.handleRequest(intentMap);
});

如何根据类型根据需要制作复杂元素complex-element类型?

标签: javascriptnode.jsdialogflow-es

解决方案


您需要捕获用户对 water-type 和 coffee-type 的响应,然后将这些参数保存在您的上下文中。例如:

    var water-type = request.body.queryResult.queryText; // this captures the user input
    agent.context.set({ // saves the parameter to a context.
      name:'sessionvars', //you can use this context to capture all variables
      lifespan: 50, //context will be available for 50 conversational turns
      parameters: {
        'water-type': water-type
      }
    });

推荐阅读