Making a Twitter Bot

Andy Zhang bio photo By Andy Zhang

Recently, I embarked on an endeavor to write my own Twitter bot.

Not that it was entirely of my own volition, though. The idea has been in the back of my mind for some time now, but now I have a class that requires spending 4 hours a week on an outside project, so I figured I might as well do something cool.

Note that there are sites that exist to do this already, such as Cheap Bots, Done Quick. However, I wanted to start completely from scratch and build my own framework.

The first step was figuring out how to access Twitter and tweet from outside the web. Luckily, Python has a highly extensible framework for working with the Twitter API, namely tweepy. Molly White has an excellent tutorial on starting a Twitter bot. Mostly, this involves creating a Twitter account, registering it as a developer account, and generating the access token and access token secret.

The code looks like this

import tweepy
from keys import *

auth = tweepy.OAuthHandler(consumerKey, consumerKeySecret)
auth.set_access_token(accessToken, accessTokenSecret)

api = tweepy.API(auth)

api.update_status('Hello World!')

Great! Now we can tweet from Python.

But how can we keep tweeting, even if the computer is off?…

I started looking at Heroku, but after fiddling around with it for over and hour, I gave up. I found a different cloud computing solution, namely in DigitalOcean. Even better, the GitHub education pack gives you $50 in DigitalOcean credit, which corresponds to 10 months of service!

After spending another hour setting up my remote server and getting cron scheduling to work, I published my first scheduled remote tweet!

You can follow the bot right now in development at @TechnoCoreUI.

Next time, I will cover developing natural text generation algorithms, starting with Markov chains.