Code (2 of 2) for @MattsOffice

As promised in my earlier post and the follow up post, here is some sample code I’m using to respond to @MattsOffice mentions. With a version of this code and a Cron job I can respond to any number of specific commands.

This code only illustrates responding to one command, “light”, but could easily be extended as is done with @MattsOffice.

<?php
include_once('class.twitter.php');// http://code.google.com/p/php-twitter/

$ioBridgeJSONFeedURL = 'http://www.iobridge.com/api/feed/key=XXXXXXXXXXXXXXXXXXXXXX';
$buzzerOnURL = 'http://yourserver.com/iobridge-proxy.php?widget=buzzeron';
$buzzerOffURL = 'http://yourserver.com/iobridge-proxy.php?widget=buzzeroff';

$t = new twitter;
$t->username = 'twitter_username';
$t->password = 'twitter_password';

// Read the ID of the last reply
$myFile = "lastreply.txt";
$fileHandle = fopen($myFile, 'r');
$lastReplyID = fgets($fileHandle);
fclose($fileHandle);

// Get the latest unanwsered replies
$data = $t->getReplies('','',$lastReplyID);

$counter = 1;
if(!empty($data)){
    foreach($data as $tweet) {
        if (!empty($tweet->user->id)){

            // Check for valid command
            if(stripos($tweet->text, "light"){
                // retrieve data from ioBridge.com
                $curl_handle=curl_init();
                curl_setopt($curl_handle,CURLOPT_URL,$ioBridgeJSONFeedURL);
                curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
                curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
                $ioBridgeResultsObject = curl_exec($curl_handle);
                $ioBridgeResultsObject = json_decode($ioBridgeResultsObject);
                curl_close($curl_handle);

                $lightResults = $ioBridgeResultsObject->{'module'}->{'channels'}[2]->{'AnalogInput'};
                $updateText = ' @' . $tweet->user->screen_name . ' Ambient Light = ' . $lightResults;
            }
            // If no valid command was found reply with what commands are valid
            else{
                $messageText = ' Valid commands are: light, bright, dark, temp, hot, cold, lcd, and/or view';
                $updateText = ' @' . $tweet->user->screen_name . $messageText;
            }

            // Send update
            $updateData = $t->update($updateText, $tweet->id);

            // Update text file with ID of last reply (first reply of loop)
            if($counter === 1){
                $myFile = "lastreply.txt";
                $fileHandle = fopen($myFile, 'w') or die("can't open file");
                fwrite($fileHandle, $tweet->id);
                fclose($fileHandle);
            }
            $counter++;

            // Sound Buzzer (serves as an audible alert when someone pings @MattsOffice)
            $curl_handle=curl_init();
            curl_setopt($curl_handle,CURLOPT_URL,$buzzerOnURL);
            curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,5);
            curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
            $results = curl_exec($curl_handle);
            curl_setopt($curl_handle,CURLOPT_URL,$buzzerOffURL);
            $results = curl_exec($curl_handle);
            curl_close($curl_handle);
        }// if (!empty($tweet->user->id)){
    }// foreach($data as $tweet) {
}// if(!empty($data)){
else {
    echo 'All replies have been responded to.';
}

print '<pre>Twitter says(list of @mentions):'; print_r($data); print '</pre>';
print '<pre>Twitter says(response to status update):'; print_r($updateData); print '</pre>';
?>

In order to keep track of what @MattsOffice mentions have already been answered, the ID of the last successful reply is saved to a plain text file. A database could also be used but the text file method is much simpler even if it doesn’t allow logging.

In order for this code to work your server needs the ioBridge PHP Widge Control API and this Twitter library.

3 Responses to “Code (2 of 2) for @MattsOffice”

  1. [...] 07/17/09 Second post is up with [...]

  2. [...] 07/17/09 Second post is up with [...]

Leave a Reply