Tuesday, June 17, 2014

How to Make Your Very Own DPLA Twitter Bot

When I saw that @HistoricalCats had launched as part of DPLA’s very nice App Library, I was a bit surprised that it wasn’t followed by a ton of other DPLA Twitter Bots. I had heard other people comment on the lack of a @HistoricalDogs twitter bot and thought that this would be a good project for me to try, especially since the examples in the @HistoricalCats source code pointed out the need for equal feline and canine representation. I thought I would write up my experiences adapting @HistoricalDogs for anyone else who needs a recipe to follow when adapting @HistoricalCats. Maybe we will soon see @HistoricalHats or @HistoricalBirds next!

Skills you need to do this:
I’m not a computer programmer, but I have some basic web administrative skills. I’ve installed mysql and php applications on a web server before. If you’re comfortable with doing some light editing (basically just copying and pasting) of php files it is easy to set up a twitter bot with the @HistoricalCats code that is provided. I did it in an hour or so one evening after work.

Things you need:

  • Web server space
  • A text editor
  • FTP program
  • Knowledge of how to set up cron jobs (optional)
First step: Get accounts and keys
Second step: Get all the files!

The Code for Historical Cats is available on GitHub (https://github.com/malantonio/HistoricalCats), just download the master zip file and open it.
Wow, that was fast and easy!

Third step: Modify the files for your new twitter bot and upload to your server space

Follow the instructions (https://github.com/malantonio/HistoricalCats) for modifying the historicalcat.php file and the keys.config.php files, adding in your own DPLA API key and Twitter consumer key, secret, access token, and access secret. If you look at the source code, you can see the cat phrases and the search terms that you can change to whatever you wish.


These are the only areas that you might need to modify in order to get your bot up and working. If you chose to, you can set up a sql database that will save your tweets and prevent tweet duplication. I didn’t set this up as part of my process, but if you have access to phpMyAdmin or something similar on your server, you can easily import the provided HistoricalCat.sql structure to a new database on your server. If you are using the database option when installing the code, you will also need to put this information into your config file.

Now you’re ready to upload the files! You should ftp these files up to your web server space. Note that you need to upload the twitteroath folder as well as historicalcat.php, config.php, and tweetcat.php to make everything run.


Visit http://yourwebsite.org/tweetcat.php to generate your first automatic tweet! If you don’t see any error messages you will see a tweet appear on your new twitter bot account. I didn’t have any problems getting this set up, except I needed to slightly adjust the filepath for twitteroath due to the way I uploaded my files. If you get error messages, they will specify the line of code in the file where the problem is. Most text editors will let you zero in on the line number where the problem is so you can do some troubleshooting.

Now, you could generate tweets automatically by just pointing your web browser at tweetcat.php whenever you want a post to appear, but if you want your twitter bot to run automatically, you will need to set up a cron job, so the page is just automatically visited at whatever times you want your tweets to be posted. Your web host may have some additional tools you can use to easily create a cron job for your new twitter bot.

Happy tweeting!!!!

Thanks very much to Adam Malantonio for creating the @HistoricalCats code, and for giving me feedback on this blog post.