Leaving the Nest: Let your Twitter Bot fly among the clouds

Kimberly McCarty
5 min readOct 11, 2017

--

Enrich your life by adding a robot child to your legacy

Writing a Twitter Bot is a fun programming exercise for programmers of any skill level. There’s lots of API wrappers for every language, and in the end, all you need to worry about is producing a script that makes some text or maybe an image. This goes double for Mastodon, the scrappy new federated microblogging network you’ll probably love if Twitter’s moderation has let you down.

I personally like Python and the Tweepy, but this guide should work for any language that runs on Heroku. I’m going to assume you’ve composed your bot of scripts that can be called from the command line, and already have the bot working on your local machine.

You’ll also need your own twitter API keys from apps.twitter.com. Twitter requires four cryptographic strings (Consumer key, consumer secret, Access token and Access token secret), which you can generate for your own account through the Twitter developer page, or you can request the access token and access token secret from Tweepy in the python repl following the guide in Tweepy’s docs.

Periodically running a script is a decent option if you don’t need your twitter bot to reply immediately to somebody. On the free tier, your script will run every 10 minutes or every hour, or daily, which I think are perfectly decent frequencies for the sort of bots I would like to make.

And if your bot needs to change or save data, you need to make sure it’s working with an interface that stores it on one of the DBs Heroku provides. I like PostgreSQL because it works with ORMs like SQLAlchemy and Pony, and I’m comfortable writing some raw SQL in a pinch. But you can definitely use other databases/datastores. Sadly, you can’t write to file, because Heroku resets your app’s filesystem between programs.

The thing to understand about Heroku is that most of the documentation assumes you’re going to host a program that will be accessible via HTTP requests. So either a web app or a REST-ful API. This isn’t our case, though. So don’t worry about webservers or workers or anything like that. We’re going to summon just one worker for when we run our script.

Create an empty file named Procfile, which will tell Heroku explicitly to not try to start a server or map your app to any URLs.

I’m assuming you have Python 3.6 installed along with Pipenv. If not, Pipenv is a great tool that makes Python’s pip package management and virtual environments a lot more comfortable. Install all your libraries with pipenv install and then run pipenv lock so that Heroku will see your Pipfile. And if your project isn’t a git repo, make it one with

git init

git add .

git rm ANYTHING_WITH_YOUR_CREDENTIALS

git commit -m "Initial commit"

Using the Heroku CLI, login to Heroku and then runheroku create, and now your git repo will have a remote repo on Heroku’s servers, and your project will have a randomly generated name you can reach from your dashboard on Heroku.com. You can type in git push heroku master to upload or update your code, just like on Github.

You do not want to share your twitter API keys on a public Github (anybody could tweet as you or abuse twitter’s services in your name). I strongly encourage you to not store those in files, and if you have those files, list them in .gitignore.

The best practice on Heroku is to use “config variables” to hold your keys. These keys are accessible to your program as environmental variables. In Python, these are exposed with the os.environ object (import OS for it to work). So where you’d load your keys from a file, instead put those in this page:

And call them by their keys in python like os.environ[“TWITTER_OAUTH_SECRET”]. You can set them locally, too. Every operating system has its own flavor of environmental variables.

While you’re in Heroku’s dashboard for your app, click the “Resources” tab, and then the purple “Find more add-ons” button. Install an add-on called Heroku scheduler. You can see I also installed the free tier of the PostgreSQL service. Make sure you have whatever database you’d like set up, and read about how to connect to it safely from your app. Again, you don’t want to accidentally leak your keys on github!

Make sure everything is working locally and you’ve committed and pushed all your changes on the master branch to Heroku (git push heroku master ). Now, let’s try it out. Run the command heroku run bash, and you’re now inside a shell with python set up to match your Pipfile specifications. Try running your script here. Does it work? Great! If it doesn’t, now’s the time to start debugging. Heroku’s docs are pretty well written, and it’s popular enough to have lots of answers from other users online. If you have a database, initialize it now.

Now that it works, let’s got into the resources tab on the Heroku dashboard. Under add-ons, click on Heroku Scheduler. You’ll see a page like this:

Type in whatever command starts your script. In my case, I have the python interpreter start my main script. This is the same as anything you could run from your bash command line.

And, congratulations! Your bot has left the nest, and is out on its own making friends and getting the followers you yourself wish you could have attracted in life. You may find yourself living vicariously through its success, and perhaps you’ll git clone it home for a chat sometimes, where you then offer it new suggestions and features and bask in a warm contentment at a job well done.

--

--