actions-on-google / actions-on-google-nodejs

Node.js client library for Actions on Google
https://actions-on-google.github.io/actions-on-google-nodejs
Apache License 2.0
899 stars 193 forks source link

How to bypass middleware and only get JSON response #418

Open JpEncausse opened 3 years ago

JpEncausse commented 3 years ago

The documentation describe a middleware that will register functions on a given intent.

I'm using Node-RED with a fullfilment end-point. I can't and don't want to build a middleware because I want to manage the response flow. I only need a wrapper on the API to retrieve a valid JSON.

Is there a way to use the conv object in order to retrieve a valid JSON response ? Something like in the playground

Fleker commented 3 years ago

Do you want to use the library then, or would you just prefer sending JSON back directly?

If you want the library, you can use something similar to the session-entities-plugin and use the serialize method:

const responseBody = this.conv.serialize() as ResponseBody
const convBody = this.conv.body as GoogleCloudDialogflowV2WebhookRequest
responseBody.sessionEntityTypes = this.entities.map(entity => {
    entity.name = `${convBody.session}/entityTypes/${entity.name}`
    return entity
})
this.conv.json(responseBody)