My first Twitter bot

25 November 2014 »

Tags: coding raspberrypi twitter


Who says Twitter is just for humans?


A few months ago I picked up a Raspberry Pi (complete with awesome PiBow case and PiGlow LED board) and set out to learn some new programming tricks.

After weighing up my options, I decided to ditch the comfort of the desktop and dive straight in to Python. And since I like learning-by-doing, powering through Learn Python the Hard Way - along with the occasional visit to Stack Overflow - was my modus operandi.

And what have I got to show for all of this effort?

Goombot is a Twitter bot that scours Twitter for news headlines and mashes them up with a character from Super Mario Bros. Somewhat surprisingly, it’s also attracted over 1,000 followers.

Building a Twitter bot is fun, and a good project for learning Python, so I thought I’d jot a few tips down here in case you fancy trying something similar.

Things you’ll need

  • A computer that you’re happy to leave running 24/7 so that the bot can do its stuff. I’m using a Model B Raspberry Pi, and rather than power a monitor and keyboard I connect via SSH and use Screen to keep sessions running after I disconnect. Alternatively, you could host your bot in the cloud with a service like Heroku.
  • A Twitter account and set of Twitter API keys, which you get after registering a new app.
  • A bunch of Python libraries (unless you really fancy starting from scratch). I’m using Tweepy for accessing the Twitter API and NLTK for parsing and tokenising tweets.
  • Some logic. Some bots tweet when certain things happen, for example, while others attempt things like poetry or art. My bot is a little simpler: it remixes tweets by other people, and makes them sillier in the process.

Things that are optional for extra fun

  • A PiGlow LED module for your Raspberry Pi. I’ve set mine to show different colours depending on what my bot is doing.

Things to watch out for

  • Twitter screen names are not the same as IDs. Screen names start with @, IDs are numeric. Use GET users/show to get one from the other.
  • Protect your API keys if you don’t want other people using them! Keep your configuration data in a separate file and don’t publish it.
  • Automated text processing isn’t always easy to get right, so put some basic filtering in place if you’re worried about what your bot might end up tweeting.

Share this page

Like Tweet +1 Share