actions-on-google / assistant-conversation-nodejs

A developer friendly way to fulfill Actions SDK handlers for the Google Assistant
https://actions-on-google.github.io/assistant-conversation-nodejs
Apache License 2.0
104 stars 27 forks source link

How to explicitly handle lambda request? #35

Open mkliu opened 3 years ago

mkliu commented 3 years ago

So i know i could do

module.exports.googleHandler = googleActionApp

But i want to explicitly handle the call myself, so i could do something before the response is sent. Something like this:

module.exports.googleHandler = async function(event: any, context: any, callback: any) {
  if (!googleActionApp) {
      googleActionApp = createApp()
  }
  (googleActionApp as LambdaHandler)(event, context, callback)
}

I could see my handler's first method is starting to get called, but it always exits pre-maturally. Any suggestions?

Basically i want to flush logging before exit, if anyone know how to have a response middleware, it'll do the job too.

mkliu commented 3 years ago

I looked at the implementation and trying to do it this way:

async function(event: any, context: any, callback: any) {
  if (!googleActionApp) {
      googleActionApp = createApp()
  }
  await googleActionApp.frameworks.lambda.handle(googleActionApp.handler)(event, context, (err, res) => {
    console.log('googleActionApp callback', err, res)
    return res;
  })
}

The request is returned by with this error from google test console Invalid response from webhook: Failed to translate JSON to ExecuteHttpResponse..

mkliu commented 3 years ago

nvm, find the solution myself, for anyone who's interested, this would work:

async function(event: any, context: any, callback: any) {
  if (!googleActionApp) {
      googleActionApp = createApp()
  }
  let response
  await googleActionApp.frameworks.lambda.handle(googleActionApp.handler)(event, context, (err, res) => {
    console.log('googleActionApp callback', err, res)
    response = res;
  })
  return response
}
Fleker commented 3 years ago

Seems like you may be interested in a postware method to compliment the middleware method.