Amazon Hacks Free Open Book

Amazon Hacks

Previous Section Next Section

Hack 95 Create an Amazon AIM Bot

figs/expert.giffigs/hack95.gif

Chat with some Perl code to get book prices via AOL Instant Messenger.

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.

95.1 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.

95.2 The Code

Create a file called asin_bot.pl and include the following code. The code that communicates with Amazon is based on a previous hack ([Hack #80]), 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.

95.3 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 6-9.

Figure 6-9. Talking ASINs with an AIM bot
figs/amzh_0609.gif

Not exactly stimulating conversation, but expanding its vocabulary is simply a matter of adding Amazon requests and responses to the script.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Credits
    Foreword
    Preface
    Chapter 1. Browsing and Searching
    Chapter 2. Controlling Your Information
    Chapter 3. Participating in the Amazon Community
    Chapter 4. Selling Through Amazon
    Chapter 5. Associates Program
    Chapter 6. Amazon Web Services
    6.1 Hacks #76-100
    6.2 What Are Web Services?
    6.3 Why Expose an API?
    6.4 What You Need
    6.5 What You Can Do
    6.6 Making Requests
    6.7 The RESTful Way: XML/HTTP
    6.8 SOAP Web Services
    6.9 Working With Responses
    Hack 76 View XML Responses in a Browser
    Hack 77 Embed Product Details into a Web Page with PHP
    Hack 78 Program AWS with PHP
    Hack 79 Program AWS with Python
    Hack 80 Program AWS with Perl
    Hack 81 Loop Around the 10-Result Limit
    Hack 82 Program XML/HTTP with VBScript
    Hack 83 Transform AWS Results to HTML with XSLT
    Hack 84 Work Around Products Without Images
    Hack 85 Syndicate a List of Books with RSS
    Hack 86 Import Data Directly into Excel
    Hack 87 Program AWS with SOAP and VB.NET
    Hack 88 Program AWS with SOAP::Lite and Perl
    Hack 89 Program AWS with NuSOAP and PHP
    Hack 90 Create a Wireless Wish List
    Hack 91 Make Product Titles Shorter
    Hack 92 Encode Text for URLs
    Hack 93 Cache Amazon Images Locally
    Hack 94 Cache AWS Responses Locally
    Hack 95 Create an Amazon AIM Bot
    Hack 96 Compare International Sales
    Hack 97 Program AWS with Mozilla
    Hack 98 Search or Browse Amazon with Watson
    Hack 99 Add Cover Art to Your Digital Music Collection
    Hack 100 Using All Consuming's SOAP and REST Interfaces
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele