(Ed note: this is a copy of a post to Pandorabots-general on July 22, 2005
in response to an inquiry about how to connect to the Pandorabots API
using Perl. The post contains both documentation and Perl code with
comments.)
This is an update to the code I originally
posted. I have fixed some errors,
made the parsing more exact, and included
catching of Pandora errors.
Perl and the Pandorabots API
Requirements:
Perl (naturally)
A published Pandorabot
The Perl module LWP::Useragent
First, take a look at the FAQ at Pandorabots:
http://www.pandorabots.com/botmaster/en/~110e53304f142ee955a2c~/faq
Using Perl and LWP Useragent, you can call the
Pandorabots API and parse the
response. This code could be used in a
subroutine, script, or another
module.
You can find Perl at:
http://activestate.com/Products/ActivePerl/?mp=1
The LWP::Useragent module can be found at
http://search.cpan.org
or installed through
the Programmer's Package
Manager included with the Activestate
distribution. Installing modules or
using PPM is beyond the scope of this document.
use LWP::UserAgent;
# Our user ID.
my $custid = "Test";
# Our message.
my $msg = "Hello";
# Lowercase and remove
spaces/underscores in user name.
$custid = lc($custid);
$custid =~ s/ //ig;
$custid =~ s/_//ig;
# Create a new useragent. Name your
agent whatever you like or
keep the default. Your bot's name is a good
suggestion.
$ua = new LWP::UserAgent;
$ua->agent("AgentName/1.0 " .
$ua->agent);
# Create a request. This sets up the
POST string.
# Field values sent two diff ways
shown here. You will need to
add your own bot's id for the botid= field.
my $req = new HTTP::Request POST =>
("http://www.pandorabots.com/pandora/talk-xml?botid=botidhere&input=$msg");
$req->content_type('application/x-www-form-urlencoded');
$req->content("custid=$custid");
# Pass request to the user agent and
get a response back
my $res = $ua->request($req);
# Create a variable to hold content.
Create a variable to hold
content. You now have the
# full source of the reply from the
API, including markup, user
input,
# and Pandora response code.
my $reply = $res->content;
# Get everything in between
tags, which is where
the bot's reply is.
($response) = $reply =~
m[(.*?)]isg;
# Trim string.
$response =~ s/^\s+//;
$response =~ s/\s+$//;
# Convert tags for quotes and links
in AIML.
$response =~ s/"/\"/g;
$response =~ s/<//g;
# Catch Pandora errors. Change
response to suit your needs.
$response = "?" if $response =~
/missing input/gi;
$response = "Please resend your
message." if $response
=~/Unknown error/gi;
$response = "Resend your message or
try again later." if
$response =~ /D timeout/gi;
$response = "Too much recursion. Try
asking a different way." if
$response =~ /Too much recursion in AIML/gi;
# $response now holds just the
bot's reply. Print or return
$response here based on your app.
And that's all it takes to call the Pandora API
in Perl. Not perfect, but
works nicely. Happy coding.
alienz
http://botworld.mazopolis.com
http://marzbot.marzoplis.com