A spectre is haunting Twitter — the spectre of bots.

As well as doing some online learning, I want to keep myself busy with little practical projects that have some kind of purpose, even if they aren’t useful. My mischievous side liked the idea of making a twitter bot and after a little bit of googling I found that this was something that wasn’t too hard to achieve, even for a novice like myself.

I used this tutorial which is incredibly thorough. The first few steps are fairly obvious, make a twitter account and because the tutorial uses the python language, install that too. The next step is to give developer access to your account, to do this you make a new twitter app. Then you obtain the keys and access tokens for that app and save them in some way because we will need them later. These are what allows your script to log in remotely to the account and post to it’s heart’s content.

After this, we need to download and install a python wrapper for the Twitter API, this allows python to access the Twitter API and execute the script we are about to write. This tutorial recommends using Python Twitter Tools by @sixohsix. After running a few setup programs its almost time to start writing the script. Before that, we need to install the MarkovBot library as this is the basis for the Twitter bot we are building. The libraries in Python are similar to those in C, in that they contain pre-made routines. After the Markov library has been installed the script needs feeding a lot of data so it can create a Markov chain N.B. I don’t really understand what a Markov chain is, something to do with the previous state of something determining the probability of future events, probability mathematics not being one of my strong points.

To get a lot of data into the bot, you tell it to read a book or two, literally. I chose Karl Marx’s and Friedrich Engel’s ‘The Communist Manifesto’. Once you have a plain text version of your book in the folder that contains all the bits for the bot we can finally start writing the script.

The script isn’t too complicated and Python seems to be a fairly easy language to get to grips with and not too dissimilar to C. In fact it seems at first glance to be a bit easier to write with than C, I’m not seeing many semi-colons or curly braces. I wont go into the specifics of the script because it is explained so well in the tutorial. I’m also going to upload the code to my GitHub so I can show the options I chose and the subtle changes I made.

The first problem I faced with the code is that the script would simply terminate straight away and the bot wouldn’t actually tweet anything. After searching through the comments on the tutorial I found that you have to use the sleep command to make the bot run in the background. This can be seen in lines 31 and 32 of my code.

The next problem I faced wasn’t really to do with the code, it was to do with Twitter. Unfortunately I was a little bit over zealous with the bot and because its keyword was so general, it was sending out far too many tweets, Twitter considered it spam and so took away the app’s write permission.


“Behind the scenes at Twitter HQ”

I suppose this is good in a way because it stops people from turning your account into a spambot but in this instance, I WANT TO BE A BOT! To hopefully remedy this I made a simple change to my code. Instead of searching for the target string ‘Communism’, the bot now looks for ‘#KarlMarx’. This hashtag turns up in twitter far less than the word ‘Communism’ so I should have very little risk of the bot spamming the heck out of people.

At the time of writing the new version of the bot has been live for about 20 minutes and so far has found two tweets that contained the new target string. Hopefully twitter won’t kill it off this time. At some point in the future I will probably end up getting a Raspberry Pi or similar and run the bot from that so I can have it running 24/7 and so I can actually turn off my laptop.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s