0

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

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

4

2 回答 2

3

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

  • 复合自定义实体

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

  • 表单参数

    您可以在页面中添加参数,而不是在添加参数的意图中定义参数,在该页面中,代理可以与最终用户进行多次交互,直到参数得到满足。

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

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

    一个。为身高和体重单位名称创建自定义实体。

    单位高度 单位重量

    湾。然后,创建复合自定义实体,其中包含每个实体的编号和单位名称。请注意,您应该添加一个别名以确保将单独返回这些值。

    重量 高度

  2. 创建将用于触发流程开始的意图。请注意为最终用户可能键入或说出的内容添加足够的训练短语。

    意图

  3. 创建一个页面,您可以在触发 Intent.BMI 意图时从默认起始页面转换。此页面还将用于收集可用于计算 BMI 的表单参数。

    页

  4. 通过为 Intent.BMI 意图添加一个意图路由来创建流,其中转换是 BMI 页面。流程看起来像这样。

    流动

  5. 现在,进入 BMI 页面并相应地添加表单参数。确保根据需要设置这些参数。还可以添加条件路由,一旦满足参数,您就可以从 webhook 返回响应。

    一个。BMI 页面可能如下所示。 体重指数页面

    湾。对于参数,这里是一个关于如何添加这些参数的示例。 参数

    C。对于条件路由,我添加了一个条件以在满足表单参数后返回响应。如果尚未完成,代理将继续提示用户输入有效输入。我使用了一个 webhook 来返回响应,其中这个 webhook 提取了每个参数的值并能够计算 BMI。 (健康)状况

  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);
});

包.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. 这是结果。 结果
于 2020-10-14T08:53:07.113 回答
1

In order to calculate the values of the input you collected from the bot, you will need to set-up a code using a webhook to calculate the BMI and connect the Webhook URL in the Dialogflow CX Console. Here’s a simple flow you can try:

  1. First, create composite custom entities which can be used to match values in the training phrases in an intent, for example, weight and height. https://cloud.google.com/dialogflow/cx/docs/concept/entity#custom.

enter image description here

enter image description here

  1. Then create an intent with training phrases that match the values with the entities you created.

enter image description here

  1. There are two ways to set the parameter values: Intent parameters and Form parameters. In my example, I used Intent parameters to get the parameter values that are stored when you query the flow of the conversation from the “Test Agent” section:

enter image description here

  1. Then prepare your code in the webhook to process the values to calculate the BMI: https://cloud.google.com/dialogflow/cx/docs/concept/webhook. Here’s a sample code using 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('/', (req, 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": "",
  "main": "index.js",
  "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. Deploy your webhook
  2. Add the webhook URL in the Dialogflow CX Console

enter image description here

  1. Use the webhook in Dialogflow CX Page wherein you will need to set the response for BMI output:

enter image description here

Here's the result: enter image description here

于 2020-10-13T23:36:58.380 回答