如何在Express中使用Node JS客户端库? [英] How to use the node js client library with express?

查看:86
本文介绍了如何在Express中使用Node JS客户端库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用node js客户端库就实现了dialogFlow实现。我一直在解析请求并手动准备响应。现在,我想开始使用节点js客户端库,如下所示: https://actions-on-google.github.io/actions-on-google-nodejs/

I have implemented a dialogFlow fulfillment without using the node js client library. I have been parsing requests and preparing responses manually. Now, I wanted to start using the node js client library, as found here: https://actions-on-google.github.io/actions-on-google-nodejs/

我正在使用express框架作为由快递生成器生成。
这是我app.js的一些内容:

I am using the express framework as generated by the express generator. Here is some of the contents of my 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;

my ./routes/smarthome5包含以下内容:

my ./routes/smarthome5 contains these:

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请求时,出现类似所以:

However, when I run the code and make a POST request to /smarthome5, I get an error like so:

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中的后处理程序时:

when I try replacing the post handler in ./routes/smarthome5 with this:

router.post('/', function(req, res, next) {
    console.log('POST ' + __filename);
    console.dir(req.body, {depth: null});
})

我能够看到POST请求。因此,我的路线正在运行,只是我不知道我是否正确使用了dfApp。

I am able to see the POST request. So my routes are working it's just that I do not know if I am using the dfApp correctly.

示例帖子请求正文和错误

请求正文1

originalDetectIntentRequest

标题

用户存储字段

推荐答案

问题是您的userStorage具有设置为字符串。该库假定此字符串为JSON并尝试对其进行解码。但是,该库在不存在时无法处理(很可能是因为它在保存时确保它是JSON)。

The issue is that your userStorage has been set to a string. The library assumes that this string is JSON and attempts to decode it. However, the library fails to handle when it isn't (likely because it ensures it is JSON when it saves it).

修复此问题很困难。如果代码在生产中,则可以查看和清除用户存储,但是在目录中列出该存储之前,该存储不可用。您可以用代码清除用户存储-但是在初始化请求时会发生此错误,因此您需要先执行此操作。 (并且您需要在第一次测试时对每个用户进行此操作。)在 router.post('/',dfApp)而不是直接调用 dfApp 函数,而是调用修改 req.body 的函数以删除或从JSON重置该字段。我还没有测试过,但是可能像

Fixing this will be difficult. If the code was in production, you can view and clear the user storage, but this isn't available before it is listed in the directory. You can clear the user storage in code - but this error is happening when it initializes the request, so you need to do this first. (And you only need to do this the first time for each user you've tested with.) In the line router.post('/', dfApp), instead of calling the dfApp function directly, call a function that modifies req.body to remove or reset that field from the JSON. I haven't tested, but possibly something like

router.post('/', function( req, res ){
  req.body.originalDetectIntentRequest.payload.user.userStorage='{}';
  dfApp( req, res );
});

如果您确实需要保留该ID(而不是仅生成一个新ID),则可以因此也要确保该字符串是有效的JSON字符串。

If you really need to preserve that ID (instead of just generating a new one), you can do so there as well, but make sure the string is a valid JSON string.

展望未来,您应该将 conv.user.storage 作为对象。存储用户ID可以使用类似的方法

Going forward, you should treat conv.user.storage as an object. Storing a user ID there would be done with something like

conv.user.storage.id = generateId();

(或选择一些适合您的属性名称。)

(Or pick some attribute name that works for you.)

这篇关于如何在Express中使用Node JS客户端库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆