【问题标题】:agent.add not working while console.log isagent.add 不工作,而 console.log 是
【发布时间】:2021-09-29 18:40:05
【问题描述】:

我正在使用 Dialogflow 内联编辑器来调用 API。当我使用 console.log 从 API 记录一些数据时,它可以工作。但是,当我将 agent.add 与相同的变量一起使用时,它会出现错误。

我阅读了有关此问题的其他一些 stackoverflows,其中人们使用了 Promise 和 resolve 调用。我试图在我的代码中实现这一点。但是,我不确定我是否以正确的方式使用它。

这是我的代码:

'use strict';
const axios = require('axios');
 
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
 
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 randomHandler(agent){
    const regio = agent.parameters.regio;
    if (regio != "Alkmaar" && regio != "Schiphol"){
      agent.add(`Deze regio bestaat helaas niet binnen NH nieuws, kies een andere regio of kies voor het nieuws uit heel Noord-Holland`);
    } else {
        return axios.get(`https://api.datamuse.com/words?rel_rhy=book`)
    .then((result) => {
        
      result.data.map (res => {
        const dataArray = ""; // An array with the results. However we got it
        const words = dataArray.map( entry => entry.word );  // Get just the "word" field from each entry in the dataArray
        const wordsString = words.join(', ');   // Put commas in between each word
        agent.add( `The results are: ${wordsString}`);
      });
      });
    }
  }
  

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('random', randomHandler);
  agent.handleRequest(intentMap);
});
    

这是我的 package.json:

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "10"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "^2.2.0",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.5.0",
    "axios" : "0.20.0"
  }
}



 

【问题讨论】:

    标签: node.js promise dialogflow-es webhooks actions-on-google


    【解决方案1】:

    正如您所怀疑的 - 问题出在 Promises 上。

    rhymingWordHandler() 中有一个异步操作(使用 axios 的网络调用),但您没有返回 Promise。

    幸运的是,axios.get()确实返回了一个 Promise。您可以知道,因为您在返回的内容上调用 .then(),而 .then() 返回一个 Promise 等等。我喜欢这个作为“承诺链”。将其更改为返回 Promise 相对简单:

    return axios.get(`https://api.datamuse.com/words?rel_rhy=${word}`)
      .then( /* and so forth */ )
    

    但是,您的代码还不够。您不需要在 .then() 函数中创建 Promise,这样做只会使事情变得混乱。

    请记住,意图名称区分大小写 - 您需要在 UI 中使用的名称以及用于 intentMap.set() 的名称中使用完全相同的大小写。您目前在一个地方有“Rhymingword”,在另一个地方有“RhymingWord”(一个 W 大写,另一个不是)。

    您还可能遇到一个小问题,您可能无法多次致电agent.add(rhyme)。一些代理只能处理一两个文本响应。相反,您需要从结果数组构建响应字符串。

    如何构建这样的字符串取决于您的确切内容。最简单的(虽然语法上不正确)是在它们之间加上逗号。我经常分步进行,所以它可能看起来像这样:

      const dataArray = // An array with the results. However we got it
      const words = dataArray.map( entry => entry.word );  // Get just the "word" field from each entry in the dataArray
      const wordsString = words.join(', ');   // Put commas in between each word
      agent.add( `The results are: ${wordsString}`. );
    

    这有很多问题,一个好的解决方案可以处理以下情况:

    • 如果没有结果怎么办?
    • 如果只有一个单词,是否应该在列表前使用不同的短语?
    • 如果有两个结果怎么办?
    • 您应该对三个或更多结果应用正确的语法/标点符号规则。

    但这应该是确保您只拨打一次agent.add() 的开始。

    【讨论】:

    • 嗨!感谢评论我已经更改了代码(我已经在问题中更新了它)。但是,它仍然没有添加到代理中。看起来它在函数中完成时无法向代理添加一些东西?这是我得到的错误:No handler for requested intent
    • 这通常表明一个非常不同的问题,您的处理程序函数甚至没有被调用。您能否更新您的问题以包括您认为应该处理的 Intent 的屏幕截图,可能还包括对话示例以及尝试时会发生什么?
    • 好的!我已经添加了意图的图像第一个)和执行(第二个)。
    • 答案已更新。请记住,意图名称区分大小写。
    • 锋利!我已经改变它看到更新的代码。但是,我仍然有同样的问题。
    【解决方案2】:

    已解决,问题是我调用了 agent.add 超过 2 次(这是最大值)。这段代码对我有用:

    const { conversation } = require('@assistant/conversation');
    const functions = require('firebase-functions');
    const axios = require('axios').default;
    const app = conversation();
    
    var titels = [];
    
    axios.get(`YOUR-API`)
      .then((result)=> {
        titels.push(result.data.categories[0].news[0].title);
        /* result.data.map(wordObj => {
          titels.push(wordObj.categories.news.title);
        });*/ 
      }); 
      
    app.handle('rhymeHandler', conv => {
      console.log(titels[0]);
      conv.add(titels[0]); 
    });
    
    exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app);    
    
    /* for (i = 1; i < 4; i++) {
      conv.add(words[i]);
      } */
      //console.log(words);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 2013-11-08
      • 2011-10-30
      • 2019-11-09
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多