Writing quorum plugins

It is possible to extend quorum’s functionality by calling routines from libraries written in other languages such as java. I wanted to create a chat bot using the Program-AB library which is the reference implementation of AIML 2.0. Program-AB is written in java and I will show you how I created a plugin for this library. The basic steps to add a plugin to quorum are as follows.

  1. Create a pure java wrapper for the library you want to add and build it to a jar file..
  2. Define the relevant system actions in quorum.
  3. Use the actions in your quorum project.

Creating the java wrapper

The java wrapper will define the methods you want to use from the library. Yes I know, the library already does this but you need a way to associate the methods of the library with quorum and this is where the wrapper comes in.

  1. Create a project of type class library using the java template. Press alt+f for file, arrow to new project and activate it by pressing the enter key. Tab once to get to a list of categories where you choose java. Tab once more and choose either a java application or a java class library.
  2. You will be asked to name your library and select some other options. This is also where you can set the location of your library. Leave these options at their default settings unless you need to change something.
  3. Tab to the button labeled “Finish” and press enter.
  4. You will be placed in an empty project. Navigate the project tree until you reach an item called ‘source packages’
  5. Expand this item and you will be on an entry called default package
  6. This is where things get tricky if you are using a screen reader. You need to create a file to hold your code inside this package. You can click in the relevant window and paste your code in. However, as a screen reader user, your best option is to activate the context menu and navigate to the new option. Expand it with the right arrow key and then use the up and down arrow keys to choose java class.
  7. Tab through the resulting dialogue. You will need to enter the name of the class. You also need to enter the package name. This is crucial because it is required for quorom to asociate your wrapper as a plugin. The package name must be prefixed by plugins.quorum. Complete entering the information and hit ok to create the class.

Once you are placed in the class definition, you must add the following line immediately after the opening brace.
public java.lang.Object me_ = null;
You now must add the dependencies for this project. These are usually in the form of jar files. To add them, you do the following.

  1. Hit ctrl+1 to get to the projects tree view.
  2. Navigate to the node called libraries .
  3. Activate the context menu and arrow to the option called add jar folder and press the enter key to activate it.
  4. You will be in a dialogue that resembles the windows file open dialogue. If you are a screen reader user, you will want to paste the path to the folder containing the jar files directly into the edit box that is asking for a file name. This is because if you brows to a folder containing a subfolder and hit enter, that folder will be entered into the dialogue box. The easiest way is to grab the path from windows explorer and paste it into the file name box of the common files dialogue.
  5. Tab to the open button and press the enter key to activate it.

You now have a regular java class where you can write standard java code. If you have elected to use a java class library, you must add a main method otherwise you will be unable to build the program because netBeans will look for a main method when building the project. See the below code for an example of an empty main routine.
public static void main(String[] args) { }
If you are a screen reader user, when you build the project for the first time, you will be asked to select a main method. There is no indication that netbeans is asking you for this. You have to hit the f6 key to navigate to that dialogue box and select the main method. This is a one time operation.

Note

Keep the name that you have defined and the name of the package handy before moving to the next section.

Defining quorum actions

You need to create a quorum project. You can choose the games template or whatever project you like. The steps to create a new project within netbeans are the same as given in the section on creating the java wrapper. Once you have created a quorum project, you need to add a separate quorum class to hold the routines from the wrapper. When creating the class, the name of the class has to be the same as that of the wrapper class. In addition, the name of the package has to be the same as what you defined when creating the wrapper project. There however is one important difference. You will not add the plugins.quorum prefix to the name of your package. Suppose the package name in your wrapper was plugins.quorum.chocolate the package name for the quorum project will be class chocolate Once again, the names of the packages are used by quorum to associate the quorum actions with the underlying code of the plugin. IN addition, you will add the jar that you built as the result of creating the wrapper class to this project. You will also add all other jar files that you added as dependencies in the java wrapper. This is a quirk of netbeans where it has runtime and compile time jar files. See the following link for more information.
How to add a JAR in NetBeans

Writing the action methods

The first thing you need to do is to tell quorum to use the library. You will do this by using the package statement. Add a line similar to the below text before the class declaration.
package chocolate
This line may already be added during the class definition process.
In the class, you need to create action methods. These methods must have the same names and signatures as the public methods of the wrapper class.

Using the plugin in the main class

You are now ready to use the plugin. Navigate to the main class and add a use statement to the top like you would when using any other quorum library.
use chocolate.all
You can begin writing code using the library.

Some sample code

My java wrapper for Program-AB is below.

package plugins.quorum.botPackage;
import org.alicebot.ab.*;
public class botHandler {
public java.lang.Object me_ = null;
String botname="ciso";
String botPath="D:/bdump";
Bot bot;
public botHandler()
{
bot=new Bot(botname,botPath);

};
public String botConverse(String rq)
{
String answer="";
Chat chatSession = new Chat(bot);
answer=chatSession.multisentenceRespond(rq);

return answer;

};
public static void main(String[] args) { }
}

My quorom test action code is below.

package botPackage
class botHandler

system action botConverse(text rq) returns text
end


I am using the library in my quorom main class as follows. Please note, this is test code to ensure that the plugin is working.

use Libraries.Game.Game
use botPackage.all

class Main is Game

botHandler bt
action Main

StartGame()
end

action CreateGame
text ans=bt:botConverse("what is cyber security")
output ans
say ans
end

/*
The update action is called on every "frame" of animation in a game. This
action should be used to do operations on items. So, for example, we
might use this action to change frames of animation, to rotate items,
or to make creatures jump.
*/
action Update(number seconds)
end
end

References

program-ab – ProgrammingInterface.wiki
Writing Quorum Plugins
Multiple Skype chats will William Allee


Filed under: robotics — Tags: , , , , — security-writer @ October 9, 2016 09:25

How to install Program-O with the LEMP stack

Program-O is an environment that allows you to host chatbots. As of this writing, it is AIML version 1.1 compliant. Its interface is easy to use and you can get started easily. However, installation can take some time and troubleshooting. Here is the guide I wish I had when I began installing Program-O.

My environment

  • Ubuntu 16.04 64bit
  • Nginx
  • MySQL
  • PHP
  • Vmware workstation and player version 12 running on Windows 10 professional 64bit.
  • Your machine obtains its IP address automatically via DHCP.

Prepareing Ubuntu

If you are starting from scratch, let the automatic installer handle the creation of the virtual machine. Follow the prompts and you will be fine. The only thing you should change is the network connection. I have set mine to bridged and to replicate the state of the physical network connection. This way, the virtual machine gets its own IP address and comes up with a working connection to your LAN. Once you have ubuntu installed, you will have to add a SSH server. You can install this using the gui but I will cover the commandline approach. Once you are at the ubuntu desktop, hit ctrl+alt+t to get into the terminal. Then type
sudo apt-get update
to ensure that the package repositories are updated and then type
sudo apt-get install openssh-server -y
this command will download and install the SSH server.
You can then run ifconfig to see the address that has been assigned to your virtual machine.
Exit the terminal and return to Windows. Use a terminal emulator to login into your virtual machine.

Configuring users and groups

You do not run as root in ubuntu. The user you created at the time of installation has sudo access. It is tempting to do everything as root but avoid that temptation because you may have to do a fair bit of ownership changes once you are ready to deploy your machine. You need to add your user to the www-data group. You will need this to work with nginx. To do this, run the following command.
usermod -a -G www-data bob

Note

I am working on the assummption that bob is your user name.
This completes the ubuntu configuration.

Installing git

You will need a git client to pull Program-O from its git repository. Git is not installed on ubuntu by default. You can install it by typing the following command into the terminal.
sudo apt-get install git -y

Installing the components required for Program-O

MySQL

The first thing you need is the MySQL database server. You can install it by issuing the following command and following the prompts.
sudo apt-get install mysql-server -y
You should secure your MySQL installation. Run the following command.
sudo mysql_secure_installation
You do not need the test database. Once this script has finished running, login into the MySQL database by typing the following command.
mysql -u root -p
Once you are in, you need to create a database. You can do this by issuing the command
create database bots;
You need a database user too as you do not want the root user doing any work with the database.
CREATE USER ‘botmaster’@’localhost’ IDENTIFIED BY ‘bot123’;
you then issue the command
use database bots;
At this point, you have a user and a blank database but the user cannot do anything. You need to make the user capable of administering the bots database.
GRANT ALL PRIVILEGES ON * . * TO ‘botmaster’@’localhost’;

Nginx

It is now time to setup the web server which is nginx. To install nginx, issue the followig command at the terminal.
sudo apt-get install nginx -y

Install php

It is time to install php. Nginx can serve static content but your chatbot is dynamic content because its input and output will change. Program-O also needs to write configuration values to the server.
sudo apt-get install -y php-fpm php-mysql php7.0-mbstring php7.0-zip php7.0-xml 
You need to change one setting in the php configuration to make things more secure. See the references section for details. Execute the following command.
sudo nano /etc/php/7.0/fpm/php.ini
You can use any editor of your choice.
Change thecgi.fix_pathinfo=1 to
cgi.fix_pathinfo=0
remembering to uncomment the above line.
Restart the php preprocessor by issuing the following command.
sudo systemctl restart php7.0-fpm
also restart the web server by using the below command.
 service nginx reload

Configuring nginx to use php

If you have followed this guide, everythingng that you need to run Program-O should be installed. You have to configure nginx to use php. This involves careful editing of the nginx configuration file. Do not be distracted by the comments in that file.
sudo nano /etc/nginx/sites-available/default
Again, you can use whatever editor you want.
The first thing we do is to set our server’s name or ip address to prevent unnecessary warnings. The chances are that you do not have a local domain name server running so set the IP of your virtual machine here.
server_name 192.168.xx.xxx
Find the line that reads
root /var/www/html;
Note the line below it.
index index.html index.htm index.nginx-debian.html;
You need to add index.php to the above line so that nginx first servers files with a php extention. The modified line looks like this.
index index.php index.html index.htm index.nginx-debian.html;
You now need to uncomment the following lines.
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Once you have completed these changes, save the file. It is crucial to test it for errors. To do this, run the following command.
sudo nginx -t
If there are no errors, move on. If you encounter errors, recheck what you changed.

Installing Program-O

You need to get the Program-O files into the web root of your web server. This should be /var/www/html if you have not changed this in nginx. To install Program-O, do the following.

  1. cd /var/www/html
  2. sudo git clone https://github.com/Program-O/Program-O.git

Setting ownership and permissions

One of the trickiest things when configuring web servers and web applications is to set the right ownership of files and folders. You have to get this right else php will be unable to write configuration information or logs may not be generated in the event of errors etc.
The web server files should not be owned by root and php should be permitted to write to the folders under the Program-O folder.To do all of this, execute the following commands.
sudo chown -R www-data:www-data /var/www/html/Program-O
The /var/logs/nginx folder should also be owned by www-data so that logs can be created.
sudo chown -R www-data:www-data /var/log/nginx

Running the installation script

You are ready to run the Program-O installation. Launch a web browser and navigate to the following link.
http://your_virtual_machine_ip_address/Program-O/install/install_programo.php
Follow the prompts and if there are no errors, you should be good to go and setup your chatbot.

References


Filed under: commonTasks,robotics — Tags: , , , — security-writer @ October 2, 2016 12:53