- You want the process that runs the bot to start up, tweet something, and shut down, all on a schedule (via cron or scheduled tasks or some similar functionality of your OS) without your intervention.
- You want to control the text generation programmatically.
- You want to be notified if people are interacting with your bot's twitter account, via scraping the logs or writing your own hook functions.
- You don't want the bot to interact with anyone, or if you do, you're ok with making it interact only when it's running on it's schedule.
EasyTweeter was born out of my journey of implementing code to do this, and finding that it was long, tedious and boilerplate-heavy, wanting to be able to reuse it without reproducing the effort.
How to get it.
EasyTweeter is available on pip:
pip install EasyTweeter
I tend to put downloadable versions of my projects directly on this site, but given the ease of downloading a version on the above releases page, I'll direct you there instead of putting one here.
As an example of the library in use, let's reproduce a pretty popular twitter bot, the Fuck Every Word bot, which takes every word out of the dictionary, and puts the word fuck before it.
First, some setup. We need to tell the bot about our twitter credentials, which we do by making a directory next to your bot's script titled to your taste, and placing a file called credentials.ini inside it. Here's the contents of
[TwitterCredentials] ConsumerKey = asdfghjklasdfghjklasdfghj ConsumerSecret = asdfghjklasdfghjklasdfghjklasdfghjklasdfghjklasdfg AccessToken = 123456789123456789-asdfghjklasdfghjklasdfghjklasdf AccessTokenSecret = asdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl
Hopefully obviously, these aren't real credentials. You can get this information for your bot's account off of Twitter's application developer website.
Next, just for our example, we need a dictionary, so we know what words to fuck. We're going to have a file with all the words in the dictionary, one per line. This isn't something most people have at arms reach, but this is something that comes with many linux distributions, so a copy is only one google away.
So, each time the bot runs, we want it to pick the first word in the dictionary file, tweet it, and remove it from that file, so the next time it runs, it'll tweet the next word.
from EasyTweeter import EasyTweeter bot = EasyTweeter('fuck_every_word_redeux') try: with open('dictionary.txt') as dictionary: lines = dictionary.readlines() bot.tweet("fuck " + lines.pop(0)) with open('dictionary.txt', 'w') as dictionary: dictionary.writelines(lines) except Exception as e: bot.logger.exception('Exception caused bot to fail.') bot.checkForUpdates()
- We tell the bot about that directory we made by passing the directory name into the constructor of EasyTweeter. In addition to storing the credentials.ini file, the bot keeps some state in there, such as the favorites, retweets, etc which it knows about, as well as the logs.
- We load up the lines in the dictionary file into a list.
- We tweet out the first word in the list, removing it from the list in the process. When tweeting, this is also when authentication to Twitter occurs.
- We write out the list of words, now without the first entry, back into the file.
- If any errors happen in our "text generation" (which is likely more complicated in your real use) they'll show up in our bot's logs, giving us valuable debugging information, since, because this is running on a schedule, we likely won't know it has failed.
- We check for updates on our account. If anyone has interacted with the bot since the last time it's run, via following it, favoriting, retweeting, or replying to any of it's tweets, or direct messaging it, you'll see it in the logs on a line with
Hopefully you'll agree that's a pretty good ratio of code you care about to boilerplate.
And let's remember what's not here:
- No hard-coded credentials in your code.
- No (visible) authentication code.
- No (visible) code to keep track of what favorites, retweets, replies, etc we've seen already.
- No (visible) code to deal with rate limiting.
As updates are necessary, I occasionally post about this project on my blog, in the EasyTweeter tag. Check that out if you want to read all of them.