Sunday, May 10, 2009

Crazy Chatting with Chatbot::ELIZA

Chatbot::Eliza is a perl module that implements the famous ELIZA program created by Joseph Weizenbaum.

Chatbot::Eliza has a object type structure, which enables it to hook up with any interface. A very trivial command line implementation of the bot would take up only four lines of code and it goes like this :

use Chatbot::Eliza;
$bot = new Chatbot::Eliza("Jack", "");

This will initiate a new bot talking with you on the command line. Our bot, Jack in this case, would begin with the classic question: "Please tell me whats bothering you?". And you know what happens next. The mindless ramblings of the bot.

When I first saw this module, the only interesting use I found of it is to hook it up with my gmail account. Since then, a lot of my contacts have talked with either Ms. Jill or Mr. Jack.

Connecting to GTalk with Perl is a trivial issue. It requires a few perl modules such as

  • IO::Socket::SSL (>=0.81 ?)

  • XML::Stream

  • Net::XMPP

  • Authen::SASL

Use the CPAN commandline shell to install them. Enter the command line shell by typing

perl -MCPAN -e shell

Now install the modules one by one using the command

install module_name

And of course you also need the Chatbot::Eliza module.
With all the requisite modules installed, you are ready to connect your bot with GTalk.
Here's the listing I used for the Eliza-gmail-chatter:

#!/usr/bin/perl -w

use strict;
use Net::XMPP;
use Chatbot::Eliza;

my $bot = new Chatbot::Eliza("Jill","");
my $message;
my $body;
my $JID;
my $userid;
my $username = "xxxxxxxxxxxxxxxx"; #Supply your gmail username here
my $password = "xxxxxxxxxxxxxxxx"; #Supply your gmail password here
my $resource = "ChatBot";

my $hostname = '';
my $port = 5222;
my $componentname = '';
my $connectiontype = 'tcpip';
my $tls = 1;

my $Connection = new Net::XMPP::Client();


my $status = $Connection->Connect(
hostname => $hostname, port => $port,
componentname => $componentname,
connectiontype => $connectiontype, tls => $tls);

if (!(defined($status))) {
print "ERROR: XMPP connection failed.\n";
print " ($!)\n";

# Change hostname
my $sid = $Connection->{SESSION}->{id};
$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;

# Authenticate
my @result = $Connection->AuthSend(
username => $username, password => $password,
resource => $resource);

if ($result[0] ne "ok") {
print "ERROR: Authorization failed: $result[0] - $result[1]\n";
$body = $bot->{initial}->[0];
$Connection->MessageSend(to=>"xxxxxxxxxxxx\",body=>$body); #Supply your contact's
#gmail id here
undef $userid;
while (1) {
if (defined ($userid)) {
$Connection->MessageSend (to=>"xxxxxxxxxxx\", body=>$body); #Supply your contact's
#gmail id here
print "Me(as the chatbot) (to $userid): ", $body, "\n";
sub doit
my ($SID, $Mess) = @_;
$JID = new Net::XMPP::JID($Mess->GetFrom());
$userid = $JID->GetUserID();
print $userid, " : " , $Mess->GetBody(), "\n";

You can have a laugh riot from your terminal watching your contact talking with the Chatbot. Make sure that you inform the contact later about who talked.

Chatbot::Eliza has a peculiar effect of making not wanting to talk with her ever again.

Try it for yourself.
Happy Chattin' Fun !

Labels: , ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home