Skip to content
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ If you want to specify a custom endpoint name for your webhook, you can do it wi

Starts the express server on the specified port. Defaults port to 3000.

#### `.getRouter()`
Returns the express router, that BootBot uses. It can be [used](https://expressjs.com/en/guide/routing.html#express-router), when you can only use one port.


##### `.getRouter()` examples:

```javascript
app.use('/bootbot', bot.getRouter());

app.listen(3000, () => console.log('BootBot and Express are listening on port 3000!'));
```

#### `.close()`

Closes the express server (calls `.close()` on the server instance).
Expand Down
19 changes: 15 additions & 4 deletions lib/BootBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class BootBot extends EventEmitter {
this.appSecret = options.appSecret;
this.broadcastEchoes = options.broadcastEchoes || false;
this.app = express();
this.router = express.Router();
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) }));
Expand All @@ -41,7 +42,7 @@ class BootBot extends EventEmitter {
* @param {Number} [port=3000]
*/
start(port) {
this._initWebhook();
this._initWebhook('app');
this.app.set('port', port || 3000);
this.server = this.app.listen(this.app.get('port'), () => {
const portNum = this.app.get('port');
Expand All @@ -50,6 +51,16 @@ class BootBot extends EventEmitter {
});
}

/**
* Returns the express router, that BootBot uses.
* It can be used, when you can only use one port.
*/
getRouter(){
this._initWebhook('router');
console.log('BootBot running on the same port as express');
return this.router;
}

/**
* Closes the express server (calls `.close()` on the server instance).
*/
Expand Down Expand Up @@ -597,8 +608,8 @@ class BootBot extends EventEmitter {
}


_initWebhook() {
this.app.get(this.webhook, (req, res) => {
_initWebhook(app='app') {
this[app].get(this.webhook, (req, res) => {
if (req.query['hub.mode'] === 'subscribe' && req.query['hub.verify_token'] === this.verifyToken) {
console.log('Validation Succeded.')
res.status(200).send(req.query['hub.challenge']);
Expand All @@ -608,7 +619,7 @@ class BootBot extends EventEmitter {
}
});

this.app.post(this.webhook, (req, res) => {
this[app].post(this.webhook, (req, res) => {
var data = req.body;
if (data.object !== 'page') {
return;
Expand Down