Code:
<?php
/**
* wrapper of imified (http://bot.im) API to ease bot development process
* @author Hasin Hayder [http://hasin.wordpress.com]
* @copyright BSD Licence
* @example check out example.php
* @link http://hasin.wordpress.com
* @
*
*/
class ImifiedHelper
{
public $botkey;
public $botownername;
public $botownerpwd;
function __construct($botkey, $botownername="",$botownerpwd = "")
{
$this->botkey= $botkey;
$this->botownername = $botownername;
$this->botownerpwd = $botownerpwd;
}
/**
* get information of a specific bot user using their corresponding user key
*
* @param string $userKey
*/
function getUserInfo($userKey)
{
if(!isset($this->botkey))
throw new Exception("You must supply your bot key to use this function [getUserInfo]");
else if(!isset($this->botownername))
throw new Exception("You must supply your bot account user name to use this function [getUserInfo]");
else if(!isset($this->botownerpwd))
throw new Exception("You must supply your bot account password to use this function [getUserInfo]");
if(!isset($userKey))
throw new Exception("You must supply a user to find info");
$url = 'http://www.imified.com/api/bot/';
$data = array(
'botkey' => $this->botkey,
'apimethod' => 'getuser',
'userkey' => $userKey,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->botownername}:{$this->botownerpwd}");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
$xmlResponse = curl_exec($ch);
curl_close($ch);
$sxml = simplexml_load_string($xmlResponse);
if($sxml['stat']=="ok")
{
$userInfo = array();
$userInfo['status']=$sxml->user->status."";
$userInfo['userkey']=$userKey."";
$userInfo['screenname']=$sxml->user->screenname."";
$userInfo['network']=$sxml->user->network."";
$userInfo['created']=$sxml->user->created."";
$userInfo['lastonline']=$sxml->user->lastonline."";
$userInfo['lastcall']=$sxml->user->lastcall."";
return $userInfo;
}
else if($sxml['stat']=="fail"){
//error, so throw an exeption
$errorMessage = $sxml->err['msg']."";
throw new Exception($errorMessage);
}
}
/**
* send an instant message to a group of user[s]
*
* @param array $userKey
*/
function sendMessage($message, $userKeys)
{
if(!isset($this->botkey))
throw new Exception("You must supply your bot key to use this function [getUserInfo]");
else if(!isset($this->botownername))
throw new Exception("You must supply your bot account user name to use this function [getUserInfo]");
else if(!isset($this->botownerpwd))
throw new Exception("You must supply your bot account password to use this function [getUserInfo]");
if(!isset($userKeys))
throw new Exception("You must supply a user or an array of users as recepient of your IM");
if(!isset($message))
throw new Exception("Message couldn't be blank");
echo "sending";
if(!is_array($userKeys))
$users = $userKeys;
else
$users = join(",",$userKeys);
$url = 'http://www.imified.com/api/bot/';
$data = array(
'botkey' => $this->botkey,
'apimethod' => 'send',
'userkey' => $users,
'msg' => $message,
);
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->botownername}:{$this->botownerpwd}");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
$xmlResponse = curl_exec($ch);
print_r($xmlResponse);
curl_close($ch);
}
catch(Exception $e)
{
print_r($e);
}
$sxml = simplexml_load_string($xmlResponse);
print_r($sxml);
if($sxml['stat']=="ok")
{
return true;
}
else if($sxml['stat']=="fail"){
//error, so throw an exeption
$errorMessage = $sxml->err['msg']."";
throw new Exception($errorMessage);
}
}
/**
* reset the step variable;
*
*/
function resetStep($stepId="")
{
if(!isset($stepId))
echo "<reset>";
else
echo "<goto step={$stepId}>";
}
/**
* for easier processing, use setCallback to invoke a function with four parameters - message, stepId, network,userKey
*
* @param unknown_type $callback
* @example a callback function must be of this format function cb($messsage,$stepId, $network, $userKey){}
*/
function setCallback($callback)
{
if(!isset($callback))
throw new Exception("callback couldn't be blank");
$message = $_REQUEST['msg'];
$step = $_REQUEST['step'];
$network = $_REQUEST['network'];
$userKey = $_REQUEST['userkey'];
$callback($message,$step,$network,$userKey);
}
}
?> |