简体   繁体   English

google-actions-on-api.ai不会在具有Node.js的POST请求中发送正文

[英]actions-on-google api.ai doesn't send body in POST request with nodejs and express

I'm trying to run the sillyNameMaker example from actions-on-google with api.ai, on my computer. 我试图在我的计算机上使用api.ai从Google- Actions上运行sillyNameMaker示例 I set up a nodejs server with express, and a ngrok tunneling. 我用express和ngrok隧道建立了一个nodejs服务器。 When I try to send a request with my agent on api.ai, my server receives the POST request, but the body appears to be empty. 当我尝试在api.ai上与代理发送请求时,我的服务器收到POST请求,但正文似乎为空。 Is there anything i didn't set up properly? 有什么我没有正确设置的东西吗?

Here is my index.js file: 这是我的index.js文件:

'use strict';
var express = require('express')
var app = express()
const ApiAiAssistant = require('actions-on-google').ApiAiAssistant;

function sillyNameMaker(req, res) {
  const assistant = new ApiAiAssistant({request: req, response: res});

  // Create functions to handle requests here
  const WELCOME_INTENT = 'input.welcome';  // the action name from the API.AI intent
  const NUMBER_INTENT = 'input.number';  // the action name from the API.AI intent
  const NUMBER_ARGUMENT = 'input.mynum'; // the action name from the API.AI intent

  function welcomeIntent (assistant) {
    assistant.ask('Welcome to action snippets! Say a number.');
  }

  function numberIntent (assistant) {
    let number = assistant.getArgument(NUMBER_ARGUMENT);
    assistant.tell('You said ' + number);
  }

  let actionMap = new Map();
  actionMap.set(WELCOME_INTENT, welcomeIntent);
  actionMap.set(NUMBER_INTENT, numberIntent);
  assistant.handleRequest(actionMap);

  function responseHandler (assistant) {
    console.log("okok")
    // intent contains the name of the intent you defined in the Actions area of API.AI
    let intent = assistant.getIntent();
    switch (intent) {
      case WELCOME_INTENT:
        assistant.ask('Welcome! Say a number.');
        break;

      case NUMBER_INTENT:
        let number = assistant.getArgument(NUMBER_ARGUMENT);
        assistant.tell('You said ' + number);
        break;
    }
  }
  // you can add the function name instead of an action map
  assistant.handleRequest(responseHandler);
}


app.post('/google', function (req, res) {
  console.log(req.body);
  sillyNameMaker(req, res);
})


app.get('/', function (req, res) {
  res.send("Server is up and running.")
})


app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

And the error I got: 我得到的错误是:

TypeError: Cannot read property 'originalRequest' of undefined
    at new ApiAiAssistant (/Users/clementjoudet/Desktop/Dev/google-home/node_modules/actions-on-google/api-ai-assistant.js:67:19)
    at sillyNameMaker (/Users/clementjoudet/Desktop/Dev/google-home/main.js:8:21)

I'm trying to print req.body but it is undefined... Thanks in advance for your help. 我正在尝试打印req.body,但是它是不确定的...预先感谢您的帮助。

Both you and the actions-on-google package are making an assumption about how you're using Express. 您和Google Actions程序包都在假设您如何使用Express。 By default, Express does not populate the req.body attribute (see the reference for req.body ). 默认情况下,快递填充req.body属性(见的req.body参考 )。 Instead it relies on additional middleware such as body-parser to do so. 相反,它依赖于诸如body-parser之类的其他中间件。

You should be able to add body parser to your project with 您应该可以使用以下方法将正文解析器添加到项目中

npm install body-parser

and then use it to parse the request body into JSON (which API.AI sends and actions-on-google uses) with some additional lines right after you define app to attach it to Express: 然后在定义将app附加到Express的app后,立即使用它将请求正文解析为JSON(API.AI发送并在Google上执行操作)为JSON(带有一些其他行):


var bodyParser = require('body-parser');
app.use(bodyParser.json());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM