Skip to content

Deploying to Heroku

Boris Mann edited this page Mar 5, 2017 · 5 revisions

Back to Home


Deploying to Heroku

Creating a server that runs SuperScript

You can use the bot-init script to quickly get an express server that runs SuperScript, and exposes a REST endpoint to get messages from it.

To do so, ensure you have SuperScript installed globally (npm install -g superscript), and run:

bot-init hal9000-bot --clients express

Be sure to replace hal9000-bot with your actual bot name! Now, we just need to check it works locally. Run the following:

cd hal9000-bot
npm install
npm run parse
npm run start-express

Note: you need to have an instance of mongo running locally, or you will get an error.

This will install dependencies, parse the example script, build the server's source code and start up the server for you, by default on http://localhost:5000.

Go ahead and send a message to your bot to double-check it works, by providing the message as the message query parameter in the URL string like this: http://localhost:5000/superscript?message=hi

Uploading to Heroku

Now we have a server we can get it running on Heroku. Firstly, if you haven't already got a Heroku account, sign up for one. It's free for experimenting in!

Next, log in with your account and go to the dashboard. Click Create New App, and enter your app name (say, 'hal9000-chatbot') and the region you want to host the service in.

You'll find yourself on a page which details how to upload your repo to Heroku. First, follow the instructions to install the Heroku CLI and login.

Then, follow the instructions to add heroku as a remote to your repo.

Next, go to the 'resources' tab on the Heroku dashboard and look for the 'mLab' add-on. Create a new free instance. This'll be for the database to store all your bot's juicy chat and facts.

You can also run heroku addons:create mongolab --app YOUR-APP-NAME on the command line to create the mongo database.

Now, there are two extra options you need to add to server-express.js to get it up and running on Heroku as opposed to locally. The following should be added to the server-express.js in the src directory:

const options = {
  logPath: null,
  mongoURI: process.env.MONGODB_URI || 'mongodb://localhost:5000/superscriptdb',  
};

logPath disables writing to the file system (which Heroku doesn't support) and setting the Mongo URI sets it to the mLab instance we just created. Don't forget to run npm run build after making this change so it is applied to the live script in the lib directory.

Heroku also needs to know which script to run. You can either add a Procfile, or more simply, add a new script to the generated package.json:

"start": "node lib/server-express.js"

To verify that everything is running, you can run heroku local which will attempt to use the heroku process to run locally.

Next, git add ., git commit -m "Make it come alive", git push heroku master and wait for it to come online.

Tada! You should now be able to just go to https://hal9000-chatbot.herokuapp.com/superscript?message=hi to see your bot in action!

Deploy to Heroku Button

There are Deploy to Heroku buttons for Express and Slack now https://github.com/bmann/superscript-heroku

Other Heroku Issues

You can run Cleanup directly on heroku:

heroku run node_modules/.bin/cleanup

HOWEVER, there is currently an error because Cleanup is looking for MONGO_URI rather than MONGODB_URI. You can copy your existing MONGODB_URI config variable and add it to Heroku as MONGO_URI and this will work normally.