Why would you want to code your own Twitter Bot?
There are so many websites that offer free Twitter bot services, with options to retweet a certain hashtag or mine certain Twitter data. Unfortunately, these ready made bots don’t typically allow for a lot of freedom or control. How many times have you seen a Twitter ReTweet Bot that tweets everything containing its designated #hashtag, including all of the distasteful Tweets that contain offensive language? Or bots ReTweeting completely irrelevant Tweets, though it contains the #hashtag, that really don’t need to be ReTweeted? These bots aren’t particularly designed well and can come across rather spammy. By programming a Twitter bot of your own, you can filter out the kinds of Tweets you don’t want, allowing you to Tweet, ReTweet and mine only the relevant Twitter data that you need. There are so many awesome marketing uses for a Twitter bot, and it all starts with learning what it is and how it works.
What is Twython?
To create a Twitter bot, you will need to create an app to connect with Twitter’s API. API stands for “Application Programming Interface”. The Twitter API is a designed way for external programs to interact with Twitter. It’s kind of like Twitter giving two people a tin can each attached by a long bit of string (but hopefully more effective). It gives both parties a way to communicate with each other – one party being us and the other of course being Twitter.
Twython is a pure Python wrapper for the Twitter API which means that you will have to download and install Python to get this thing running. This tutorial is going to assume that you’ve already installed Python and are familiar enough with it to at least be able to save and run programs.
There are a couple of other well known wrappers that do the same job, such as Tweepy or Python-Twitter, but it seems that Tweepy has long been abandoned by its owner and formed some kind of merge with Twython. Twython is currently accepted generally to be the best, as it is actively maintained and includes functions for everything you’re going to need. There’s decent community support as well with Twython which is great!
A working knowledge of the Python programming language is obviously recommended to do anything particularly original, but to be honest there’s so much support for Twython that just to make a simple Twitter bot, all you’ll really need is the Twython documentation, Google and a little bit of patience. And hopefully a splash of creativity.
You can download the Twython library on GitHub here. You will need to install the Twython packages too, but you can do this via pip or easy_install. Python should already have easy_install built in, in which case try:
or for pip:
I’m not sure why but authenticating seemed to be the biggest initial challenge for me. I had to regenerate my API keys and access tokens three times before they eventually got working. I think maybe my own impatience was to blame there though, as when I changed the API permissions it may have taken a little extra time after regenerating my keys maybe?
It seems that a lot of the tutorials or blog posts that I read online about authenticating access to the Twitter API are outdated, and written back when you only needed to enter your Twitter username and password to gain access to the API. You will need OAuth now, as it has been for a while.
On your Twitter homepage, you should see the footer items as pictured above. You can either navigate from Twitter’s Developers pages on the off-chance you bump into anything that interests you along the way. Or… you could just go straight to apps.twitter.com and hit that Create New App button!
If you’re still navigating via the Twitter Developers Page, you’ll get there eventually. I’ll wait for you.
The Application Form is pretty straight-forward. Twitter holds your hand through it enough, without me holding your other hand through it too. (Because then how would you type?!)
Yes, you agree! Create your Twitter application!
First thing you’re going to want to do is modify your app permissions so that you are able to Read, Write and Access direct messages. This will allow your Twitter bot to actually be fully functional.
On the API Keys tab you should see the:
- API key – This is your key to get in.
- API secret – This code right here has to be kept secret.
- Access level – This should now read “Read, Write and Access direct messages”.
- Owner – This should say your Twitter username.
- Owner ID – This number here is your Twitter ID. Not important to you.
And if you scroll down a bit, you want to click “Generate my access token”! You need this to make API requests on your Twitter account”s behalf. You should see:
- Access token
- Access token secret
- Access level
- Owner ID
Aside from the token and token secret, the last three points should match the previous Application Settings.
The reason that we need so many codes to get started is that, in order to be able to Tweet, ReTweet, Follow, Unfollow and all that good stuff via our Twitter bot, we need to identify ourselves securely to access Twitter’s API. The app key and secret are like our app’s unique username and password, different to our Twitter profile username and password as we can have multiple apps per user. The token and token secret are like the app’s credentials, usable only by the app, and determines the app’s level of access on behalf of the user account at the discretion of the OAuth provider. Basically, it’s a secret handshake with four moves. Twitter needs to know that you know all four moves, or you can’t be part of the Twitter API Cool Club.
While you’re here, if you really want, you can change your Application Icon or Organisation Details. But you shouldn’t need it for the sort of Twitter Bot you’re probably going to want to be building.
Testing Twython with a Twitter Status Update
Now try running your program and visit your Twitter profile. Has it appeared?
“But what good is this? I could have just updated my status myself!”
Next week I’ll be going through what kind of magic you can actually perform with your newly acquired wizardry!