Writing a simple Twitter-bot in PHP

OK kiddos, buckle up because today we're going to write a simple PHP Twitter-bot that:

  • Gets a list of a user's direct messages.
  • For each direct message, replies with the current date and time.

You're going to need a few things to get started:

  1. You'll need two Twitter accounts. One that you can send messages from and another to make API calls.
  2. A web server with PHP 5+ and cURL. I've done this on both CentOS 5 and XAMPP on Windows.

The Twitter REST API is very well written and simple to use. Since it's a RESTful API, all we're doing is using cURL to call various URLs. Retrieving data is a GET request while sending/deleting data is a POST request.

Step 1: Get a list of direct messages

~~~~ {.php name="code"} $username = "Your Twitter Username";$password = "Your Twitter Password";$url = "http://twitter.com/direct_messages.json";$httpReq = curl_init();curl_setopt($httpReq, CURLOPT_URL, $url);curl_setopt($httpReq, CURLOPT_RETURNTRANSFER, true);curl_setopt($httpReq, CURLOPT_USERPWD, $username . ':' . $password);$jsonret = curl_exec($httpReq);curl_close($httpReq);$data = json_decode($jsonret); wzxhzdk:0

With the above, it's trivial to iterate over the list of messages.

~~~~ {.php name="code"} foreach($data as $message) { echo 'Message from ' . $message->sender->screen_name . '';} wzxhzdk:1

The only difference here is that, since we're sending data to the API, we need to use POST instead of GET.

We also pass a couple of parameters with the request. The first is the Twitter username of the person we're sending the message to and the second is the message itself (remember, 140 characters max!). The return value is an encoded copy of the message you just sent.

We probably don't want to respond to the same messages over and over so let's delete each message when we're done.

~~~~ {.php name="code"} foreach($data as $message) { $username = "Your Twitter Username"; $password = "Your Twitter Password"; $url = 'http://twitter.com/direct_messages/destroy/' . $message->id . '.json'; $httpReq = curl_init(); curl_setopt($httpReq, CURLOPT_URL, $url); curl_setopt($httpReq, CURLOPT_RETURNTRANSFER, true); curl_setopt($httpReq, CURLOPT_USERPWD, $username . ':' . $password); curl_setopt($httpReq, CURLOPT_HTTPHEADER, array('Expect:')); curl_setopt($httpReq, CURLOPT_POST, true); $jsonret = curl_exec($httpReq); curl_close($httpReq); $data = json_decode($jsonret);} ~~~~

In this case, the return is a list of the remaining messages.

There are lots of Twitter libraries in different languages. However, as part of this exercise, I've written a couple of simple PHP classes.

  • TRequest.php is a base class for doing Twitter requests. The idea was to build specific types of requests on top of this class.
  • TDirectMessages.php is a simple class for handling direct messages and extends TRequest.

And here's the code for implementing a simple Twitter-bot like the one I described above.