DialogFlow CX计算值

问题描述

我从用户输入中收集数据,最后我想基于该输入来计算一个值。

例如,我先收集人的体重,然后再收集身高,以计算人的BMI General Flow。 如何在最后一步中计算BMI并将结果显示用户

解决方法

除了jess的帖子外,您还可以尝试以下另一种方法,以便根据用户的输入来计算BMI。以下是与提供的第一种方法的区别:

  • Composite custom entities

    这将允许您创建实体,在其中您可以轻松提取用户提供的数字,而不用获取字符串并将此字符串转换为Webhook中的数字。使用这些实体,无需列出其他所有关于身高和体重的选项。

  • Form Parameters

    您可以将参数添加到页面中,代理可以与最终用户多次交互,直到满足参数为止,而不必在添加参数的意图中定义参数。

这是实现这些功能的分步过程。

  1. 创建复合自定义实体,以便从最终用户那里收集页面的表单参数。您可以按照以下方式设计自定义实体:

    a。创建身高和体重单位名称的自定义实体。

    unit-height unit-weight

    b。然后,创建包含每个数字和单元名称的复合自定义实体。请注意,您应该添加一个别名,以确保这些值将分别返回。

    weight height

  2. 创建一个intent,该触发器将用于触发流程的开始。请注意为最终用户可能键入或说的内容添加足够的培训短语。

    intent

  3. 创建一个page,您可以在其中触发Intent.BMI意向后从默认起始页过渡。此页面还将用于收集可用于计算BMI的表单参数。

    page

  4. 通过为过渡为BMI页面的Intent.BMI intent添加flow来创建intent route。流程看起来像这样。

    flow

  5. 现在,进入BMI页面并相应地添加表单参数。确保根据需要设置这些参数。同时添加condition routes,一旦参数完成,您就可以从Webhook返回响应。

    a。 BMI页面可能看起来像这样。 BMI page

    b。对于参数,这是有关如何添加这些参数的示例。 parameters

    c。对于条件路由,我添加了condition以在表单参数满足后返回响应。如果尚未满足,代理将继续提示用户输入有效的信息。我使用了一个webhook来返回响应,其中该webhook提取了每个参数的值并能够计算BMI。 condition

  6. 在您的webhook中,创建一个函数,该函数将提取表单参数并根据这些值计算BMI。这是使用Node.js的另一个示例。

index.js

'use strict';

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

var port = process.env.PORT || 8080;

app.use(
    bodyParser.urlencoded({
      extended: true
    })
);
  
app.use(bodyParser.json());

app.post('/BMI',(req,res) => processWebhook4(req,res));

var processWebhook4 = function(request,response ){

    const params = request.body.sessionInfo.parameters;
    
    var heightnumber = params["height.number"];
    var weightnumber = params["weight.number"];
    var heightunit = params["height.unit-height"]
    var weightunit = params["weight.unit-weight"]
    var computedBMI;

    if (heightunit == "cm" && weightunit == "kg") { //using metric units
        computedBMI = ((weightnumber/heightnumber/heightnumber )) * 10000;
    } else if (heightunit == "in" && weightunit == "lb") { //using standard metrics
        computedBMI = ((weightnumber/heightnumber/heightnumber )) * 703;
    }

    const replyBMI = {
        'fulfillmentResponse': {
            'messages': [
                {
                    'text': {
                        'text': [
                            'This is a response from webhook! BMI is ' + computedBMI
                        ]
                    }
                }
            ]
        }
    }
    response.send(replyBMI);
}

app.listen(port,function() {
    console.log('Our app is running on http://localhost:' + port);
});

package.json

{
   "name": "cx-test-functions","version": "0.0.1","author": "Google Inc.","main": "index.js","engines": {
       "node": "8.9.4"
   },"scripts": {
       "start": "node index.js"
   },"dependencies": {
       "body-parser": "^1.18.2","express": "^4.16.2"
   }
}
  1. 这是结果。 result
,

为了计算从机器人收集的输入值,您将需要使用Webhook设置代码以计算BMI并在Dialogflow CX控制台中连接Webhook URL。您可以尝试以下简单流程:

  1. 首先,创建复合自定义实体,该实体可用于匹配意图中的训练短语中的值,例如,体重高度https://cloud.google.com/dialogflow/cx/docs/concept/entity#custom

enter image description here

enter image description here

  1. 然后使用与值匹配的训练短语创建意图 与您创建的实体。

enter image description here

  1. 有两种方法来设置参数值:Intent parametersForm parameters。在我的示例中,我使用了Intent参数来获取当您从“测试代理”部分查询对话流时存储的参数值:

enter image description here

  1. 然后在Webhook中准备代码以处理值以计算BMI:https://cloud.google.com/dialogflow/cx/docs/concept/webhook。这是使用NodeJS的示例代码:

index.js

const express = require('express') // will use this later to send requests 
const http = require('http') // import env variables 
require('dotenv').config()
const app = express();
const port = process.env.PORT || 3000

/// Google Sheet 
const fs = require('fs');
const readline = require('readline');

app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/',res) => { res.status(200).send('Server is working.') })
app.listen(port,() => { console.log(`? Server is running at http://localhost:${port}`) })

app.post('/bmi',(request,response) => {
    let params = request.body.sessionInfo.parameters;
    let height = getNumbers(params.height); // 170 cm from the example
    let weight = getNumbers(params.weight); // 60 kg from the example

    let bmi = (weight/(height/100*height/100));

    let fulfillmentResponse = {
        "fulfillmentResponse": {
            "messages": [{
                "text": {
                    "text": [
                        bmi 
                    ]
                }
            }]
        }
    };
    response.json(fulfillmentResponse);
});

// Extract number from string
function getNumbers(string) {
  string = string.split(" ");
  var int = ""; 
  for(var i=0;i<string.length;i++){
    if(isNaN(string[i])==false){
    int+=string[i];
    }
  }
 return parseInt(int);
}

package.json

{
  "name": "server","version": "1.0.0","description": "","scripts": {
    "start": "node index.js","test": "echo \"Error: no test specified\" && exit 1"
  },"keywords": [],"author": "","license": "ISC","dependencies": {
    "dotenv": "^8.2.0","express": "^4.17.1"
  }
}
  1. 部署Webhook
  2. 在Dialogflow CX控制台中添加webhook URL

enter image description here

  1. 在Dialogflow CX页面中使用webhook,您需要在其中设置BMI输出的响应:

enter image description here

结果如下: enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...