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

Provides opportunity to use BootBot without creating a new express server. #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
@@ -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).
19 changes: 15 additions & 4 deletions lib/BootBot.js
Original file line number Diff line number Diff line change
@@ -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) }));
@@ -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');
@@ -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).
*/
@@ -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']);
@@ -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;