question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware Support [Feature Request]

See original GitHub issue

Hey @Charca , I saw your post about BootBot in the Bots Facebook group and after looking at the framework, I really want to try BootBot for my next Messenger bot. I’ve been using NLP services in most of my bots (specifically Wit.ai) however and would love it if BootBot had support for middleware to do additional processing when a message is received. I could use bot.on('message') to process messages with Wit.ai, but there wouldn’t be a way to dispatch on these results using bot.hear(). My solution to this is allowing the BootBot to accept middleware, which are executed when a message is received and add a context which hear() can use to dispatch on. Here is some pseudocode of how it could be used:

'use strict';
const BootBot = require('bootbot');

const bot = new BootBot({
  accessToken: 'FB_ACCESS_TOKEN',
  verifyToken: 'FB_VERIFY_TOKEN',
  appSecret: 'FB_APP_SECRET'
});

// A middleware returns a promise that when resolved adds context
// that can be used to dispatch on hear messages
// hear callbacks are not executed until all middleware have been resolved
bot.addMiddleware((message) => fetchWitAiIntent(message).then(
  (witAiResult) => {witai: {intent: witAiResult.userIntent}}
));

// a predicate can be used to decide what response should be triggered by the bot
function userHungry(payload, middlewareContext) {
  return middlewareContext.witAi.intent === 'hungry';
}

// predicate used to determine if case should be executed
bot.hear(['food', userHungry], (payload, chat, middlewareContext) => {
  // Do something here.
});

I don’t know if middleware is in the roadmap or not. Let me know what you think, and if you are fine with it I would love to implement it.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Charcacommented, Aug 5, 2016

Hey @SawyerHood, this is a great suggestion. I wasn’t planning on adding middleware support right now, but anything that helps integrating other services (specially NLP services) it’s in the roadmap!

I like your initial proposal, I’ll run a few experiments during the weekend to get a better feel of it, but please feel free to submit a PR implementing this feature. Can’t wait to see it!

0reactions
Charcacommented, Apr 4, 2017

Yep, I’m going to close this one. Thanks everyone for the comments and the workaround!

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature Request] Add first-party router support in middleware ...
Describe the feature you'd like to request Context Since next@12.1.7-canary.9 Next.js uses one single root middleware instead of nested middleware, ...
Read more >
Advanced Features: Middleware - Next.js
Middleware allows you to run code before a request is completed, then based on the incoming request, you can modify the response by...
Read more >
Request Features — ASP.NET documentation - Read the Docs
Feature interfaces; Feature collections; Middleware and request features ... Http.Features which are used by servers to identify the features they support.
Read more >
ASP.NET Core Middleware | Microsoft Learn
Provides support for decompressing requests. Before components that read the request body. Response Caching, Provides support for caching ...
Read more >
What is middleware? - Red Hat
New application development. Middleware can support modern and popular runtimes for a variety of use cases. Developers and architects can work ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found