【问题标题】:Error in accessing parameters from fulfillment in dialogflow in-line editor在对话流内嵌编辑器中从实现中访问参数时出错
【发布时间】:2020-07-23 21:32:19
【问题描述】:

A.我做的步骤

  1. 创建了新的机器人
  2. 使用@sys.geo-country.country 创建实体visaCountry
  3. 创建了名为 getVisaCountry 的 Intent 并在参数中调用了实体。
  4. 检查 $visaCountry 是否正确设置为文本响应中的参数。
  5. 将意图与实现联系起来
  6. 将 package.json 中的最新版本更新到

            "@google-cloud/firestore": "^0.16.1",
             "actions-on-google": "^2.12.0",
             "firebase-admin": "^8.10.0",
             "firebase-functions": "^3.6.0",
             "dialogflow": "^0.6.0",
             "googleapis": "^27.0.0",
             "dialogflow-fulfillment": "^0.6.1",
             "request": "^2.85.0",
             "uuid": "^3.0.1"
    
  7. 在 index.js 中添加管理员权限

             var admin = require('firebase-admin');
             var app = admin.initializeApp(); 
    
  8. 映射意图

              intentMap.set('getVisaCountry', ffVisaCountry);
    
  9. 创建函数 ffVisaCountry

  10. 函数中调用参数visaCountry。
  11. 回复是[object object]

B.我的代码

    'use strict';
 
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

var admin = require('firebase-admin');
var app = admin.initializeApp();


process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
 
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 to my agent!`);
  }
 
  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }
  
  function ffVisaCountry(agent) {
   let visaCountry = agent.parameters.visaCountry;
    agent.add(`FF reply: ${visaCountry}`);  
  }

'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('getVisaCountry', ffVisaCountry);
  agent.handleRequest(intentMap);
});

C.我尝试遵循备用参数。

  1. 用 const 和 var 替换 let
  2. 将参数移出函数括号
  3. agent.parameters.visaCountry; 替换为agent.parameters['visaCountry'];
  4. 尝试添加上下文

D.代理的预期和实际行为

预期行为:FF 回复:{用户输入的国家/地区名称}

实际行为:FF回复:[object Object]

【问题讨论】:

  • 欢迎来到 StackOverflow!尽管您的问题非常详细,但如果您使用出现问题的 Intent 的 Dialogflow 配置屏幕截图更新问题也会有所帮助 - 在这种情况下为 getVisaCountry Intent。

标签: node.js dialogflow-es-fulfillment


【解决方案1】:

但是,对于在意图中使用从混合实体派生的参数 (不是像@sys.geo-country 这样的系统实体)

代码应该写成

agent.add(FF reply: ${visaCountry['geo-country']});

谢谢。

【讨论】:

    【解决方案2】:

    感谢您的及时答复。 正如您正确提到的那样,我在意图“getVisaCountry”中使用了混合实体“visaCountry”。 根据您的解决方案,我在所述意图中使用@sys.geo-country 作为参数,问题已解决。 感谢您的支持和技术上完美的回答

    【讨论】:

      【解决方案3】:

      问题在于 visaCountry 是 JavaScript 对象而不是字符串。当您尝试将其转换为行中的字符串时

      agent.add(`FF reply: ${visaCountry}`);
      

      JavaScript 不知道怎么做,所以它把它表示为一个通用字符串“[object Object]”。

      如果您只想查看对象表示法,可以使用JSON.stringify() 方法将对象转换为 JSON 字符串。像这样的

      agent.add(`FF reply: ${JSON.stringify(visaCountry)}`);
      

      可能会给你这个结果

      { "name": "Canada", "alpha-2": "CA", "numeric": 124, "alpha-3": "CAN" }
      

      如果您只需要 ISO-3166-1 国家/地区代码,您可以使用类似的方式访问它

      let countryCode = visaCountry['alpha-2'];
      

      但为什么 Dialogflow 会发送一个对象?

      您没有显示您正在使用的 Intent,但如果您使用的是 @sys.geo-country-code 实体类型,那么 Dialogflow 会将其发送给您的履行 as an object

      如果您只需要名称,则可以使用@sys.geo-country 实体类型。这会将其作为您的代理当前区域设置语言的字符串发送。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 2023-03-31
        • 1970-01-01
        • 2015-12-02
        • 1970-01-01
        相关资源
        最近更新 更多