zhuiks
zhuiks

Reputation: 111

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

I'm trying to use actions-on-google with koa framework. I can't understand which request and response to use with AoG constructor in koa middleware.

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:

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. I'm just not sure about full json response format for Dialogflow fulfillment in case of AoG request

Upvotes: 1

Views: 628

Answers (1)

Sachit Mishra
Sachit Mishra

Reputation: 309

The Node.js Client Library for Actions on Google expects a Express-style request/response pair.

Upvotes: 2

Related Questions