Sign In/My Account | View Cart  


AddThis Social Bookmark Button


Build Your Own AIM Answerbot
Pages: 1, 2

Build the Bot

With the database squared away, it was now time to set up the bot. The code resided in a file dubbed, and basically consists of a section which logs into the AIM service, waits for the on_im event to be triggered, and then uses Perl's DBI module to query the database and return a result set.


use warnings;
use strict;
use Net::OSCAR qw(:standard);
use DBI;

# Config
my $screenname = 'rtfmbot';
my $password   = 'secreto';
my $dbconn     = 'host=localhost dbname=docbot';
my $dbuser     = 'rtfmbot';
my $dbpasswd   = 'secreto'

my $oscar;
$oscar = Net::OSCAR->new();
$oscar->signon($screenname, $password);

    # do stuff here

sub im_in {
    my($oscar, $sender, $message, $is_away) = @_;
    print "[AWAY] " if $is_away;
    print "$sender: $message\n";

    # Some AIM clients send HTML, we need
    # to convert it to plain text 
    $message =~ s/<(.|\n)+?>//g;
    my $key  = $message;

    my $dbh  = DBI->connect("dbi:Pg:$dbconn","$dbuser","$dbpasswd);
    my $SQL  = "SELECT url FROM keylist WHERE keyword = ?";
    my $sth  = $dbh->prepare_cached($SQL);

    if ($sth->rows == 0)
        $oscar->send_im($sender,"nothing found :-(");
        my $url;
        while( $sth->fetch() )

See It in Action

At this point, things are ready to roll. You can fire up the bot from the command line simply by typing perl Once you do, it will log in to AIM and you can converse with it like anyone else on your buddy list (see Figure 2).

Thumbnail, click for full-size image.
Figure 2. The rtfmbot in action (click for full-size image).

Need some help with the syntax of the GRANT command? No problem. The new guy wants to know about using PostgreSQL on Windows? Okay. Need to do some research on PostgreSQL replication? Here are a few links you might want to look at.

Signing Off

You can probably see how such a bot might be helpful to your organization. On its face it could be a handy reference point for new employees, or serve as a quick reference for those a bit more seasoned. Looking deeper, the shell provided here could really connect to any database and retrieve any kind of information your company might want to transmit. Perhaps your docbot could take a keyword such as "holidays" and send back all of the official holidays for your company; that would be pretty handy stuff and none of it would take very much code to put together.

It is worth pointing out that this information goes across AOL's network, so you wouldn't want to send anything sensitive across it, but for regular information, or customer accessible information such as store hours, customer support phone numbers, or product information, it should work just fine.

(If you do need more privacy, running your own Jabber server can give you more control.)

One more link I found helpful is AOL's bot homepage. If you are thinking about building your own bot, I recommend the resources I used.

Robert Treat is a long-time user, developer, and advocate for free and open source software.

Return to