[英]actions-on-google api.ai doesn't send body in POST request with nodejs and express
我試圖在我的計算機上使用api.ai從Google- Actions上運行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!')
})
我得到的錯誤是:
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,但是它是不確定的...預先感謝您的幫助。
您和Google Actions程序包都在假設您如何使用Express。 默認情況下,快遞不填充req.body屬性(見的req.body參考 )。 相反,它依賴於諸如body-parser之類的其他中間件。
您應該可以使用以下方法將正文解析器添加到項目中
npm install body-parser
然后在定義將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.