joo*_*ood 1 node.js express actions-on-google google-home dialogflow-es
我正试图在我的计算机上运行带有api.ai的actions-on-google上的sillyNameMaker示例.我用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!')
})
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
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)
Run Code Online (Sandbox Code Playgroud)
我正在尝试打印req.body,但它未定义...提前感谢您的帮助.
您和Google上的操作包都在假设您如何使用Express.默认情况下,快递也没有填充req.body属性(见的req.body参考).相反,它依赖于身体解析器等额外的中间件来实现.
您应该能够使用身体解析器添加身体解析器
npm install body-parser
Run Code Online (Sandbox Code Playgroud)
然后使用它将请求主体解析为JSON(API.AI发送和使用google上的操作),并在您定义app将其附加到Express 之后立即使用一些额外的行:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1452 次 |
| 最近记录: |