Posted in software | 4 Comments »
Recently I wanted to set up an automatic tweet counting down the days until the end of 2010 to remind people to send holiday cards to Bradley Manning and members of the US Congress. Here’s the solution I used:
- Log into your Twitter account.
- Go to http://api.twitter.com/apps..
- Register a new application
- When you’re done registering, go to http://dev.twitter.com/apps and click on the application name.
- Copy and save your consumer key and consumer secret.
- Click “My access token” at right.
- Copy and save your access token and your access token secret.
- Download Abraham Williams’ twitteroauth package from github. (If you have any trouble later, download and use the 0.2.0-beta3 branch instead of the latest.)
- Unpack the archive and transfer the directory it contains to your web server’s public_html or whatever directory is set up as your document root.
- IMPORTANT: Change the name of the directory to something secret. Make up a string of random letters and numbers. We’ll call this string SECRETDIR.
- Go into SECRETDIR, edit config.php and insert the consumer key, consumer secret, access token and access secret.
- Go into SECRETDIR and create a “tweet.php” file, like this:
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$today = idate('z', time());
$enddate = idate('z', strtotime('31 December 2010'));
$daysleft = $enddate - $today;
$tweetstr = 'Only ' . $daysleft . ' day';
if ($daysleft > 1) $tweetstr .= 's';
$tweetstr .= " left to send out a holiday card for Bradley Manning's freedom! http://bit.ly/dW5w5e #freebrad";
$connection->post('statuses/update', array('status' => $tweetstr));
- Now create a cron job on your server. I’m tweeting once daily at 9:00am in my webhost’s time zone, so I used:
00 9 * * * wget http://www.bradleymanning.org/SECRETDIR/tweet.php
- Enjoy your auto-tweeting goodness!
Yes, using a SECRETDIR as a security (by obscurity) measure is not the best idea, but it does allow for a quick and dirty solution. Other solutions are possible, but this will get you started.