Creating a Twitter bot is a nice exercise in programming because it touches upon many aspects: APIs, url / text parsing, automation (cronjobs), logging. Some time ago I created two Twitter bots:

  1. @newsafaribooks: tweets every day what new book get added to safaribooksonline.
  2. @cine_tv_es: tweets every hour what movies are broadcasted on Spanish TV. It matches/embeds the trailer for each movie.

To refresh my memory and share the knowledge I went through the motions again today creating another bot. For this post I created @quotes_ondesign, a bot that sends out design quotes retrieved from the Quotes on Design API. The code is on github if you want to check it out.

Steps:

  • Create a twitter account (or use your own, it depends your needs)

  • Add (confirm) a phone number. This is required to create a Twitter app, however the good news is that you can use your phone number for multiple accounts.

  • If you use my code copy config.py-example to config.py

  • Go to apps.twitter.com and create a new app, you need to put the following keys in config.py:

    • Consumer Key (API Key)
    • Consumer Secret (API Secret)
  • Create an access token (button) and paste these two keys in config.py as well:

    • Access Token
    • Access Token Secret
  • I use tweepy for Twitter API handling, so you need to: $ pip install tweepy

  • Some comments about the code:

    • I use requests to retrieve the data from the API
    • I parse the returned JSON to create the tweet. As tweets can be max. 140 chars I have some math going on in create_tweet(), when the quote text is too long I shorten it, appending 3 dots (…), I link to the source of the quote (quotesondesign.com).
    • Posting to Twitter is very simple with tweepy (api = get_twitter_api() and api.update_status(tweet))
    • As the API returns some encoded HTML (it seems it was build for web use), I unescape html for which I used HTMLParser
    • I use the logging module which is very easy to set up (logging.basicConfig…) and which gets you the imported module’s logging out-of-the-box.
    • I also played a bit with defining customized exceptions. The nice thing about trying this kind of exercise is that you learn a lot of Python related stuff.
    • Again the script is on github, any questions or comments, use the comments below.
  • The last step is to put the script in a cronjob to have it run every hour:

    0 * * * * $HOME/code/quotebot/quotes.py 2>&1 » botlog

    • 0 * * * * makes it run every hour
    • cron jobs tend to send email so I direct everything to a text file (and the logging bit of the scripts writes to $HOME/quotes.log)
    • if you installed tweepy as your user you might get an ImportError back from the cronjob. In that case you need to prepend it with the following (or wherever you have the Python installed you want to use):

      export PYTHONPATH=$HOME/bin/python/lib/python2.7/site-packages && $HOME/bin/python/bin/python2.7


And the result: auto-generated quote tweets (frequency is greater due to testing, but should post once a hour):

twitter bot results, nice

Use the comments below if you have coded a Twitter bot yourself that you want to share, or if you have any good ideas for other Twitter bots.


Bob Belderbos

I build useful apps and share my learning About me