Programe E ALICE AIML Bot Using Flash (PHP & Actionscript Walkthrough + Source Files)

System requirements:
*PHP (4.0.4 and up)
*MySQL
*Apache Server
*Flash
(If you found this via a google search… Source files for the Flash are at the end of the post…)

Short Summary:
Update: For a tutorial, and source code, on integrating Pandorabots with Flash, click here.
This is a concise collection of all the information that’s out there (as far as my research leads me to believe – since I last checked), + some personal trial-error-experimeddling on installing Program’s to host your own ALICE bot.
This also walks you through the basics for building your own Flash interface (for your bot).
My goal was to make a tutorial for people that don’t want to use pandorabots.
I hope to build on this in the near future.

Extended Summary:
I love playing around with bots, and especially love lurking on lists like Robitron. Projects like ChatScript & RiveScript are excellent ways of tickling the inner geek and keeping you busy in all your free time…
Of course, this completely devastates your social life, but hell… Who needs friends when you can MAKE them. Ha! Pun intended!
Chatbots in games is something that I’m particularly passionate about, and sell everyone on. I’ve successfully incorporated them into a number of my client’s projects. They’ve fit beautifully into these every-time.
Naturally, if you want to see them in action visit Tetrageddon Arcade, or chat with Minibyte on the front page of AlienMelon

This is going to cover a means of making a Flash based ALICE AIML bot that you can host without pandorabots.
You’ll need a host provider like MediaTemple where you have basic control over your own database, ect…
Now, I know that AIML is not necessarily “the best” solution out there.
There are fucktoneries of technologies/experiments, like Jabberwacky, VoiceXML, ELIZA (of course), IBM’s Watson, the list goes on and onIn terms of authoring, AIML is difficult to look at, BUT it makes the most effective and believable personality I know of… Especially if you don’t have years to develop your own personality.
I turn to it because of all the AIML sets that are so freely available out there, allowing you to quickly and easily customize your own personality… Not because it is necessarily “better”, but because there are so many sets available, you don’t need to spend years on creating something that is somewhat believable.
I terms of “building from the ground up”, I notice users still pick up on the “fakeness” of other bots too easily…
Nevertheless, picking your own solution is for you to decide, besides this is a post about building and hosting your own Flash ALICE bot!
Again, like I said, if you want to see a Flash front-end and AIML back-end in full frontal wackiness (crazy little character) then check out these two examples:
Alienmelon (minibyte, the most popular)
They Came From My Inbox (talk to the spam via the text input field at the bottom of the menu)
So…

INSTALLING THE BOT

Head on down to
http://www.alicebot.org/downloads/programs.html
And grab yourself a copy of Programe E… That’s their PHP version.
Notice that there are a plethora of other funky awesome implementations there, and that makes things all the more exciting!

Program E is a platform for running artificial intelligence robots written in PHP. It runs bots written in AIML (Artificial Intelligence Markup Language). Custom tags are a feature of AIML engines such as Programe E and it uses MySQL for its database bits and pieces.
You’ll have to know a little thing or two about running sql scripts, which is not hard!
In fact, it can be a lot of fun!
Note that AIML is really easy to understand if you know XML. It basically is a funked up version of XML.
A noteworthy collection of AIML sets are the Annotated A.L.I.C.E. AIML files found here:
http://www.alicebot.org/aiml/aaa/
On the other hand, if you are crazy enough to start from complete scratch I suggest investigating other solutions.

You’ll also need a noteworthy AIML editor. For most people I suggest just heading down to Pandorabots and using that as a development environment.
They already have something like an “editor” there where you can keep testing & uploading.
Go to “Create a Pandorabot“, there you can also select whatever set you wish…
Create it and then go to “AIML”. You can download sets there as well.
You can do your testing and personality forging there, and then upload the sets to your own server. It’s one good way of having a reliable environment.
Another great desktop editor is the GAITObot AIML editor (Windows only, MAC users must use bootcamp):
http://www.gaitobot.de/gaitobot/AIMLEditor/download.aspx
It’s another good starting point for learning.
There is also SAE (Simple AIML Editor). It’s Free by Riot Software
AND AI Dealer
Pick your point of entry!

Setting up a local testing environment is also something I highly recommend for safety reasons…
I suggest MAMP for that:
http://www.mamp.info/en/index.html
It’s for godless MAC users like me.
It’ll simulate Apache, MySQL, and PHP environments.
It works charms and keeps you from making killer mistakes that’ll down all your sites because you’ve accidentally failed to terminate a loop on line 34 and it overloaded the server… or some freak shit like that…
Whatever!
Don’t judge me.
For setting up MAMP wordpress.org has a wonderful walk through:
Installing WordPress Locally On Your Mac With MAMP
And here is another good walkthrough:
Setting Up Your Mac For Local Development Using Coda WordPress Mamp

Either way. This is LOTS OF FUN and IS NOT HARD. :D
If I can do it, a monkey can!

So! After you’ve got all your settings upped, safeties strapped, balls secured, and your copy of Program E downloaded…

1) Create a directory (on your server) called “programe“. Here is where you’ll install your little robot friend.
Unzip and upload all the Program E files into this directory.

Unzip in Program E

2) Create a database for the program to use in MySQL. Call it something like “programe“.

3) Here’s the fun part. In the Program E install you downloaded navigate to:
sql > mysql > db.sql
Open that up. You’ll need to run that script.
If you have phpMyAdmin, then do the following…
Hit the SQL tab at the top:

The SQL Tab...

Paste the contents of “db.sql” into the “RUN SQL query / queries on database

Run SQL Query

And hit “go“!

Booya! You got bot.

U got bot

4) By default, AIML files and startup.xml are in the aiml directory. Use the startup.xml file included in the distribution.
DO NOT use the startup.xml you get with AIML files from alicebot.org. AIML files that work well with Program E can be found at http://sourceforge.net/project/showfiles.php?group_id=43190
Once you’ve got your AIML sets unzip and upload all the files to the AIML directory.
Delete “std-65-percent.aiml” & “std-pickup.aiml“.
You don’t need those.

u dont need

Booya! Bot got brain.

5) In the “aiml” directory there is a “startup.xml“:

aiml startup.xml

Edit that to fit your bot’s personality. You can also do this later, but this is an important 1st step in forging your own bot.
Note that this is like the Pandorabots “properties“:

pandorabots properties

Same thing… It’s basically predefined character traits (values) that you continuously refer to in the AIML files.

6) Go to “/src/admin/dbprefs.php“, and edit dbprefs.php with your database information and aiml directory information.
If you have AIML files in the default directory you don’t have to change that. See dbprefs.php for other options that can be configured.
The values you need to edit are:
$DB_HOST (database host name)
$DB_UNAME (database username)
$DB_PWORD (password)
$DB_DB (name of the programe database)
For the AIML directory for $rootdir use the absolute path and include “/”. If this is wrong you may have to revisit this. You’ll get an error, and then you’ll know if you did it wrong. Sorry maaaaan.

For example if you’re on a (mt) gridserver:

$rootdir="/../../aiml/";
$DB_HOST="internal-db.sxxxxx.gridserver.com";
$DB_UNAME="username";
$DB_PWORD="password";
$DB_DB="dbxxxx_programe";

Note that src/admin needs to be writable by the web server because botloader.php will create a file subs.inc. You can also create an empty subs.inc file and make that writable.

8) From your site access botloader.php
Example:
http://MYSITE.com/programe/src/admin/botloader.php
To load the AIML into the database. This’ll take a bit so be patient!
On a dual PIII it takes approx. 1 minute for 10,000 categories.
If your PHP is running in safe mode you might need to use botloaderinc.php to load the AIML files one at a time.

botloaderinc

If you experience timeouts then access botloaderinc.php instead.
Note that whenever you load botloader.php it DELETES ALL the patterns and templates that you have in your database and then loads/reloads them from the AIML files you have. This is basically how you re-load/update your bot… by accessing that file.

9) Navigate to:
http://MYSITE.com/programe/src/talk.php

talk.php

To give it a test ride.
If it talks back, you know you dundidit right!

Sup, bro.

10) For safety reasons: PASSWORD PROTECT THE ADMIN DIRECTORY WITH A .htaccess FILE SO NO ONE CAN ACCESS THAT DIRECTORY.
Lock it down, or remove it and re-upload later when needed.

11) Finally you’ll need a PHP script for the Flash end of things to access.
In your aiml/startup.xml there is a bit at the top < bot id="TestBot" ... >

This

Substitute “TestBot” with your bot name first. That’s going to be hardcoded into the following PHP script.
Start a blank .php file then copy & paste the following:

<?php
//load in contents of respond file for use...
include "respond.php";
if (isset($_POST['theinput'])){
	$numselects=0;
	session_start();
	//unique session id for user
	$sessionid=session_id();
	$response=replybotname($_POST['theinput'],$sessionid,'TestBot');
	echo "theoutput=".trim($response->response);
}
?>

Save that as “talk_flash.php

Notice in ( $_POST ['theinput'] , $myuniqueid, 'TestBot' ); The ‘TestBot‘ is your bot id. Rename that to the name you chose for your bot. Else your shit wont work for shit.

Done! The non-Flash end is all setup
Next up…
Planet Flash.

PROGRAMMING AND CREATING THE FLASH FRONT END

In a new Flash document you’ll need two text fields.
One named “txt_output” set as a Dynamic Text field.
One named “txt_input” set as an Input Text field.

And the following actions that do something along the lines of…

ACTIONSCRIPT 3.0 would look like:

var url_php:String = "http://www.nathalielawhead.com/programe/src/talk_flash.php";
var bool_firstrun:Boolean = true;
//Run
function run():void {
	//Set up url vars
	var url_vars:URLVariables = new URLVariables();
	var request:URLRequest = new URLRequest();
	//if its first run send an initial message
	if (bool_firstrun) {
		//then set to false...
		bool_firstrun = false;
		//firstrun message to establish a connection
		url_vars.theinput="Hello. My name is visitor.";
	} else {
		//if not do default
		url_vars.theinput=txt_input.text;
		txt_input.text="";
	}
	//Post
	request.url=url_php;
	request.method=URLRequestMethod.POST;
	request.data=url_vars;
	//URL loader for above
	var loader:URLLoader = new URLLoader();
	loader.dataFormat=URLLoaderDataFormat.VARIABLES;
	loader.addEventListener(Event.COMPLETE, sendcompletehandler);
	loader.load(request);
	//receive all the above and throw it into txt_output
	function sendcompletehandler(event:Event):void {
		txt_output.text=event.target.data.theoutput;
	}
}
//Keyboard
txt_input.addEventListener(KeyboardEvent.KEY_UP, key_run);
function key_run(event:KeyboardEvent):void {
	if (event.keyCode==Keyboard.ENTER) {
		run();
	}
}
//start
run();

ACTIONSCRIPT 2.0 version would look like:

var myfilename:String = "http://YOURSITE.com/programe/src/talk_flash.php";
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
	if (Key.getCode() == Key.ENTER) {
		submit();
	}
};
Key.addListener(keyListener);
function submit() {
	var result_lv:LoadVars = new LoadVars();
	result_lv.onLoad = function(success:Boolean) {
		if (success) {
			txt_output.text = result_lv.theoutput;
		} else {
			trace("error");
		}
	};
	var send_lv:LoadVars = new LoadVars();
	send_lv.theinput = txt_input.text;
	send_lv.sendAndLoad(myfilename, result_lv,"POST");
	txt_input.text = "";
}

[swfobj src=”http://nathalielawhead.com/sourcefiles/Chatbot_Programe_E_Flash/pflash.swf” height=”400″ width=”400″]
Type above to test
Flash Chat Bot

You get the idea…
This is a veeeeeery super simple barebones example that really just does the basic sending / receiving of variables.
You can easily take this example and extend it.
Make a class out of it. Have the text write itself out a character at a time, as if the bot is typing to you. Have multiple text fields that connect to multiple bots to create a virtual chatroom. Or have an isometric game where each character is hooked to a bot that you talk to, and they give you information to send you on your quest… You get the idea… Bots are fucking awesome, and this is how simple they are.
Now go and have some creative fun on your great bot master adventures!

Source Files Here:
http://nathalielawhead.com/sourcefiles/Chatbot_Programe_E_Flash

Further Links:

Façade

RiveScript
(Perl) A text-based scripting language for giving responses to chatterbots.
This one’s worth keeping an eye on.

Beyond Façade: Pattern Matching for Natural Language Applications

ChatScript
http://sourceforge.net/projects/chatscript/files/

suzette
http://www.chatbots.org/chatbot/suzette/
http://ai.bluemars.com/chat/
http://bluemars.com/

cleverbot
http://www.cleverbot.com/

ultra hal
http://www.zabaware.com

http://www.personalityforge.com/

http://www.chatbots.org/

French version of installing here!