Search code examples
node.jsexpressciscobotkit

Host a Node.js bot (express and botkit)


I just made a bot in node.js for the Cisco Webex Teams application. My bot uses "express" and "botkit". "Express" requires listening on the port "3000" and "Botkit" listening on the port "8080".

I tried heroku.com but it does not accept two predefined ports and does not save files dynamically (fs.write)

var PUBLIC_URL = "http://a796e3b7.ngrok.io";
var port ='3000';
var ACCESS_TOKEN ='xxx';
var SECRET = "xxx";

var express = require('express');
var app = express();

var Botkit = require('botkit');
var controller = Botkit.webexbot({
    log: true,
    public_address: PUBLIC_URL,
    access_token: ACCESS_TOKEN,
    secret: SECRET,
    webhook_name: process.env.WEBHOOK_NAME || 'Email2Webex',
});

controller.setupWebserver(8080, function(err, webserver) {
    controller.createWebhookEndpoints(webserver, bot, function() {
        console.log("Webhooks set up!");
    });
});

app.post('/mailgun', upload.any(),function(req, res, next){

  res.end('ok');

});

app.listen(port);

Currently I use ngrok to host the bot locally on my computer and I want to be able to host it on a server so I do not have to worry about it. how can I do ?


Solution

  • You can't set the port on Heroku apps. Heroku sets the port you're supposed to use through the PORT environment variable, and you should use it via process.env.PORT. Generally speaking, deployed applications should not run on development ports like 8080 - if it's an HTTP server, it must listen on port 80, for example.

    In order to have two apps listening at the same time, I suggest you refactor your code and include both your bot and your app into a single express server that will listen at the port defined by Heroku's PORT environment variable.

    Concerning access to the file system, it is borderline possible to use it, but there are high security restrictions, so a code that might run on your machine is likely to break on the server. Generally speaking it's a bad idea to access the file system directly in Heroku, except for read-only actions on deployed files. That is in part because the file system is ephemeral, so dont assume your written files will always be there. Most issues related to the caveats of using the file system can be resolved by using database or file storage features provided by Heroku, though.