Heroku, Sheroku, Zheroku, We All Roku

If I understand your email, you've already got the Heroku toolbelt installed so you can get a list of your apps by running:

$ heroku list

I'm going to assume the app name in this case is bletchley-bot, I hope that's right!



1: Git init and Link to Heroku App

First thing first, we need to make your folder with source code/txt file into a git repository, then tie it to your Heroku app. In your working directory do:

$ git init
$ heroku git:remote -a bletchley-bot

You should see "Git remote heroku added," and now the heroku command knows what app we're dealing with here.



2: Use .env file for secrets

Since we're going to be putting this code into source control, you don't want your API secrets in the .py file itself. Just make a file named .env that looks like this:

CONSUMER_KEY=
CONSUMER_SECRET=
ACCESS_KEY=
ACCESS_SECRET=

Fill in each of those lines (no need for quotation marks.) And update your bletchley.bot.py to use Environment Variables instead:

import os

CONSUMER_KEY = os.environ['CONSUMER_KEY']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
ACCESS_KEY = os.environ['ACCESS_KEY']
ACCESS_SECRET = os.environ['ACCESS_SECRET']

Now the secrets can be sent over to Heroku as "Config Vars" which live in the web dashboard but aren't in source control for other people to steal:

$ heroku config:push

To make sure the .env file doesn't leave your computer, you want a .gitignore file in the folder that just has one line: .env.



3: Procfile Tells Heroku What To Do

Where you say your command to run is just python bletchley.bot.py bletchley.txt we want to tell Heroku that's what it should do when it's starting/restarting as well. Create a Procfile:

worker: python bletchley.bot2.py bletchley.txt

Here we name the process "worker" and tell it this is what the bot does.



4: requirements.txt Tells Us What Python Needs

So how the script requires tweepy? We might have any number of modules python needs to install.

Depending on the language, Heroku has a file telling it what the prerequisities for running are. Node has package.json, Ruby has Gemfile and Python has requirements.txt:

tweepy==3.1.0

With this, Heroku knows it's a Python app and knows what it need to get it up and running.

5: Ship it!

Now we have everything we need in this folder: your python script, your txt file, the .env file with secrets, the .gitignore file so we don't share those secrets, and the Procfile.

Let's tell git these are the files we want, and then ship them off to Heroku, along with a nice free online log viewer:

$ git add .gitignore bletchley.bot.py bletchley.txt Procfile requirements.txt
$ git commit -m "First commit!"
$ heroku addons:add papertrail
$ git push heroku master

There will be some messages about Heroku staging and preparing the environment, and hopefully nothing blows up!



6: Start it Up

A kind of weird thing about Heroku is the app deploys with 0 "Dynos" active, meaning it won't do anything. You can either scale it up to 1 dyno from the web dashboard, or get it rolling with this command:

$ heroku ps:scale worker=1

That should be everything!

This took longer to type up than I expected so I haven't actually been able to test it. I'll be happy to do so, but I have to get back to work for now!