Created
July 13, 2010 01:01
-
-
Save Ttech/473305 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class pandorabot { | |
public $botid; | |
public $_Pipe; // This will be our cURL handle in a bit | |
public $timout = 50; | |
public $default_responce = "Probably"; // Use the function to set this. | |
private $path_url = "http://www.pandorabots.com/pandora/talk?botid="; // So we can easily change url if needed | |
/* Sanity Checking Function */ | |
public function pandorabot($botid=""){ | |
/* Run all init and such now. */ | |
$botid = trim($botid); | |
if(isset($botid)){ | |
// Init Curl | |
curl_setopt($this->_Pipe, CURLOPT_URL, $this->path_url.$botid); | |
curl_setopt($this->_Pipe, CURLOPT_POST, 1); | |
curl_setopt($this->_Pipe, CURLOPT_RETURNTRANSFER, 1); | |
} | |
} | |
public function default_responce($responce=""){ | |
/* Check to see if $responce is set otherwise we return the default */ | |
if(isset($responce)){ | |
// Check to make sure new responce is actually there | |
if(!$this->sanitize($responce) == FALSE){ | |
$this->default_responce = $this->sanitize($responce); // Set Responce | |
} | |
} else { | |
// No new responce set, return the already set one. | |
return $this->default_responce; | |
} | |
} | |
public function say($user_input){ | |
$name = "input"; // Used to submit the form post | |
$input = $this->sanitize($user_input); | |
// Stupid debug stuff | |
echo $this->timeout."<br />"; | |
curl_setopt($this->_Pipe, CURLOPT_TIMEOUT, $this->timout); | |
curl_setopt ($this->_Pipe, CURLOPT_POSTFIELDS, "Name=$name&input=$input"); | |
curl_setopt ($this->_Pipe, CURLOPT_FOLLOWLOCATION, 1); | |
$reply = curl_exec($this->_Pipe); | |
if(isset($reply)){ | |
return $this->get_say($reply); | |
} // Something... | |
curl_close($this->_Pipe); | |
} | |
public function set_timeout($int){ | |
if(!is_int($int)){ | |
$this->timeout = 60; | |
return FALSE; | |
} else { | |
$this->timeout = $int; | |
return TRUE; | |
} | |
} | |
private function sanitize($string){ | |
$string = trim(str_replace("\n", "", stripslashes(html_entity_decode($string)))); | |
if(!empty($string)){ | |
return $string; | |
} else { // Nothing is returned, return false | |
return FALSE; | |
} | |
} | |
private function get_say($input, $tag='font'){ | |
// Do a little regex to get the bot reply | |
$pattern = "#<$tag color=\"\w+\">(.*?)</$tag>#"; | |
$var = preg_match($pattern, $input, $matches); | |
$result = $this->sanitize($matches[1]); // Get outout and send for validation | |
/* Simple Sanity Check - Null */ | |
if($result == FALSE OR empty($result)){ | |
return $this->default_responce(); | |
} else { | |
return $result; // Return valid string. | |
} | |
} | |
} | |
// Basic Example | |
$bot = new pandorabot("****"); // Botid | |
$bot->set_timeout(180); // 60 should be fine | |
echo $bot->say("hi"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment