Charca / bootbot

Facebook Messenger Bot Framework for Node.js
MIT License
974 stars 252 forks source link

Https server ? #182

Closed mrpinkcat closed 5 years ago

mrpinkcat commented 5 years ago

Can I use https server with certificate insted express http server ? @Charca

mrpinkcat commented 5 years ago

I have extend the BootBot class for https server.

class HttpsBootBot extends BootBot {
    constructor(option) {
        super(option);
    }
    startHttps(credentials, port) {
        console.log('BOT Https');
        this.httpsServer = https_1.default.createServer(credentials, this.app);
        this.start(port);
    }
    start(port) {
        this._initWebhook();
        this.app.set('port', port || 3000);
        if (this.httpsServer) {
            this.httpsServer.listen(this.app.get('port'), () => {
                const portNum = this.app.get('port');
                console.log('BootBot running on port', portNum);
                console.log(`Facebook Webhook running on localhost:${portNum}${this.webhook}`);
            });
        }
        else {
            console.log('error extend bootbot start');
        }
    }
    close() {
        if (this.httpsServer) {
            this.httpsServer.close();
        }
        else {
            console.log('error extend bootbot stop');
        }
    }
}

Insted of call new BootBot(...).start(port) you can now call startHttps e.g.

const privateKey = fs.readFileSync(env_1.config.tls.key, 'utf8');
const certificate = fs.readFileSync(env_1.config.tls.cert, 'utf8');
const credentials = { key: privateKey, cert: certificate };

new HttpsBootBot(...).startHttps(credentials, port);
Charca commented 5 years ago

Hey @mrpinkcat, I think that's a solid solution. Thanks for sharing!

If you'd like to add the option to BootBot itself, feel free to open a PR and tag me on it.