Chatbot::Alpha
AiChaos Alpha Chatterbot
¤ Name
AiChaos, Inc.'s AlphaBot Reply System.


¤ Description
Alpha is a simplistic yet very powerful response language.


¤ Synopsis
use Chatbot::Alpha;
my $alpha = new Chatbot::Alpha (debug => 1);

$alpha->load_folder ('./replies');
$alpha->load_file ('./more_replies.txt');

# Set and remove variables.
$alpha->set_variable ("master", "1");
$alpha->remove_variable ("master");
$alpha->set_variable ("var", "value");
$alpha->clear_variables;

# Go get a reply.
my $user = "foo";
my $message = "Hello Alpha";
my $reply = $alpha->reply ($user,$message);


¤ Methods
new
Creates a new AlphaBot instance. If you want to have more than one instance of Alpha (i.e. multiple bots), you need to create a new instance for each one.

This can also take the flag DEBUG, if you are a developer.

my $alpha = new Chatbot::Alpha (debug => 1);

version
Returns the version of the module, useful if you want to require a specific version.

my $version = $alpha->version;

debug
Prints a debug message... it shouldn't be called by itself, the module will call it when a debug message needs to be printed.

$alpha->debug ($message);

load_folder
Loads a whole folder of reply files. By default it will load every file it finds, but if the folder contains files of other types it may cause errors... in this case, a parameter of FILE_TYPE may be sent, so that it will only open that type of file. It's always best to send the FILE_TYPE in anyway.

$alpha->load_folder ("./replies", "txt");

Will return 0 if the folder couldn't be accessed, or -1 if the folder was empty (or no files of the specified type were found), or -2 if there was a fatal error with one of the files. Having debug mode turned on will reveal the problem.

load_file
Loads a single file. This is also called by the load_folder method for each file in that folder. Returns the same values as load_folder (0 = file not found, -2 = file had errors)

$alpha->load_file ("./reply.txt");

default_reply
Sets the reply that Alpha will return if no better reply can be found. This can include pipes for random responses.

$alpha->default_reply ("Hmm...|I don't know.|Let's change the subject.");

set_variable
Sets a global variable inside the brain.

$alpha->set_variable ("var", "value");

remove_variable
Removes a global variable from the brain.

$alpha->remove_variable ("var");

clear_variables
Clears all set variables added through set_variable

$alpha->clear_variables;
reply
Gets a reply. Prerequisite is that replies have to be loaded, of course. Reply files are loaded through the load_folder or load_file methods.

$alpha->reply ($id,$message);


¤ Alpha Language Tutorial
The Alpha reply language is a simple line-by-line command-driven language. The comment indicator is a "//", as it is in JavaScript and C++. Each line has two components: the first character, the command char., and the rest of the line, the command arguments. The command characters are as follows:

+ (Plus)
The + command indicates the starting of a new reply, and that the immediately following commands relates to that reply. The data for the + command would be the message trigger (i.e. "+ hello bot"). Each reply can have only one trigger, however there is a "redirect" command (see below) to redirect a message to another reply.

- (Minus)
The - command indicates the response to the + trigger above it. The - command has many different uses, however; for example, a single + and a single - can create a single one-way question/answer response. Or, a single + can have multiple -'s and each - would be a random response to the input. Or, if you're making a "conversation holder" (see below), there would be one - as the first message. Or, if you're making a conditional, the -'s would only be called when the conditionals all returned false.

@ (At Symbol)
The @ command is used as a redirect. Each reply set can have only one redirect element. The data for the redirect would be the message that would be matched by another reply. For example, if you have a reply to "identify yourself" with the bot's identity, you could have another trigger "who are you" refer back to "identify yourself" - there's an example of this, see below.

* (Star Symbol)
The * command is for conditionals. A good conditional looks like this: "* if (variable) = (value)", each element should be separated by a single space. If the elements can't be found, it will not match correctly. If the "IF" part isn't where it should be, the entire thing will be skipped over. There's an example of this too, see below.

& (Amperstand)
The & command indicates a "conversation holder." One such reply would begin with the + command, as always, followed immediately by a - command (for the first reply in the chain). All other commands following would start with an "&" and would be called one at a time until there are none left. There's a couple examples of this too, see below. Also, this is the one special case in which the <msg> tag can be included in the replies.
# (Pound)
The # command indicates an evaluation. Its arguments would be Perl codes (assuming the parser is Perl), and the code would be evaluated at the end. It's always a good idea to include a - reply, in case something goes wrong.


¤ Example Reply Code
Here's an example reply code:

// Test Replies

// A standard reply to "hello", with multiple responses.
+ hello
- Hello there!
- What's up?
- This is random, eh?

// A simple one-reply response to "what's up"
+ what's up
- Not much, you?

// A test using <star1>
+ say *
- Um.... "<star1>"

// This reply is referred to below.
+ identify yourself
- I am Alpha.

// Refers the asker back to the reply above.
+ who are you
@ identify yourself

// Conditionals Test
+ am i your master
* if master = 1::Yes, you are my master.
- No, you are not my master.

// Perl Evaluation Test
+ what is 2 plus 2
- Placeholder.
# $reply = "2 + 2 = 4";

// A Conversation Holder: Knock Knock!
+ knock knock
- Who's there?
& <msg> who?
& Ha! <msg>! That's a good one!

// A Conversation Holder: Rambling!
+ are you crazy
- I was crazy once.
& They locked me away...
& In a room with padded walls.
& There were rats there...
& Did I mention I was crazy once?
¤ Change Log
Version 1.1
¤ Added the # command.
¤ Fixed bug in reply matching.

Version 1.0

¤ Initial release.
¤ Author
Copyright (C) 2004 Cerone Kirsle; kirsle[at]aichaos[dot]com

Chaos AI Technology