Posts tagged bot

Developing more intelligent algorithms

0

First of all, what do we call intelligence when talking about algorithms? Well, if we have an algorithm that is able to forecast when it is going to rain and when it will be sunny, we might call that intelligent, but it depends on some other variable, the time and effort it took for it to get those results. Thus the faster and lower number of variables our algorithm needs to return the same or even better results, the more intelligent it is.
That is a rule of thumb, but how do we do that?

Well, many scientists say "Copy nature", and that is indeed a good solution, why? Because nature know how to do perfect algorithms. I'll take simple animals to explain this, for example the ants. If we were to program ants, we would say it is too hard, but are we sure?
How nature did them was by "programming" the simplest possible set of rules (Apart from the moving/eating/basic functions), which put together create the complex societies we see them live in.

Say we want to program ourselves an animal, and we want it to always be safe. Then, instead of programming complex recognition software, or advanced algorithms that determine the surroundings, why don't we try nature's approach, the simplistic approach?
Of course we need to build some "complex" systems, for understanding certain objects, lighting conditions, wet/dry states... Now, we give its algorithms a set of rules, to call them somehow:
First, don't run out of battery. So it has to learn how to plug itself, or any other charging method right?
Second, escape from moving objects.
Third, shade is safer.

Another requirement is that we give the robot the ability to store information, so it can learn what places are better and which ones are not. Now if we set it to "on" and watch what happens, the robot will cautiously start moving toward safer environments, and whenever battery runs lower, it will leave the safe place to look for a plug.

This sounds ok right? Now from this base we could start building better intelligence, but the base is this. We need something basic, some sort of default behavior.

If I have time I will build a java applet or something of the sort to test this out,

Best regards,
Alex

Creating an IRC bot in PHP from scratch

7

IRC Bot :DI came across several articles about this topic, and I decided to take a look at it, combine them, and actually have the bot work.

The way this bot works is it will open up a server, join an IRC chat channel and post a message, going into idle status after that, waiting to receive a ping in response. If you don't want to wait, once the message is posted you can cancel page loading and enter the page again (Not reload it, since that would send the message again), and you would have the message posted.
A good use for this bot (Although not recommended because of the high traffic) would be to send messages to hundreds of different IRC channels, or to have the program refresh the page every minute or so and send a message to certain IRC channels every x time.

We will create a new file, named "irc_bot.php" with the following function:

 
< ?php
function writeIRC($msg){
	// define your variables
	$host = "irc.dal.net";
	$port=6666;
	$nick="demoTester";
	$ident="DemoTester";
	$chan="#frih";
	$readbuffer="";
	$realname = "Demo Tester";
 
	// open a socket connection to the IRC server
	$fp = fsockopen($host, $port, $erno, $errstr, 30);
 
	// print the error if ther eis no connection
	if (!$fp) {
		echo $errstr." (".$errno.")\n";
	} else {
		// write data through the socket to join the channel
		fwrite($fp, "NICK ".$nick."\r\n");
		fwrite($fp, "USER ".$ident." ".$host." bla :".$realname."\r\n");
		fwrite($fp, "JOIN :".$chan."\r\n");
 
		// write data through the socket to print text to the channel
		fwrite($fp, "PRIVMSG ".$chan." :$msg\r\n");
 
		$timep = 0; //set up timer
		$timep = round(microtime(), 3); //start microtime
		// loop through each line to look for ping
		 while (!feof($fp)) {
 
			$line =  fgets($fp, 128);
			echo $line."\n";
 
			$line = explode(":ping ", $line);
 
			echo $line[0]."\n";
 
			if ($line[1]) {
 
				fwrite($fp, "PONG ".$line[1]."\r\n");
			}
			$time2 = round(microtime(), 3); //set up second timer
			$gen = $time2 - $timep; //find the difference
			if($gen > 30){
				//timeout - break loop
				echo '
 
Operation finished without receiving ping. Check out the stats iframe to see your message.
 
';
				break;
			}
		}
 
		fclose($fp);
	}
}
?>

Using it:

So now you should join a channel in the server you like, for this example I will be using one of frihost.com's IRC channels (Server: irc.dal.net Channel: #frih)

The way you would use this bot would be to send messages every set period of time for example, with loop functions, or to manage IRC channels, if you leave the last loop of code and you examine every single line posted on the channel.
You could also use it to warn users, or to post to several channels at the same time... The uses are varied, and here you have a base code to start from... :D

Have fun, and if you use it, post a link in the comments and I'll include it here in the article, as examples of usage.

Example of Bot working

Well I have created a simple page that will use POST to send data to the IRC server, and then I included the channel stats in an iframe below, so you can see the results live... ;) It's pretty cool, so try it, and if you wonder how it is done download the demo page.

  1. Go to demo page
  2. Download demo page (3kb)

For any questions/suggestions please comment

Go to Top