Deploying a bot to a server

Example file download here.

You will deploy your bots on a Heroku server. Heroku is a remote server service. It offers free server space for low use. It also has a good command line interface. HUZZAH!

You will be deploying your bot using the command line and a program called git. Git is a version management system that also underlies the website ‘Github’. It is a way of sending files between your computer and a server and keeping track of the different versions and how each version is changed. It can be VERY complicated so we are only going to use it a little as we have to to manage the versions of your bot.

Don’t worry, this tutorial outlines how you can use it to get your bot running autonomously.

Setup

1. Install git. For a mac you can install git from the Git for Mac website at http://mac.github.com. For Windows, install from here: https://windows.github.com/. During install, this tool has an option to install command line tools as well. Make sure you choose this option.

2. Sign up for a Heroku account. Choose node.js as your development language. Login to your account and follow the instructions to download and install the Heroku toolkit for your operating system from here. (https://devcenter.heroku.com/articles/getting-started-with-nodejs#set-up).

Once you have finished making your bot javascript file and set up your config file with the twitter authentification information, go to your terminal. We are now going to set up.

3. Open your terminal and cd to the folder where you have your bot files saved.

4. Check your bot is working with no errors, you should be able to run it in your terminal by calling

node yourfilename.js

Git in your local directory

5. Now make your local folder a Git repository. This adds a .git folder to the current directory and makes it possible to start recording revisions of the project. It is a bit of software that tracks what version is in your local folder and what version is in your remote folder (on the server). You do this by:

git init

Authenticate with Heroku

6. Authenticate with heroku on your computer. Do this by entering

heroku login

and enter your heroku user name and password.

Create your app on the Heroku server.

7. You can do this with the following command. This creates a placeholder for where we will upload your new app on the Heroku server. Doesn’t matter what you call it here, its just for your future reference. I’ve called mine filebotty.

heroku create filebotty

You should see text print out in the terminal like:
Creating filebotty… done, stack is cedar-14
https://filebotty.herokuapp.com/ | https://git.heroku.com/filebotty.git

You can check also this worked by going to the heroku website and now looking at the menu option myApps where you should see the name of your app in the list.

8. Set this up as your remote directory with the command:

heroku git:remote -a filebotty

9. Check it worked by entering:

git remote -v

. You should see something like this print out:
Screen Shot 2015-04-22 at 10.46.29 PM
Now you have set up a link between your local file and the folder on the Heroku server. Good job! Nearly there.

Prepare your bot files for deployment

10. Now it is time to push your files to your remote folder. Git requires you do this in 3 stages, add, commit, and push.
Stage your files.

    git add .

Now commit the changes to the repository:

    git commit -m "Demo"

Now deploy, just as you did previously:

    $ git push heroku master

Congratulations you have pushed your first app to a remote server.

Package.json

You are not quite finished. If your repository (your folder) does not contain a Package.js file or a Procfile you will need to create one. If it does, you need to update, save and then push this to heroku.

If you don’t have these, add in a json file that will enable your bot to run. This file contains instructions as to what modules your bot uses to run. JSON refers to a file with a particular structure for storing data. Many APIS return their data as a JSON file. Generally JSON files have lots of brackets.

In your text editor open a new file and copy the code from here into it. Update the author with your name and twitter handle. Save the file in your twitter bot repository as package.json

Create a Procfile

Create a file called Procfile in your project’s folder using your text editor. This file tells the server what sort of app you’re running and the command that runs the app.

The default Node.js gettings started docs explain that you should define a process type of web, but in our case, without a web interface to our Twitter bot, we need a different type of single process type.

There are a variety of process types that can be defined in a Procfile, but for something really simple like our Twitter bot, we just a need a worker. A worker is a background job that can run outside a user-initiated request/response cycle.

Put the following text into your Procfile (using the name of your js file).

worker: node filebot.js

This file does not have an extension.

Push changes to Heroku

As you change and modify your bot you will want to push them to the remote server. Edit your files as normal and when you are done, save them and cd into the folder from the terminal. Once there enter the following:

Stage local changes

    git add .

Now commit the changes to the repository:

    git commit -m "Demo"

Now deploy, just as you did previously:

    git push heroku master

Now you have a bot, ready to go on your Heroku server. Well done.

It’s alive

Last thing to do is to go to the Heroku website, go to myApps. Click on your app and go to the Resources folder. You need to set up a dyno. This is the process that will kick off your bot. Click edit over on the right and then pull the slider so that you have one dyno running.