-
Notifications
You must be signed in to change notification settings - Fork 256
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
base: master
Are you sure you want to change the base?
middleware proposal #104
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) | ||
}) | ||
} | ||
start(port) { | ||
// set middlewares | ||
this.setBeforeMiddlewares( this._options.beforeMiddlewares || [] ) | ||
this.app.use(bodyParser.json({ verify: this._verifyRequestSignature.bind(this) })); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. So we could make Or do you have a reason for setting it after |
||
//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'); | ||
|
There was a problem hiding this comment.
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.