#!/usr/local/bin/perl -w use strict; use WWW::Mechanize; use Getopt::Long; use Data::Dumper; use HTML::Strip; use Text::Autoformat qw(autoformat); # Parse command line arguments and assign corresponding variables GetOptions ( 'q|question=s' => \( my $QUESTION = undef), 'f|format=s' => \( my $FORMAT = 'TEXT' ), 'v|verbose' => \( my $VERBOSE = 0 ), ); unless ( defined $QUESTION && $FORMAT =~ /^(text|html)$/i ) { print <> Asking START the question:\n" . ">> $QUESTION\n" if $VERBOSE; my $robot = new WWW::Mechanize; print ">> Fetching query form...\n" if $VERBOSE; $robot->get($URL); print ">> Submitting query...\n" if $VERBOSE; $robot->form_number('1'); $robot->set_fields('query' => $QUESTION); # ask a question $robot->click(); # Get the reply to my question print ">> Fetching answer...\n" if $VERBOSE; my $html = $robot->content(); # Extract the answer my ($text) = $html =~ /(

START(?:.|\n)*(?:
|line-rain.gif" width=100% height=3>))/m; if (!defined $text || $text =~ /^\s+$/) { $text = NoAnswer(); } if ($FORMAT eq 'TEXT') { # Reformat the text my $hs = HTML::Strip->new(); $text = $hs->parse( $text ); $text =~ s/>/>/g; # Quick and dirty fix $text =~ s/</1, right=>60, all=>1 }); } print "$text\n"; exit(0); sub NoAnswer { my @responses = ( 'is silent', 'looks puzzled', 'refuses to give an answer', 'shakes his head', 'gives no answer', 'could not understand the question', 'says: please try again', 'is currently off-line', 'Ur?', 'Which question?', 'Why?', 'Can you repeat the question again?', 'May I have your name please?', 'I am just a robot, what do you expect?', 'Please ask a different question', ); print Dumper(\@responses) if $VERBOSE; my $r = $responses[rand($#responses+1)]; $r = 'START ' . $r if $r =~ /^[a-z]/; $r =~ s/([^\?])$/$1./; return $r; }