I’ve been using twitter for several years and one of the most fascinating uses I’ve seen is to build bot accounts that tweet on their own, like those created by Darius Kazemi (@tinysubversions) or Allison Parrish (@aparrish).
So I decided I wanted to make my own twitterbot. Some ideas sparkled in my mind, and some of them will eventually become a twitterbot I guess. However, I decided to start small and make a simple twitterbot to begin with.
After some research on the Twitter API, which had nourished a good ecosystem on its top (and then kind of attacked it), I found an open source library in Python to access the API that seemed simple and powerful: tweepy.
In order to make a twitterbot, there are some steps that have to be taken before start coding (or at least before making the twitterbot tweet):
- Register a new account on Twitter
- An email address is required
- Go to the developers site and create a new app to access the Twitter API
- Obtain the keys and access tokens needed to authorize the app against the API
Predicting I’ll create more than one twitterbot, I stored all this information in a spreadsheet: email address and password, twitter username and password, consumer key (API key), consumer secret (API secret), access token and access token secret. So for each twitterbot I’ll have a row storing all the needed information to use the API.
##Time to code! One of the most convenient things Tweepy provides is the authorization management:
After that, updating the twitterbot’s status on Twitter is very easy:
Having the foundation working, what’s left is to decide on what the twitterbot will tweet. I wanted to make the twitterbot tweet regularly, so I though on something suitable to tweet each hour, for example. The most direct thing was to build a clock bot. To spice things a little bit, and given that one of my planned twitterbots involves using emoji, I though to make a cuckoo clock with some little graphics topping :smile:. One of the things I discovered is that one can directly embed emoji symbols within the code:
So this code will compose a tweet showing an emoji clock face depending on the current hour, and then append as many “cuckoos” as needed. Simple, but enough to start :smile:.
In order to obtain the clock behavior, I just added a new entry to
crontab to run this script hourly at each o’clock.
If you notice the argument to this script, it’s a feature I built with a dual purpose:
- Be able to have a test account, or taking it further, disassociate a twitterbot behavior from an specific twitterbot account.
- Be able to publish the source code of the twitterbots, without having to publish their credentials.
What I’m passing as an argument to the twitterbot script is the alias (and for alias I use its Twitter username) of the twitterbot I want to tweet using the behavior described in the script, in this case, the cuckoo bot.
This means I’ve registered a Twitter username cuckoobot, which is the twitterbot, and I’ve stored its API access credentials in a pickled file using the username as the filename.
I’ve also built a convenience script to interactively store the credentials in a file, which I use locally for testing with a test twitterbot account, and in the server for each of the twitterbots accounts.
And also the corresponding script to load the credentials from the file and use them in the bot script:
Happy automatic tweeting!