IRC Bot Example

Plaintext Version

<?php
require("classBot.php");//Bot Class
$loaded=false;//variables to be used in the functions that need to be global
///Functions the bot will perform\\\
function greet($bot)
{
	global $loaded;
	if(strpos($bot->LastMessage,"JOIN")===false){
		if($bot->LastSpeaker==$bot->BotNick && !$loaded){
			$bot->Output("PRIVMSG",$bot->IrcChan,$bot->BotNick." is now loaded.  Type !help for my commands.");
			$loaded=true;
		}
		else if($bot->LastSpeaker==$bot->BotNick)
			return false;
		else if($bot->LastSpeaker==$bot->AdminName)
			$bot->Output("PRIVMSG",$bot->IrcChan,"Ahhh, massa is here. I am to serve him without question. Massa is kind. Massa feeds me.");
		else
			$bot->Output("PRIVMSG",$bot->IrcChan, $bot->LastSpeaker.", welcome to the channel! Type !help for my commands.");
 
	}
}
function say($bot)
{
	$bot->Output("PRIVMSG",$bot->LastWhere, $bot->LastArgs);
}
function fml($bot)
{
	$url="http://api.betacie.com/view/random?key=readonly&language=en";
	$xml = simplexml_load_file(rawurlencode($url));
	if(empty($xml->items->item->text))
		$bot->Output("PRIVMSG",$bot->LastWhere,"Sorry, couldn't fetch FML, try again in a couple minutes.");
	else
		$bot->Output("PRIVMSG",$bot->LastWhere,$xml->items->item->text);
}
function help($bot)
{
	$bot->Output("NOTICE",$bot->LastSpeaker,"---------------------------------------------------------------------------");
	$bot->Output("NOTICE",$bot->LastSpeaker,"| User Commands"); 
	$bot->Output("NOTICE",$bot->LastSpeaker,"---------------------------------------------------------------------------");
	$bot->Output("NOTICE",$bot->LastSpeaker,"| !help Displays this message.");
	$bot->Output("NOTICE",$bot->LastSpeaker,"| !say <message> Tells to the bot to say \"message\".");
	$bot->Output("NOTICE",$bot->LastSpeaker,"| !fml Displays a random FML.");
	$bot->Output("NOTICE",$bot->LastSpeaker,"| !openticket **NOT YET FUNCTIONAL**");
	$bot->Output("NOTICE",$bot->LastSpeaker,"| !about Displays bot info.");
	$bot->Output("NOTICE",$bot->LastSpeaker,"---------------------------------------------------------------------------");
	if($bot->LastSpeaker==$bot->AdminName)
	{
		$bot->Output("NOTICE",$bot->LastSpeaker,"| Admin Commands ");  
		$bot->Output("NOTICE",$bot->LastSpeaker,"---------------------------------------------------------------------------");  
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @ident Identifies the bot with the server."); 
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @reg Registers the bot with the server.");
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @op user Gives user op status, if the bot is op itself.");  
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @kick user Kicks user, if the bot is op.");
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @kill user Disconnects user, if the bot has the privs."); 
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @topic New Topic Changes the current topic to New Topic.");  
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @nick NewNick Changes the bots nick to NewNick.");  
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @join #channel Tells the bot to join #channel."); 
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @part #channel Tells the bot to part #channel.");
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @rejoin Makes the bot quickly part the channel and rejoin.");  
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @quit Tells the bot to disconnect from the server.");
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @ctcp VERSION/USERINFO Runs a CTCP flood on the channel.");
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @flood message Floods the channel with \"message\", and bleep.");
		$bot->Output("NOTICE",$bot->LastSpeaker,"| @botify Tells the server it's a bot.");
		$bot->Output("NOTICE",$bot->LastSpeaker,"---------------------------------------------------------------------------");
	}
}
function quit($bot)
{
	if($bot->LastSpeaker==$bot->AdminName)
		$bot->BasicOutput("QUIT See ya's lataz");
}
function rejoin($bot)
{
	if($bot->LastSpeaker==$bot->AdminName)
	{
		$bot->Output("PRIVMSG",$bot->IrcChan,"Back in a jiffy!" ); 
		$bot->BasicOutput("PART ".$bot->IrcChan); 
		$bot->BasicOutput("JOIN ".$bot->IrcChan);
	}
}
function part($bot)
{
	if($bot->LastSpeaker==$bot->AdminName)
	{
		$bot->Output("PRIVMSG",$bot->LastArgs,"Bye!" ); 
		$bot->Output("PART",$bot->LastArgs,''); 
	}
}
function joinchan($bot)
{
	if($bot->LastSpeaker==$bot->AdminName)
		$bot->Output("JOIN",$bot->LastArgs,''); 
}
 
function openticket($bot)
{
	if($bot->LastArgs=='')
	{
		$bot->Output("PRIVMSG",$bot->LastSpeaker,"----------------------------------------------------");
		$bot->Output("PRIVMSG",$bot->LastSpeaker,"| To open a ticket you must format it a certain way");
		$bot->Output("PRIVMSG",$bot->LastSpeaker,"| !openticket <name::email::subject::message>");
		$bot->Output("PRIVMSG",$bot->LastSpeaker,"| You need to include the \"<\",\"::\",and \">\" otherwise it will cause an error");
		$bot->Output("PRIVMSG",$bot->LastSpeaker,"| You will be provided with a ticket ID and a weblink to");
		$bot->Output("PRIVMSG",$bot->LastSpeaker,"| follow up with the ticket.");
		$bot->Output("PRIVMSG",$bot->LastSpeaker,"----------------------------------------------------");
	}else{
		$url = "http://www.samplewebhost.com/WHMCS/includes/api.php"; # URL to WHMCS API file
		$postfields["action"] = "openticket"; 
		$postfields["clientid"] = "0";
		$postfields["lastname"] = "pyr0t3chnician";
		$postfields["deptid"] = "1";
		$postfields["subject"] = "API Ticket";
		$postfields["message"] = "This is a sample ticket opened by the API";
		$postfields["priority"] = "Low";
 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 100);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
		$data = curl_exec($ch);
		curl_close($ch);
 
		$data = explode(";",$data);
		foreach ($data AS $temp) {
		  $temp = explode("=",$temp);
		  $results[$temp[0]] = $temp[1];
		}
 
		if ($results["result"]=="success") {
		  echo $data;
				  # Result was OK!
		} else {
				  # An error occured
		  echo "The following error occured: ".$results["message"];
		}
	}
}
function laugh($bot)
{
	$random=rand(1,5);
	switch($random)
	{
		case 1:
			$string="HA!  That is funny!";
			break;
		case 2:
			$string="Lolz";
			break;
		case 3:
			$string="haha!";
			break;
		case 4:
			$string="Hilarious...";
			break;
		case 5:
			$string="Ha! Funny!";
			break;
	}
	$bot->Output("PRIVMSG",$bot->LastWhere,$string);
}
function about($bot)
{
	$bot->Output("NOTICE",$bot->LastSpeaker,"<>-----------------------------------------------<>");
	$bot->Output("NOTICE",$bot->LastSpeaker,"<> PHPBotClass v0.1 Programmed by pyr0t3chnician.");
	$bot->Output("NOTICE",$bot->LastSpeaker,"<> pyr0t3chnician[at]gmail.com");
	$bot->Output("NOTICE",$bot->LastSpeaker,"<> Report any bugs you have found");
	$bot->Output("NOTICE",$bot->LastSpeaker,"<>-----------------------------------------------<>");
}
 
///Create the bot and add the custom commands\\\
$bot=new bot;
$bot->AddEventCommand("JOIN","greet");
$bot->AddCommand("!say","say");
$bot->AddCommand("!fml","fml");
$bot->AddCommand("!help","help");
$bot->AddCommand("@quit","quit");
$bot->AddCommand("@rejoin","rejoin");
$bot->AddCommand("!openticket","openticket");
$bot->AddEventCommand("lol","laugh");
$bot->AddEventCommand("lmao","laugh");
$bot->AddEventCommand("haha","laugh");
$bot->AddEventCommand("LOL","laugh");
$bot->AddEventCommand("LMAO","laugh");
$bot->AddCommand("@part","part");
$bot->AddCommand("@join","joinchan");
$bot->AddCommand("!about","about");
$bot->BotNick="Pyr0sSampleBot";
//Connect\\
$bot->OpenConnection();
?>