【问题标题】:DialogFlow CX calculate valuesDialogFlow CX 计算值
【发布时间】:2020-10-06 14:45:59
【问题描述】:

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

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

【问题讨论】:

    标签: calculation dialogflow-cx


    【解决方案1】:

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

    • Composite custom entities

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

    • Form Parameters

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

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

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

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

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

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

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

    4. 通过为 Intent.BMI 意图添加 intent route 来创建 flow,其中转换是 BMI 页面。流程如下所示。

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

      一个。 BMI 页面可能如下所示。

      b.对于参数,这里有一个如何添加这些参数的示例。

      c。对于条件路由,我添加了一个condition,以便在满足表单参数后返回响应。如果尚未完成,代理将继续提示用户输入有效输入。我使用了一个 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);
    });
    
    

    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. 这是结果。

    【讨论】:

      【解决方案2】:

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

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

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

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

      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('/', (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. 部署您的网络钩子
      2. 在 Dialogflow CX 控制台中添加 webhook URL

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

      结果如下:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-08
        • 2022-07-20
        • 2021-01-16
        • 2023-02-03
        相关资源
        最近更新 更多