
AOL Instant Messenger isn't the most likely place
you'll need Amazon book data, but that doesn't mean the applications
aren't fun to connect. With Perl and the
Net::AIM
module, you can have your own chattering book-bot requesting Amazon
information for you.
What You Need
First you'll need the Net::AIM library, which provides all the
functions for logging into AIM and sending or receiving messages. You
can find it at activestate.com (http://aspn.activestate.com/ASPN/CodeDoc/Net-AIM/AIM.html).
To get a jumpstart on coding, check out the tutorials at Wired Bots
(http://www.wiredbots.com/tutorial.html). They
have some fully functional sample bots and lots of example code for
working with Net::AIM.
You'll also need an AIM screen name and password for your new virtual
assistant, along with a screen name for yourself if you don't have
one; sign up at http://www.aim.com.
Running the Hack
Start up AOL Instant Messenger and add the virtual screen name you
gave your bot to your buddy list. When you run
asin_bot.pl, you should see the bot appear among
your online buddies. Send a message consisting of only an ASIN, and
you should get the book title and Amazon price back. This
conversation is shown in .
Figure 1. Talking ASINs with an AIM bot
Not exactly stimulating conversation, but expanding its vocabulary is
simply a matter of adding Amazon requests and responses to
the script.
The CodeCreate a file called
asin_bot.pl
and include the following code. The code that communicates with
Amazon is based on a previous hack (), though the AWS request is
made inside the on_im subroutine, when a message
comes in. Instead of printing out to the console, it saves the
results in a variable, $detail, and sends it as an
instant message back to the person sending
the message. #!/usr/bin/perl
# asin_bot.pl
#
# An AIM bot that given an ASIN will
# return the product title and price.
# Usage: perl asin_bot.pl
use warnings;
use strict;
use Net::AIM;
use LWP::Simple;
use XML::Simple;
# fill in your relevants.
my $aim_un = 'insert AIM username';
my $aim_pw = 'insert AIM password';
my $dev_key = 'insert developer token';
my $af_code = 'insert affiliate tag';
# create an AIM connection
# and return it for usage.
my $aim = new Net::AIM;
$aim->newconn(Screenname=>$aim_un,Password=>$aim_pw)
or die "Cannot connect to AIM.";
my $conn = $aim->getconn();
# Set up a handler for messages.
$conn->set_handler('im_in', on_im);
$conn->set_handler('error', on_error);
print "Logged on to AIM!\n\n";
$aim->start;
# incoming.
sub on_im {
my ($aim, $evt, $from, $to) = @_;
my $args = $evt->args();
($from, my $friend, my $msg) = @$args;
# cheaply remote html.
$msg =~ s/<(.|\n)+?>//g;
# if this isn't an ASIN sized string,
# send back an error message stating such.
$aim->send_im($from, "I only accept ASINs.") unless length($msg) eq 10;
# create our final URL.
my $url = "http://xml.amazon.com/onca/xml3?t=$af_code".
"&dev-t=$dev_key&type=lite&f=xml&".
"AsinSearch=$msg";
my $content = get($url);
my $response = XMLin($content);
my $detail = $response->{Details}->{ProductName}||"no title";
$detail .= " $response->{Details}->{OurPrice}";
$aim->send_im($from, $detail);
}
# oops!
sub on_error {
my ($self, $evt) = @_;
my ($error, @stuff) = @{$evt->args()};
# Translate error number into English.
# then filter and print to STDERR.
my $errstr = $evt->trans($error);
$errstr =~ s/\$(\d+)/$stuff[$1]/ge;
print "ERROR: $errstr\n";
}
Notice that inside the on_im subroutine, the
script checks to make sure the incoming message is exactly 10
characters, the length of an ASIN. Otherwise it sends back the
message, "I only accept ASINs." It's a good idea to set up rules like
this for any kind of queries you allow. Bots should always send a
message about success or failure.
Showing messages 1 through 14 of 14.
-
HACK #95
2004-01-14 07:20:16
anonymous2
[View]
-
HACK #95
2004-01-15 12:03:29
anonymous2
[View]
-
It worked for me
2005-12-12 07:12:52
wireless18
[View]
-
Doesn't seem to work..
2006-03-29 10:43:04
vb7ue
[View]
-
Doesn't seem to work..
2006-03-29 22:57:46
RB-Napster
[View]
-
Doesn't seem to work..
2006-04-06 12:43:47
Deals
[View]
-
Doesn't seem to work..
2006-06-18 12:40:04
jessetechie
[View]
-
Hack
2004-01-15 22:45:57
anonymous2
[View]
-
Hack
2004-12-21 07:12:10
rancidlivings007
[View]
-
Hack
2005-08-30 15:07:31
Crushonu821
[View]
-
Hack
2005-12-05 04:36:29
hilton
[View]
-
Hack
2006-11-27 17:15:36
redsoxred12
[View]
-
Hack
2006-11-27 17:15:40
redsoxred12
[View]
-
Code Don't work
2005-10-26 14:05:07
jimn
[View]
|
Showing messages 1 through 14 of 14.
|
|
O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website:
| Customer Service:
| Book issues:
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
|
|