Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware proposal #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/BootBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,45 @@ class BootBot extends EventEmitter {
if (!options || (options && (!options.accessToken || !options.verifyToken || !options.appSecret))) {
throw new Error('You need to specify an accessToken, verifyToken and appSecret');
}
this._options = options
this.accessToken = options.accessToken;
this.verifyToken = options.verifyToken;
this.appSecret = options.appSecret;
this.broadcastEchoes = options.broadcastEchoes || false;
this.app = express();
this.webhook = options.webhook || '/webhook';
this.webhook = this.webhook.charAt(0) !== '/' ? `/${this.webhook}` : this.webhook;
this.app.use(bodyParser.json({ verify: this._verifyRequestSignature.bind(this) }));
this._hearMap = [];
this._conversations = [];
}

setBeforeMiddlewares( beforeMiddlewares ){
if( !( beforeMiddlewares instanceof Array ) ){
throw new Error( 'Paremeter in setBeforeMiddlewares() must be an array of middlewares' )
}else if( !beforeMiddlewares || !beforeMiddlewares.length ){
return
}
beforeMiddlewares.forEach( middleware => {
this.app.use( middleware )
})
}
setAfterMiddlewares( afterMiddlewares ){
if( !( afterMiddlewares instanceof Array ) ){
throw new Error( 'Paremeter in setAfterMiddlewares() must be an array of middlewares' )
}else if( !afterMiddlewares || !afterMiddlewares.length ){
return
}
afterMiddlewares.forEach( middleware => {
this.app.use( middleware )
})
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two methods are identical, you can just have a setMiddlewares() method and you just call it before and after initializing the webhooks with different params.

start(port) {
// set middlewares
this.setBeforeMiddlewares( this._options.beforeMiddlewares || [] )
this.app.use(bodyParser.json({ verify: this._verifyRequestSignature.bind(this) }));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives us an opportunity to make this optional (but defaulting to active) in case you are handling other kinds of non-bot http requests with this express instance.
I, for example, have had to patch this in my own multipage bot in the past.

So we could make _verifyRequestSignature "public", add a boolean this.shouldVerifyRequest or something, set that default to true and also add it before setBeforeMiddlewares so we can halt the request before hitting other middlewares so we don't waste cpu cycles.

Or do you have a reason for setting it after setBeforeMiddlewares @JacobGadawski?

//init webhook
this._initWebhook();
this.setAfterMiddlewares( this._options.afterMiddlewares || [] )

this.app.set('port', port || 3000);
this.server = this.app.listen(this.app.get('port'), () => {
const portNum = this.app.get('port');
Expand Down
Loading