TypeError: Cannot read property 'forEach' of undefined at V2Agent.addActionsOnGoogle_

TypeError: Cannot read property 'forEach' of undefined at V2Agent.addActionsOnGoogle_

我正在使用 dialogflow-fulfillment、actions-on-google 创建聊天机器人并将其托管在 aws lambda 中。我正在尝试请求用户权限,但是当我尝试添加一些关于 google 的操作时遇到问题,例如权限。

我已经尝试过此处对话流示例的作用:

https://github.com/dialogflow/fulfillment-actions-library-nodejs/blob/master/functions/index.js

但是,由于我使用的是 AWS lambda,所以我没有遵循确切的代码,可能我遗漏了什么。

 router.post('/', (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 the webhook`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  } 

function locationIntent(agent) {
    let conv = agent.conv();
    conv.ask(new Permission({
      context: 'To give results in your area',
      permissions: 'DEVICE_PRECISE_LOCATION',
    }))
    agent.add(conv);
  }

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

  if (agent.requestSource === agent.ACTIONS_ON_GOOGLE) {
    intentMap.set('location', locationIntent);
  } else {
    intentMap.set(null, other);
  }
  agent.handleRequest(intentMap);
});

app.use('/', router);

module.exports = app;

我已经用过这个

const { WebhookClient} = require('dialogflow-fulfillment');

const { dialogflow,
  SimpleResponse,
  BasicCard,
  Suggestions,
  Permission,
  UpdatePermission,
  RegisterUpdate,
  Carousel,
} = require('actions-on-google');

我的依赖项是:

  "dependencies": {
    "actions-on-google": "^2.5.0",
    "aws-serverless-express": "^3.3.5",
    "body-parser": "^1.18.3",
    "compression": "^1.7.3",
    "cors": "^2.8.5",
    "dialogflow": "^4.0.3",
    "dialogflow-fulfillment": "^0.6.1",
    "express": "^4.16.4",
    "googleapis": "^27.0.0"
  }

我从 dialogflow 博客中的这个 post 获得了许可意向: https://blog.dialogflow.com/post/fulfillment-library-beta/

我检查了我的 aws-lambda 中的日志,发现了这个:

TypeError: Cannot read property 'forEach' of undefined at V2Agent.addActionsOnGoogle_

当我 agent.add(conv) 时会发生这种情况。如果我这样做 agent.add("This is location permission intent"); 有效。我希望获得许可并稍后使用它来获取一些坐标

我正在使用节点 v10.15.0

来自 aws-lambda 的 Stracktrace:

2019-01-24T12:42:52.013Z    2531e964-1f94-4f41-997a-a4ef14cb55da    TypeError: Cannot read property 'forEach' of undefined
at V2Agent.addActionsOnGoogle_ (/var/task/node_modules/dialogflow-fulfillment/src/v2-agent.js:313:29)
at WebhookClient.addResponse_ (/var/task/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:269:19)
at WebhookClient.add (/var/task/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:245:12)
at locationIntent (/var/task/app.js:75:11)
at WebhookClient.handleRequest (/var/task/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44)
at router.post (/var/task/app.js:156:9)
at Layer.handle [as handle_request] (/var/task/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/task/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/task/node_modules/express/lib/router/layer.js:95:5)

注意:我已经尝试使用 google 上的操作中的其他元素,例如轮播,但失败了。这让我觉得 agent.addconv

有问题

这强烈表明,尽管列出了依赖项,但您使用的是 dialogflow-fulfillment 库的旧版本。确保您安装了最新版本

npm update