Create a Twitter Bot With PHP, YOURLs and Cron

Ever since I’ve been on Twitter I’ve been fascinated by Twitter bots. You know, automated Twitter posting robots. There are all sorts of Twitter bots. Most of them are Twitter accounts with a single purpose that are run by some sort of programming. Most often they monitor Twitter’s RSS feed for specific keywords and either reply to or retweet them.

A lesser used (I think) kind of Twitter bot is the kind that will post specific links on a scheduled basis. A couple of weeks ago I came up with an idea for one of these and set out to create it.

The tools I used were PHP, YOURLs (to create a short URL for the links the bot would post), Abraham Williams’s PHP library for Twitter OAuth and a cron job. Here’s how to do it:

  1. Before you start coding you must register the bot as an app on Twitter. To do that go to http://dev.twitter.com/, log in using the Twitter account you’d like to use the bot from and register your app. After that’s done you’ll have proper OAuth keys to use to post to Twitter from your bot.
  2. For the sake of this example we’ll build a bot that will post a link to the day’s Dilbert comic strip. The URL for a given day’s Dilbert strip is as follows:
    http://www.dilbert.com/strips/YYYY-MM-DD/
    To adjust this URL to be able to work on any day simply do a little PHP date substitution and assign it to a variable as follows:
    $url = "http://www.dilbert.com/strips/".date("Y")."-".date("m")."-".date("d")."/";
  3. Now that you have a variable that holds the URL for the strip you’re ready to send it to YOURLs for shortening. (Different URL shortening services have different ways to use their APIs. If you’re not using YOURLs consult the documentation for the API of your service of choice. Continue at step 6 once you have your shotened URL.) First create the URL for the API call and assign it to a variable:
    $link = "http://yourdomain.com/yourls/yourls-api.php?signature=yoursignature&action=shorturl&format=xml&url=".urldecode($url);
    Replace “yourdomain.com” and “yoursignature” in the above code with the domain of your YOURLs installation and the signature for your YOURLs user name.
  4. Use CURL to initiate the API call and retrieve the resulting XML containing the short URL:
    $ch = curl_init($link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $reply = curl_exec($ch);
    curl_close($ch);
  5. Parse the XML returned by the above code, retrieve the short URL and assign it to a variable:
    $xml = new DOMDocument();
    $xml->loadXML($reply);
    $node = $xml->getElementsByTagName('shorturl')->item(0);
    $shorturl = $node->textContent;
  6. Create the body of the Tweet and assign it to a variable:
    $message = "Today's Dilbert (".date("F j, Y").") - ".$shorturl;
  7. Now it’s time to log into Twitter using OAuth and post the Tweet. Before doing this be sure to include the OAuth library in the script with a simple include statement:
    require_once('twitteroauth.php');
  8. Define the OAuth keys for the application. These keys were supplied by Twitter when you registered your app in step 1 (use your keys in place of the Xs).
    define("CONSUMER_KEY", "XXXXXXXXXXXXXXXXXXXXXX");
    define("CONSUMER_SECRET", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    define("OAUTH_TOKEN", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    define("OAUTH_SECRET", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  9. Log into Twitter:
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
    $content = $connection->get('account/verify_credentials');
  10. Post your Tweet:
    $connection->post('statuses/update', array('status' => $message));
  11. Upload your code and twitteroauth.php & OAuth.php from the PHP library for Twitter OAuth to a directory on your web space.

Your bot is coded and ready to run. You can try it by going to the URL for it and checking your Twitter feed for the result.

Since it’s coded in PHP you’ll need to set a cron job to run it at the time you’d like it to run. Consult the cron documentation for your web provider to learn how to do this.

If you want to see the bot created above in action then subscribe to my twitter feed and watch it at 8:30 AM Eastern time.

Tags: , , ,

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.