【问题标题】:Correct way to send response when using Actions-on-google with koa.js将 Actions-on-google 与 koa.js 一起使用时发送响应的正确方法
【发布时间】:2018-06-07 14:55:13
【问题描述】:

我正在尝试将 actions-on-google 与 koa 框架一起使用。 我无法理解在 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 请求的情况下,我只是不确定 Dialogflow 实现的完整 json 响应格式

【问题讨论】:

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


    【解决方案1】:

    用于 Google Actions 的 Node.js 客户端库需要 Express 样式的 request/response 对。

    【讨论】:

    猜你喜欢
    • 2017-10-28
    • 2019-07-08
    • 2015-10-22
    • 2015-07-08
    • 2018-06-07
    • 2014-08-30
    • 2011-10-05
    • 2015-03-18
    相关资源
    最近更新 更多