首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >actions-on google api.ai不使用nodejs和express在POST请求中发送正文

actions-on google api.ai不使用nodejs和express在POST请求中发送正文
EN

Stack Overflow用户
提问于 2017-04-21 13:25:56
回答 1查看 1.6K关注 0票数 1

我正试图在我的电脑上运行sillyNameMaker实例,使用谷歌上的actions和api.ai。我用express和ngrok隧道设置了一个nodejs服务器。当我试图在api.ai上用我的代理发送请求时,我的服务器会收到POST请求,但是主体似乎是空的。有什么事情我没做好吗?

这是我的index.js文件:

代码语言:javascript
运行
复制
'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!')
})

我所犯的错误是:

代码语言:javascript
运行
复制
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)

我想打印req.body,但它没有定义.提前谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-21 14:01:58

你和谷歌上的行动包都在假设你是如何使用Express的。默认情况下,Express不使用而不是填充req.body属性(参见req.body参考)。相反,它依赖于额外的中间件,如体解析器

您应该能够将正文解析器添加到您的项目中

代码语言:javascript
运行
复制
npm install body-parser

然后使用它将请求体解析为JSON ( API.AI发送和操作-on-google使用),并在定义app之后添加一些行,将其附加到Express:

代码语言:javascript
运行
复制
var bodyParser = require('body-parser');
app.use(bodyParser.json());
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43543965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档