Middleware Support [Feature Request]
See original GitHub issueHey @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:
- Created 7 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top GitHub Comments
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!
Yep, I’m going to close this one. Thanks everyone for the comments and the workaround!