【发布时间】:2018-08-31 19:28:47
【问题描述】:
我在没有使用 node js 客户端库的情况下实现了一个 dialogFlow 实现。我一直在手动解析请求并准备响应。现在,我想开始使用 node js 客户端库,如下所示:https://actions-on-google.github.io/actions-on-google-nodejs/
我正在使用 express 生成器生成的 express 框架。 下面是我的 app.js 的一些内容:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var smarthome5 = require('./routes/smarthome5');
app.set('view engine', 'jade');
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/smarthome5',smarthome5);
module.exports = app;
我的 ./routes/smarthome5 包含这些:
const express = require('express')
var router = express.Router();
const {dialogflow} = require('actions-on-google')
const dfApp = dialogflow()
router.post('/', dfApp)
// Register handlers for Dialogflow intents
dfApp.intent('Default Welcome Intent', conv => {
conv.ask('Hi, how is it going?')
conv.ask(`Here's a picture of a cat`)
})
// Intent in Dialogflow called `Goodbye`
dfApp.intent('Goodbye', conv => {
conv.close('See you later!')
})
dfApp.intent('Default Fallback Intent', conv => {
conv.ask(`I didn't understand. Can you tell me something else?`)
})
module.exports = router;
但是,当我运行代码并向 /smarthome5 发出 POST 请求时,我收到如下错误:
SyntaxError: Unexpected number in JSON at position 1
at JSON.parse (<anonymous>)
at new User (/app/node_modules/actions-on-google/dist/service/actionssdk/conversation/user.js:75:43)
at new Conversation (/app/node_modules/actions-on-google/dist/service/actionssdk/conversation/conversation.js:73:21)
at new DialogflowConversation (/app/node_modules/actions-on-google/dist/service/dialogflow/conv.js:38:9)
at Function.<anonymous> (/app/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:113:24)
at Generator.next (<anonymous>)
at /app/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:22:71
at new Promise (<anonymous>)
at __awaiter (/app/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:18:12)
at Function.handler (/app/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:84:16)
当我尝试用这个替换 ./routes/smarthome5 中的 post 处理程序时:
router.post('/', function(req, res, next) {
console.log('POST ' + __filename);
console.dir(req.body, {depth: null});
})
我可以看到 POST 请求。所以我的路线是有效的,只是我不知道我是否正确使用了 dfApp。
【问题讨论】:
-
你能显示 req.body 的内容吗? (或者至少是它的前几个字符?)
-
我已经编辑了我的帖子以包含:)
-
显示标题,但不显示正文。由于错误与 JSON 正文有关,因此我们需要查看它以帮助弄清楚发生了什么。
-
我的错。我已经包含了请求正文第一部分的快照
-
好的,至少这是一个好兆头。您能否在
originalDetectIntentRequest下包含包含user对象的请求正文部分?