简体   繁体   English

在Koa.js上使用Google Actions-on时发送响应的正确方法

[英]Correct way to send response when using Actions-on-google with koa.js

I'm trying to use actions-on-google with koa framework. 我正在尝试通过koa框架使用Google行动。 I can't understand which request and response to use with AoG constructor in koa middleware. 我不明白在koa中间件中与AoG构造函数一起使用的请求和响应。

This is my code: 这是我的代码:

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 }` )
} )

Trying it on AoG console Simulator I get the following error: 在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)

If I use json response instead of .ask() it works fine. 如果我使用json响应而不是.ask()则可以正常工作。 I'm just not sure about full json response format for Dialogflow fulfillment in case of AoG request 我只是不确定在AoG请求的情况下Dialogflow实现的完整json响应格式

Google上的Node.js Actions客户端库需要Express样式的请求 / 响应对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM