Tips for Twitter bots and automation

Who says Twitter is just for humans?

18 August 2019
Post Tweet Share

I’ve had a lot of fun over the years building Twitter bots and working with the Twitter API. It’s also been a great way to improve my Python! So I thought I’d jot down a few tips in case you fancy trying something similar.

Check out my analysis of Trump’s followers and fake followers in British politics for some examples of data analysis using the API, and @deepindex to see a simple Twitter bot in action.

Things you’ll need

  • A Twitter developer account and a set of API keys to access your app
  • 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
  • An idea for what you want to do (check out this list of Twitter bots for some inspiration)

Useful Python libraries

  • Tweepy for accessing the Twitter API
  • Pillow for image processing
  • NLTK for parsing and tokenising text
  • NumPy for data manipulation
  • Urllib for fetching URLs

Optional accessories

  • A PiGlow LED module for your Raspberry Pi. I’ve set mine to show different colours depending on what my bot is doing
  • An awesome PiBow case to keep it all in

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
  • It’s really important to protect your API keys. Keep your configuration data in a separate file to your application, and don’t publish it
  • Always follow the Twitter automation rules!
13 Sep 2019 →

Populist vs. Populist