Creating a Twitter Question/Revision Bot

I recently wanted to a CSV of questions and answers in to a Twitter account that tweeted questions followed by the correct answers every hour like so:

My CSV looks like this when opened in excel. There are 5 columns in each row, the first of which is the question, followed by a description of the answer, the answer itself and two incorrect answers. For this project I am not going to use the description of the answer in the second column, although I will be leaving it in for a different project.

The process is broken down in 4 parts with a few steps in each.

1. Setting up a *nix environment to handle it.

I wanted to do this on a Raspberry Pi because it is cheap to leave on all day and I wanted to do a bunch of other stuff with it. Also I can leave the Pi on and it can push tweets to the account 24/7. Unfortunately I don’t actually have a Raspberry Pi yet, I am doing this on OS X tho so the process is very similar and should be easy to follow. When I do get a Pi I will update the tutorial accordingly!

The only things I really need are Python and Cron, both of which are available on *nix systems. If you are using windows then you will have to find a way yourself to periodically run my script.

Python is already installed on MAC OS X and should be on Debian based systems. The script uses the Tweepy package which will not be installed and needs to be via:

pip install tweepy

2. Create a twitter APP

To use the Twitter API you need to create an application. I’ve found this to be a real pain recently as Twitter seems to be turning its back on small time developers having fun to concentrate on the big customers, I quite often get all sorts of unexplained errors while creating an app, these errors appear to be an attempt to crack down on small time developers having a good time. To combat this it seems best to use an account that has been active for a while and has a mobile phone number associated with it.

You need to sign in to https://apps.twitter.com/ and create a new app, the details you fill in when creating the account are not that important and you can leave the callback URL blank. Once you’ve created the application you will see a bunch of information about your application, you need to make note of 4 things: