#!/usr/bin/perl ##------------------------------------------------------ ## amazon.pl ## ## Asterisk AGI script to accept a 10-character ISBN ## and return the current Amazon.com price for the ## book it represents, using the Amazon.com XML API ## to look up the price. ## ## Version 1.0, July 12, 2004 ## Written by Peter Rukavina, Reinvented Inc. ## ## Extended from the article "Create an Amazon AIM Bot" ## at http://hacks.oreilly.com/pub/h/479 ## ## You are free to copy, modify, etc. with credit. ## ## 1. Install this script in /var/lib/asterisk/agi-bin ## 2. Make it executable with: ## chmod +x amazon.pl ## 3. Add an extension to extensions.conf ## that calls the script, like: ## exten => 5,1,agi,amazon.pl ##------------------------------------------------------ use LWP::Simple; use XML::Simple; use Asterisk::AGI; my $AGI = new Asterisk::AGI; # Fill in your Amazon.com developer key and affiliate code my $dev_key = 'XXXXXXXXXXXXX'; my $af_code = 'XXXXXXXXXXXXX'; my %input = $AGI->ReadParse(); my $DONE = 0; # Ask for ISBN numbers until the script times out (i.e. caller hangs up) while (!$DONE) { # Get a 10-digit ISBN, with a 10,000ms timeout # There need to be a GSM sound file, called 'getisbn.gsm' # in /var/lib/asterisk/sounds that should say something # like 'Enter an ISBN; enter * for X'. my $isbn = $AGI->get_data("getisbn", "10000", "10"); # If we timeout, or enter nothing, then we're done if ($isbn eq "") { $DONE = 1; } else { # Convert the '*' to an 'X', for that's a valid part of an ISBN $isbn =~ s/\*/X/g; # Prepare a URL to send to Amazon.com my $url = "http://xml.amazon.com/onca/xml3?t=$af_code". "&dev-t=$dev_key&type=lite&f=xml&". "AsinSearch=$isbn"; # Send the query to Amazon.com, get a response my $content = get($url); my $response = XMLin($content); my $detail = $response->{Details}->{OurPrice}; # Remove the dollar sign from the price $detail =~ s/\$//g; # Split the price into dollars and cents ($before,$after) = split(/\./,$detail); # If the 'dollars' is more than zero, then we have a price... if (int($before) > 0) { # Say 'The cost of that item at Amazon.com is...' # Requires a GSM sound file, called costatamazon.gsm, # in /var/lib/asterisk/sounds $AGI->exec('Playback','costatamazon'); # Say the 'dollars' number like 'one hundred and thirty five' $AGI->exec('SayNumber',$before); # Say 'dollars', requires /var/lib/asterisk/sounds/digits/dollars.gsm $AGI->exec('Playback','digits/dollars'); # If there's a 'cents' part, say it too... if (int($after) > 0) { # Say the 'cents' number $AGI->exec('SayNumber',$after); # Say 'cents', requires /var/lib/asterisk/sounds/digits/cents.gsm $AGI->exec('Playback','digits/cents'); } } else { # Say 'Sorry that item wasn't found' (or something like that) # Requires a GSM sound file, called sorrynoitem.gsm, # in /var/lib/asterisk/sounds $AGI->exec('Playback','sorrynoitem'); } # Start over with a new ISBN (just making sure it's empty). $isbn = ""; } } # Say good-bye $AGI->stream_file('vm-goodbye'); # Hang up $AGI->hangup(); exit(0); ##------------------------------------------------------ ## The End ##------------------------------------------------------