A P2P AIM Robot in Perl | 4 | WebReference

A P2P AIM Robot in Perl | 4

A P2P AIM Robot in Perl

The AIM Eliza Chatter-bot

The IM Rogerian Psychotherapist

What could be a cooler example than an Eliza AIM bot? If you didn't already know, Eliza was written by Joseph Weizenbaum and described in the Communications of the ACM in 1966. It interacts with a user by answering with questions. For this example, I utilized the Chatbot::Eliza module which is freely available on CPAN.

#!/usr/bin/perl -w
use strict;
use Net::AIM;
use Chatbot::Eliza;
   
my $nick  = "motherofperl";
my $aim   = new Net::AIM;
my $mybot = new Chatbot::Eliza;
$aim->debug(1);
my $conn = $aim->newconn(Screenname   => 'motherofperlbot',
			 Password     => 'motherofperl')
    or die "Can't connect to AIM server.\n";
$conn->add_handler('config',    \&on_config);
$conn->add_handler('im_in',    \&on_im);
$conn->add_handler('error',    \&on_error);
$aim->start;
sub on_config {
    my ($self, $event) = @_;
    my ($str) = $event->args;
    $self->set_config($str);
}
sub on_error {
	my ($self, $event) = @_;
	my $error;
	my @stuff;
	($error, @stuff) = $event->args;
	my $errstr = $event->trans($error);
	$errstr =~ s/\$(\d+)/$stuff[$1]/ge;
	print STDERR "ERROR: $errstr\n";
}
sub on_im {
    my ($self, $event) = @_;
    my ($nick) = $event->from;
    print $event->dump;
    my @args = $event->args;
    $self->send_im($nick, $mybot->transform($args[2]));
}

The script is very similar to the last, except for the message that we're sending back to the remote AIM user in the on_im subroutine. If you'll notice, we're calling the transform method of the Chatbot::Eliza module, which processes the incoming message and returns the Eliza response, which is sent back to the remote AIM user. So with this script, an AIM user can carry on an interactive conversation with your AIM Perl bot. Cool yes?

But, just like a bad infomercial, there's more!


http://www.internet.com

Produced by Jonathan Eisenzopf
All Rights Reserved. Legal Notices.
Created: September 28, 2000
Revised: September 28, 2000

URL: http://www.webreference.com/perl/tutorial/13/4.html