jschnurr / botkit-middleware-dialogflow

Botkit middleware for Dialogflow (formerly api.ai). Supports Dialogflow API v1 and V2.
MIT License
29 stars 18 forks source link

Remove sending error through next #25

Closed adantoscano closed 3 years ago

adantoscano commented 5 years ago

I've just removed sending error to continue with botkit pipeline to handle the raw message. #24

StephRicardo commented 4 years ago

Hello! I am using this middleware in one of my projects, and I've noticed this error appear when my bot posts a message. Any information on whether this fix will be applied?

dermidgen commented 4 years ago

@jschnurr Any updates on this? In the meantime, I've been monkey-patching around this. @adantoscano you might look at this solution if we can't get your patch merged.

const DialogFlowMiddleWare = require('botkit-middleware-dialogflow')
const { GOOGLE_APPLICATION_CREDENTIALS } = process.env

const dialogflowMiddleware = GOOGLE_APPLICATION_CREDENTIALS
  ? DialogFlowMiddleWare({
      keyFilename: GOOGLE_APPLICATION_CREDENTIALS,
    })
  : DialogFlowMiddleWare()

const middlewareWrapper = controller => (bot, message, next) => {
  const _next = err => {
    if (!err) return next()
    message.error = err
    controller.trigger('pipeline_error', bot, message)
  }
  dialogflowMiddleware.receive(bot, message, _next)
}

module.exports = {
  dialogflowMiddleware: middlewareWrapper,
}

Setup with

controller.middleware.receive.use(dialogflowMiddleware(controller))

Here, we don't fire next() at all. Instead, we allow botkit to intercept and we fire the pipeline_error event - which appears to be borked. I expected botkit to fire pipeline_error or receive_error when calling next(err) from a receive middleware.

Since next() isn't called, and the message is intercepted, your upstream features won't be called. You can trap the triggered pipeline_error with:

  controller.on('pipeline_error', (bot, message) => {
    const { error = {} } = message
    bot.reply(
      message,
      `There was an error processing your request. Please try again later. Error: ${error.toString()}`
    )
  })

Of course, you can inspect the error a give a more meaningful response to the user. Like trapping the 256 char limit and giving the user a nice message as referenced in #24

adantoscano commented 3 years ago

Close due proyect discontinued