Welcome to Episode 2 of this series. In the previous episode, we looked at how to use the new Version 2 (V2) of the Google Wave Robot API to write your first Google Wave Robot. In this episode and the next few ones, I shall be covering a RAD Framework that makes writing Google Wave Robots much easier and has features that hides a lot of details from you and lets you focus on the functionality of your Robot. The WadRobotFramework allows you develop your Robot quickly so that you can focus on the logic of your Robot, while letting the framework do the heavy lifting.
What is the WadRobotFramework?
The WadRobotFramework is available at http://code.google.com/p/wadrobotframework.This framework is the work of Jerome BATON, who has stated that the goal of the framework is to make writing Google Wave Robots in Java much simpler.
Here are some of the features that WadRobotFramework has:
- An Auto Generator that allows you to simply specify some properties and it will generate an entire Eclipse Project with the templates present for your Robot. You just need to then fill out the method implementations and you are done. It is RAD at its best. And we will cover that in a future episode.
- Defines Simple and Advanced Robots. These Robots provide base classes that encapsulate a lot of the Wave API so that you can focus on writing your Robot code and not worry about the Wave API specifics. Again if you want that the flexibility of using the Wave Robot, be assured that the Advanced Robots exposes all the Wave objects so that you have full control over how you want the Robot to implement its logic.
- Several other interesting features like Obedient Robots that allow your robot to have fine grained control over what the participants can do in the Wave.
I suggest you visit the page at http://code.google.com/p/wadrobotframework/ and take a look at the documentation. Feel free to contribute to it also.
For now, let us focus on writing simple Robots using this framework. But first some basics on the types of Robots that WadRobotFramework provides. The framework goes ahead and defines two kinds of Robots: Simple and Advanced.
Simple Robots
Simple Robots are typically those that can react to a new Blip. This kind of Robots simply wait till a Blip is submitted. And on submission of the Blip, they will append a message to the Blip. It could be as simple as just analyzing the text of the blip and appending some new information or simply a fun sort of a Robot, which simply prints a random message.
An example of a this is shown below and which we will build in this episode:
In the above wave, there are two participants (myself and the Robot). The Robot announces itself when it is added to the Wave (note that the message that you see above is auto generated by the Robot due to inbuilt support by WadRobotFramework. You could provide your own welcome message too!). Additionally, whenever I submit a Blip (for e.g. Hello Dear Appender Robot), it simply appends a new blip with some text.
Simple Robots are also those that can modify the Blip Text instead of appending a new blip. For e.g. They could read the blip text and filter out the profanities, do automated spell checks or simply add some information to the existing Blip Text itself. In other words, it modifies the Blip Text.
Shown below is an example of a Simple Robot that modifies the Blip Text:
In the above wave, there are two participants (myself and the Robot). The Robot announces itself when it is added to the Wave. Additionally, whenever I submit a Blip (for e.g. Hello Blip Modifier Robot), it simply modifies the Blip Text by mentioning when I submitted the Blip text.
There is also a 3rd thing that the Simple Robots could do, which is very useful too. They can act in a stealth manner and simply do their work quietly. For e.g. whether you wish to write a Robot that appends a Blip or modifies a submitted Blip, you can simply chose not to append or modify but instead get the Blip text and do your work quietly. For e.g. count the words in the background, etc.
The WadRobotFramework also supports Advanced Robots, which are particularly interesting and will be covered in the Next Episode.
Advanced Robots are those that can react to commands in the Blips. Here are some samples of Advanced Robots and how they would react to commands from Blips:
1. A character Counting Advanced Robot:
Your submitted Blip Text contains : “Here is some text in the blip. Please count the length of this message for me. {mycountingrobot:count}”.
You can write an advanced Robot (mycountingrobot) that knows how to count the length of the message. So it gets notified when it there is a command (count) in the Blip. Your Advanced Robot can then count out the characters and then either append or modify a blip as needed.
2. A Tweeting Robot:
Your submitted Blip Text contains the following text : “{mytweetingrobot:tweet}
You can write an advanced Robot (mytweetingrobot) that knows how to tweet the message to Twitter. So it gets notified when it there is a command (tweet) in the Blip. Your Advanced Robot can then append a blip or modify a blip saying that the message has been tweeted.
The best part of it all is that you could combine all of this into a single Robot that can respond to one or more commands. For example, take a look at Today’s Special Robot (seehttp://ppandhi.wordpress.com/2009/11/08/todays-special-robot/) that can respond to more than one command. It can give you the quotes, day in history, word of the day, cricket score, your daily horoscope by simply responding to the command that you type in. With the WadRobotFramework, we shall see in the next episode how you can write such an Advanced Robot that responds to different commands. If you cannot wait, take a look at the documentation at the site (Advanced Robots).
Since this episode will be focusing on Simple Robots, let us recap what we know so far: The WadRobotFramework supports Simple Robots in two flavours: The BlipAppenderRobot and the BlipModifierRobot. The BlipAppenderRobot is used to simply append to a Blip. TheBlipModifierRobot is used to modify a submitted Blip.
Simple, isnt it? Lets develop and deploy our Simple Robots using the WadRobotFramework.
Create a New Project
We need to create a New Project first. Follow the steps below:
1. Either click on File –> New –> Other or press Ctrl-N to create a new project. Select Google and then Web Application project. Alternately you could also click on the New Web Application Project Toolbar icon as part of the Google Eclipse plugin.
2. In the New Web Application Project dialog, deselect the Use Google Web Toolkit and give a name to your project. I have named mine WadRobots and I suggest you go with the same name so that things are consistent with the rest of the article (but if you wish to name it something else, that is fine too). The Screenshot is shown below:
3. Click on Finish. This will generate the project and also create a sample Hello World Servlet for you. But we will be writing our own Servlet. So I suggest that you can delete the Servlet Java class and the mappings made in the web.xml or you can leave it for now since we are going to write our own.
Adding Google Wave Robot JAR files and WadRobotFramework JAR file to your Project Path
Since we are going to be writing a Wave Robot, we need some additional files on the client side. These additional files (JAR files) are required for the additional Wave API’s and also for deployment in your WEB-INF\lib folder, so that they are correctly deployed and available to the run-time engine. These JAR files do not ship along with the Google Eclipse plugin, so you will need to download them for a website. The Google code website for the JAR files is:
http://code.google.com/p/wave-robot-java-client/downloads/list
The web page when you navigate to the above URL is shown below:
Download the above file to your machine. It consists of a single ZIP file that contains several JAR, source and documentation files. You will need all of these JAR files in your project. The JAR files that you will find in the ZIP file and which you will need to add to your Project classpath are shown below:
- commons-codec-1.4.jar
- gson.jar
- oauth-20090617.jar
- wave-model-20100129.jar
- wave-robot-api-20100428.jar
The WadRobotFramework JAR file is available at the following location :
http://code.google.com/p/wadrobotframework/downloads/list
The web page when you navigate to the above URL is shown below:
You will need to download the WadRobotFramework.jar file. Once you have downloaded the files, follow these steps to setup your Project Build Path and runtime correctly.
1. Copy all the 5 JAR files to the WEB-INF\lib folder of your Eclipse Project.
2. Right-click on the Project in the Project Hierarchy. Select Propertiesand then Java Build Path. Click on Add JARs and then select the 5 JAR files from your Project WEB-INF\lib folder.
3. Your Project Build Path should like the screenshot below.
Click on OK to proceed. This completes your Build Path setup with the Google Wave Robot JAR andWadRobotFramework JAR files.
Writing the Simple Robot: MyAppenderRobot : MyAppenderRobot.java
Let us first create our Simple Robot Java class based on the WadRobotFramework. This Simple Robot is the one that will react to a new Blip. So all we will do is to make the robot append to the blip, when the blip is submitted. The Robot demonstrated here will be straightforward and you can easily modify it to make it react in your own way.
As discussed, this is known as the BlipAppenderRobot and so all we need to do is to extend the BlipAppenderRobot class in the WadRobotFramework and provide our simple implementation.
The steps are straightforward and given below. All we need to do is write our class that extends the org.wadael.waverobotfrmwrk.simple.BlipAppenderRobot class and provide an implementation for the getTextToAppend method.
Follow these steps:
1. Create a new Java class within the same package. The New Java Class dialog is shown below. I have named the class MyAppenderRobot as shown below. Click on the Browse button to select a Super Class.
2. In the Superclass Selection dialog shown below, type the word BlipAppenderRobot (some part of it is also OK as the screenshot shows below) in the Choose a type field as shown. This will bring up the correct Matching items i.e. org.wadael.waverobotfrmwrk.simple.BlipAppenderRobot. Click on OK.
This will generate the code and you simply replace it with the following code listing given below:
package com.googlewaveexperiments.wadrobots;
import org.wadael.waverobotfrmwrk.simple.BlipAppenderRobot;
public class MyAppenderRobot extends BlipAppenderRobot {
@Override
protected String getTextToAppend(String arg0) {
// TODO Auto-generated method stub
return "I am appending some text too";
}
}
Let us go through the code now:
1. We have extended the BlipAppenderRobot since all we want to do in this Robot is to react to the blip and append our own Blip
2. All we need to do as part of extending the BlipAppenderRobot class is to implement thegetTextToAppend method. This method gets passed one parameter msg that is the blip text that was submitted. So in all essence, you could inspect what the text was in the blip submitted and then react to it. The implementation simply returns the string that it wants to append. The WadRobotFramework’s BlipAppenderRobot will take care of creating a Blip and appending it to the current blip.
That is all there is to implemented the MyAppenderRobot. Simple … isnt it ?
Configuring the MyAppenderRobot in web.xml
We need to add the MyAppenderRobot in the <servlet/> and <servlet-mapping/> entry to the web.xml file. This file is present in the WEB-INF folder of the project. The necessary fragment to be added to your web.xml file are shown below.
<servlet> <servlet-name>AppenderRobotV2</servlet-name> <servlet-class>com.googlewaveexperiments.wadrobots.MyAppenderRobot</servlet-class> </servlet> <servlet-mapping> <servlet-name>AppenderRobotV2</servlet-name> <url-pattern>/_wave/*</url-pattern> </servlet-mapping>
Deploying the Application
To deploy the application, you will need to first create your Application ID. The Application Identifier can be created by logging in at http://appengine.google.com with your Google Account. You will see a list of application identifiers already registered under your account (or none if you are just getting started). To create a new Application, click on the Create Application button and provide the Application Identifier as requested. Please note this name down since you will be using it for deployment.
For e.g. I have registered an application identifier named wadrobot.
To deploy the application, follow these steps (they should be familiar to you now):
- Click on the Deploy Icon in the Toolbar.
- In the Deploy dialog, provide your Email and Password. Do not click on Deploy button yet.
- Click on the App Engine Project settings link. This will lead you to a dialog, where you need to enter your Application ID [For e.g. my Application Identifier wadrobot]
- Click on OK. You will be lead back to the previous screen, where you can click on the Deploy button. This will start deploying your application to the GAEJ cloud. You should see several messages in the Console window as the application is being deployed.
- Finally, you should see the message “Deployment completed successfully”.
MyAppenderRobot in Action
Your application is going to be available at the http://yourapplicationid.appspot.com. In my case, the application is available at http://wadrobot.appspot.com.
To test out the Robot, you need to launch the Google Wave client and login in with your account by going to http://wave.google.com. On successful login, you will be inside the Wave client from where you can create a new wave by clicking on the New Wave link. When you do that, currently you are the only participant in the wave.
Click on the + sign next to your icon and you can add one or more participants as shown below:
NOTE : Your Google Wave Robot is going to be available at <YOURAPPLICATIONID>@appspot.com , hence I have added wadrobot@appspot.com as that was my application id. But you can replace it with your application id.
If all goes well, you will see your Robot added as a participant and it will announce itself through the default message that the WadRobotFramework generates for you. In future episodes we will take a look at customizing it but the default implementation will suffice for now.
Now I type a message “Hello Dear Appender Robot” as shown below
and click on the Done button, the BLIP_SUBMITTED event is fired and our Robot gets the event i.e. the getTextToAppend method is invoked on our Robot Java class. The method simply appends a blip as shown below:
Writing another Simple Robot: MyBlipModifierRobot : MyBlipModifierRobot.java
We saw how easy it was to create our Simple Robot that simply appended a blip to the wave conversation when a blip was submitted. Let us now write another Simple Robot that will modify the Blip Text when the blip is submitted.
As discussed, this is known as the BlipModifierRobot and so all we need to do is to extend the BlipModifierRobot class in the WadRobotFramework and provide our simple implementation.
The steps are straightforward and given below. All we need to do is write our class that extends the org.wadael.waverobotfrmwrk.simple.BlipModifierRobot class and provide an implementation for the modifyBlipText method.
Follow these steps:
1. Create a new Java class within the same package. Name it MyBlipModifierAppenderRobotas shown below and mention the superclass as org.wadael.waverobotfrmwrk.simple.BlipModifierRobot
This will create the MyBlipModifierRobot.java file and you can replace it with the following source:
package com.googlewaveexperiments.wadrobots;
import java.util.Date;
import org.wadael.waverobotfrmwrk.simple.BlipModifierRobot;
public class MyBlipModifierRobot extends BlipModifierRobot {
@Override
protected String modifyBlipText(String originalBlipText) {
// TODO Auto-generated method stub
return originalBlipText + " [You typed this at :" + new Date().toString() + "]";
}
}
Let us go through the code now:
1. We have extended the BlipModifierRobot since all we want to do in this Robot is to modify the Blip that was submitted.
2. All we need to do as part of extending the BlipModifierRobot class is to implement the modifyBlipText method. This method gets passed one parameter originalBlipText that is the blip text that was submitted. So in all essence, you could inspect what the text was in the blip submitted and then modify it as needed . The implementation above appends the text “You typed this at [DateTimeStamp]” to the originalBlipText and simply returns that string. The WadRobotFramework’s BlipModifierRobot will take care of modifying the Blip Text.
That is all there is to implemented the MyBlipModifierRobot. Simple again, isnt it ?
Configuring the MyBlipModifierRobot in web.xml
We need to add the MyBlipModifierRobot in the <servlet/> and <servlet-mapping/> entry to the web.xml file. This file is present in the WEB-INF folder of the project. The necessary fragment to be added to your web.xml file are shown below.
<servlet> <servlet-name>ModifierRobotV2</servlet-name> <servlet-class>com.googlewaveexperiments.wadrobots.MyBlipModifierRobot</servlet-class> </servlet> <servlet-mapping> <servlet-name>ModifierRobotV2</servlet-name> <url-pattern>/_wave/*</url-pattern> </servlet-mapping>
If you wish to check out this Robot in action, you can do so with the same project. Simply replace the previous Robot’s (MyAppenderRobot) servlet entries with the one shown above. And deploy the application following the same steps in the Deploying the Application section.
Once you deploy the MyBlipModifierRobot, you should see it in action as shown below. The Robot announces itself in the standard fashion and then it modifies the blip text that I submitted by appending the TimeStamp to it.
Conclusion
This concludes Episode 2 of the series. We saw how easy it was to write Simple Google Wave Robots using the WadRobotFramework. Stay tuned for more Google Wave Robot goodness using the WadRobotFramework.




Tweets that mention Episode 2 : Writing a Wave Robot using WadRobotFramework « Google Wave Experiments -- Topsy.com
May 15, 2010 @ 15:06:01
Surfing History | Jason He
May 16, 2010 @ 08:59:48
Google Wave Programming Articles Update « Google App Engine Java Experiments
May 31, 2010 @ 03:35:08