actions-on-google api.ai 不会在 POST 请求中使用 nodejs 和 express 发送正文
actions-on-google api.ai doesn't send body in POST request with nodejs and express
我正在尝试在我的计算机上 运行 来自 actions-on-google 和 api.ai 的 sillyNameMaker example。
我用 express 和 ngrok 隧道设置了一个 nodejs 服务器。当我尝试通过我的代理在 api.ai 上发送请求时,我的服务器收到了 POST 请求,但正文似乎是空的。是不是我设置的不对?
这是我的 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!')
})
我得到的错误是:
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 但它未定义...在此先感谢您的帮助。
您和 actions-on-google 包都在假设您如何使用 Express。默认情况下,Express 不会 填充 req.body 属性(参见 reference for req.body). Instead it relies on additional middleware such as body-parser 这样做。
您应该能够使用
将正文解析器添加到您的项目中
npm install body-parser
然后使用它将请求正文解析为 JSON(API.AI 发送和 actions-on-google 使用),并在您定义 app
将其附加到 Express:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
我正在尝试在我的计算机上 运行 来自 actions-on-google 和 api.ai 的 sillyNameMaker example。 我用 express 和 ngrok 隧道设置了一个 nodejs 服务器。当我尝试通过我的代理在 api.ai 上发送请求时,我的服务器收到了 POST 请求,但正文似乎是空的。是不是我设置的不对?
这是我的 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!')
})
我得到的错误是:
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 但它未定义...在此先感谢您的帮助。
您和 actions-on-google 包都在假设您如何使用 Express。默认情况下,Express 不会 填充 req.body 属性(参见 reference for req.body). Instead it relies on additional middleware such as body-parser 这样做。
您应该能够使用
将正文解析器添加到您的项目中npm install body-parser
然后使用它将请求正文解析为 JSON(API.AI 发送和 actions-on-google 使用),并在您定义 app
将其附加到 Express:
var bodyParser = require('body-parser');
app.use(bodyParser.json());