Pandorabots To Flash (Actionscript Tutorial)

As promised this is a tutorial for building a Flash interface for a Pandorabots chatbot.
This is a portion of an unreleased game I was building where you are stuck inside of a giant maze. The maze is actually a machine. An artificial intelligence called “I”.
You can talk to it throughout your journey, and it engages with you in existential conversations, often taunting you. You can check out some screenshots, and more of that here.

It was intended to be part of BlueSuburbia. At present it’s slipped to the back burner.

The source files that are given with this tutorial are a completely built, fully done up, interface of the above… Preloaders, animations, and all.
I’ve simplified it, and the workings of it should be pretty self-explanatory.
It should suffice in starting you out on your bot mastering, programing, and creating adventures! :D

** DOWNLOAD SOURCE FILES HERE.
* ACTIONSCRIPT 3.0 VERSION
* ACTIONSCRIPT 2.0 VERSION

I have an extensive, and more informative, tutorial on setting up a Program E (php) bot here.
It is a lot more concise. I strongly recommend visiting that page as well.
It features a very thorough introduction into the world of chatbots. CLICK HERE.

To begin…

You will need to start an account with Pandorabots.
Create your bot starting either from scratch, or with an already available AIML set.
I prefer to use The Annotated A.L.I.C.E. AIML available here.
They are the simplest, and cleanest to start with.
After that is done go to the “My Pandorabots” page, then click on your bot’s name.
It will give you the URL where your bot is published at. Copy and paste the botid (the numbers after "botid="). You’ll need that later in the Flash code end.
Like so:

Basically, all that is necessary in the Flash-end are two text fields.

One set to DYNAMIC text for the bot’s responses, called:
output_txt
One set to INPUT for the user’s input, called:
input_txt

Everything else here is purely decorative treatment.

Following is the send/receive code.
Comments explaining what’s what are in-line:

function submitReply()
{
	//XML that is received, replies are received and parsed as XML
	var botReply:XML = new XML();
	//Load vars set up
	var botURL:URLRequest = new URLRequest("http://www.pandorabots.com/pandora/talk-xml");
	var botLoad:URLLoader = new URLLoader();
	var botSend:URLVariables = new URLVariables();
	botLoad.addEventListener(Event.COMPLETE, bot_onLoad);
	botLoad.addEventListener(IOErrorEvent.IO_ERROR, bot_onLoadError);
	
	//supports a unique custid that you can send, 
	//In this case we wont support that and let Pandorabots take care of it...
	//botSend.custid = String(loaderInfo.loaderURL);
	//The bot ID. This is given to you from Pandorabot
	botSend.botid = "94118f788e36cd0b";
	//The user's input to send to the bot
	botSend.input = input_txt.text;
	
	//send it over
	botURL.method = URLRequestMethod.POST;
	botURL.data = botSend;
	botLoad.load(botURL);
	
	function bot_onLoad(event:Event):void
	{
		//capture the reply (botReply.that)
		//and "write it out"
		botReply = XML(botLoad.data);
		writeText(botReply.that);
	}
	function bot_onLoadError(event:IOErrorEvent):void
	{
		writeText("There was an error in my system. Please contact: nathalie.lawhead@gmail.com");
	}
	input_txt.text = "";
}

The code for “writing out” the bot’s reply is as follows…

//TEXT:;
var writeInt:Timer = new Timer(50);
var writeCnt:Number = 0;
var writeStr:String = "";//the string to write out
//Write out text character by character
function write(event:TimerEvent)
{
	if (writeCnt< =writeStr.length)
	{
		output_txt.htmlText = writeStr.substr(0,writeCnt) + "_";
		output_txt.scrollH++;
		writeCnt +=  1;
	}
	else
	{
		//trace("Timer stopped.");
		writeInt.stop();
		writeInt.removeEventListener(TimerEvent.TIMER, write);
	}
}
//start writing - reset previous
function writeText(myText:String)
{
	writeStr = myText;
	output_txt.text = "";
	writeCnt = 0;
	writeInt.stop();
	writeInt.addEventListener(TimerEvent.TIMER, write);
	writeInt.start();
}

Update: An even better version of the above “writeText”…

//Write out text
function writeText(myText:String, myTextField:Object) {
	var i:Number = 0;
	function write() {
		if (i< =myText.length) {
			myTextField.htmlText = myText.substr(0, i)+"_";
			myTextField.scrollH++;
			i = i+1;
			trace("calling "+myTextField.name);
		} else {
			trace("ended");
			clearInterval(writeInt);
		}
	}
	clearInterval(writeInt);
	var writeInt:uint = setInterval(write, 40);
}
//Usage: writeText("Text to write out.", txt_test);

And that’s all that’s needed! Simple and sweet.

Here’s the end result (you’ll have to turn him on first!) :

[swfobj src=”http://nathalielawhead.com/sourcefiles/Pandorabots_Flash/pandorabots_I_AS3.swf” height=”500″ width=”800″]
Talk to him! Click here for fullscreen.