在koa.js上使用带谷歌的动作时发送响应的正确方法

时间:2017-12-26 21:56:14

标签: node.js actions-on-google koa dialogflow koa2

我正在尝试使用koa框架上的google动作。 我无法理解在koa中间件中使用AoG构造函数的请求和响应。

这是我的代码:

const Koa = require( 'koa' )
const koaBody = require('koa-body')
const { DialogflowApp } = require('actions-on-google')

const koaApp = new Koa()
koaApp.use(koaBody())

koaApp.use(async (ctx) => {
    const googleAssistant = new DialogflowApp({request: ctx.request, response: ctx.response});
    const body = ctx.request.body;
    console.log(body)
    if (!body.result) ctx.throw(400, 'wrong request')

    ...

    const listItems = []
    results.forEach((result, i) => {
        listItems.push(
            googleAssistant.buildOptionItem('Item'+(i+1))
                .setTitle(result.title)
                .setDescription(result.text)
        )
    })
    const list = googleAssistant.buildList().addItems(listItems)
    console.log('Response to Dialogflow (AoG): ' + JSON.stringify(list))
    googleAssistant.askWithList('Here some results', list)
})

const PORT = process.env.PORT || 3000
koaApp.listen( PORT, () => {
   console.log( `Listening on ${ PORT }` )
} )

在AoG控制台模拟器上尝试它我收到以下错误:

server error TypeError: this.response_.status is not a function 
    at DialogflowApp.doResponse_ (/home/zhuiks/myProject/node_modules/actions-on-google/assistant-app.js:2372:41)
    at DialogflowApp.askWithList (/home/zhuiks/myProject/node_modules/actions-on-google/dialogflow-app.js:620:17)
    at setGoogleResponse (/home/zhuiks/myProject/app.js:73:25)
    at koaApp.use (/home/zhuiks/myProject/app.js:88:9)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:169:7)

如果我使用json响应而不是.ask(),它可以正常工作。在AoG请求的情况下,我只是不确定完全json响应格式的Dialogflow实现

1 个答案:

答案 0 :(得分:2)

针对Google上的操作的Node.js客户端库需要快速式request / response对。