A P2P AIM Robot in Perl | 3
|
|
A P2P AIM Robot in Perl
Getting Interactive
Replying to messages
Not only can we send messages to AIM users, but we can also receive and respond to them. The on_im event is initialized when another AIM users sends a message to your AIM client. We can capture this information and send a reply.
#!/usr/bin/perl -w
use strict;
use Net::AIM;
my $nick = "motherofperl";
my $aim = new Net::AIM;
$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);
$self->send_im($nick, 'testing...');
}
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, "Hi $nick. You said: $args[2]");
}
The script above is identical except for two additional handlers. One to handle the error and one to handle on_im which is called when we receive a message. The event arguments are retrieved by calling the args method of the $event scalar, which returns an array. The message that was sent is contained in the third element of the array, which we send back to the sender in the on_im subroutine.
|
|
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/3.html
Information Technology Manager (MN)
Next Step Systems
US-MN-Minneapolis
SR UI DEVELOPER - JSF - Ajax - ICE Faces - Rich Faces
Next Step Systems
US-IL-Chicago
Technical Specialist – Pre-sales (MA)
Next Step Systems
US-MA-Littleton


