1

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

zhuiks
  • 111
  • 7

1 Answers1

2

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

Sachit Mishra
  • 309
  • 1
  • 4
  • What should be the response format for Google Assistant [Dialogflow.ai fulfilment](https://dialogflow.com/docs/fulfillment) if I don't want to use Client Library for AoG? – zhuiks Dec 29 '17 at 04:40
  • You can use the 'data' portion of the fulfillment response to send Google specific visual element items in the richResponse property of the data.google element of your JSON, as shown here https://developers.google.com/actions/dialogflow/webhook#response – Sachit Mishra Jan 02 '18 at 14:56