End of this blog

2 Comments

Google officially announced that it is no longer going to develop Wave. The product in its current form will be available only till end of 2010.

Given these developments, I have decided to stop focusing on Wave. I personally think and still believe that Wave is an excellent product. Its biggest strength was its flexibility. Ironically, that proved to be its weakest link.

Google Wave is dead. Long live Google Wave.

Episode 3: Authoring Google Wave Gadgets using Adobe Flex

2 Comments

My article on “Authoring Wave Gadgets using Adobe Flex” is now published on Inside RIA (http://www.insideria.com).

URL : http://insideria.com/2010/07/developing-google-wave-gadgets.html

Hope you find it useful.

Episode 2 : Writing a Wave Robot using WadRobotFramework

3 Comments

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):  

  1. Click on the Deploy Icon in the Toolbar.
  2. In the Deploy dialog, provide your Email and Password. Do not click on Deploy button yet.
  3. 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]
  4. 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.
  5. 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.

Episode 1 : Writing your First Google Wave Robot using Robot API v2

18 Comments

Welcome to Episode 1 of this series. I plan to make this blog dedicated to experiments that I do with Google Wave. I have another blog called GAEJExperiments, where I chronicle my experiments with Google App Engine Java.

I had written an episode on GAEJExperiments : Episode 7 where I described how you can write your first Google Wave Robot using the Robot API. The Google Wave Team has released a *new* version of the Robot API V2, with exciting new features. Additionally, the old API is also deprecated and will stop functioning on 30th June 2010. Given that in mind, I have updated the original article in this blog post.

And moving forward, all my Google Wave experiments will be chronicled here. Hope you will find them useful. Lets go…

Google Wave, as you must have heard is a collaborative messaging platform from Google. A Wave is a collaboration between several participants (humans and non-humans) using threaded conversations/documents/gadgets to achieve their task. The task could be as simple as chatting among themselves to figure out where to dine tonight to as complex as achieving a Return Order process between a purchaser and seller.

I will not delve into the specifics of Google Wave and will assume that you have a high level idea of what it is. In fact, a great online book has just been released that covers Google Wave in depth. I strongly recommend you to read it if you want to look into Google Wave in detail. The book is named The Complete Guide to Google Wave and you can read it here.

What does this episode cover?

In this episode, we are going to look at how you can write a Google Wave Robot using the Google plugin in Eclipse. We will be using the latest version of the Robot API : version 2. We will then deploy it and host this robot in the Google App Engine cloud. This Robot is then live and can be added to any Wave and participate in the conversation.

It helps to see the final result before we start out. Even if you do not understand whats going on, just bear with me. So here is the screenshot:

Let us dissect this screen and in the process, I can hopefully explain to you some Google Wave terminology in a straightforward manner without too many details. The main points are:

1. What you see in the screen above is a Google Wave that me (the person with the photo created). A Wave is a collaboration between one or more participants (human or Robots).

2. Since we need participants to make it meaningful, you can add one or more of them. All you need to do is click the + sign that you see on the top. And  you can add one or more participants who are also signed up for Wave. In this case, my Robot GAEJ Application is hosted at http://waverobotv2.appspot.com and so I can add waverobotv2@appspot.com as a participant.

3. On adding the Robot participant, Google wave system pulls up its profile (image, etc) and adds it as a participant in the Wave that I created.

4. The Robot can receive events and it can respond to them. We shall see how to write them later (source code!!), but at this point it is enough to understand that the Robot can choose what events to listen to as the Wave collaboration happens and it can then react to them. So in the case of my Wave Robot, I have specified that I am interested in knowing when participants come and leave the Wave and when someone has submitted a message.

5. So you can see that the Wave Robot was notified when it was added to the Wave, so in response to that it printed out the message “Hello I am version 2.0 of the Robot API”. As participants get added (In this case the Robot itself), it displayed a message “Hello from Wave Robot Version 2.0 to all participants”. It will do that for any new participants added to the wave. Similarly, when I finished typing the message (Hello Robot Version 2.0), the Wave Robot got notified. It then took the message it simply echoed back what I typed.

Hope this makes things clear. But a few more terms first : Wave, Wavelet and Blip. Let me keep it simple. In the above example, the Wave was the entire container. A Wave consists of one or more Wavelets. A Wavelet can be thought of as a threaded conversation that can go on within the wave (both public and private). And each Wavelet consists of one or more messages known as a Blip. Blip is the actual message that was typed and submitted by the user. So when I typed “Hello Robot Version 2.0” above, it was a Blip.

I have simplified things a bit here but even if things are not clear at this point, do not worry. Once you get your code working, things will fall in place.

A final note above Google Wave Extensions. Extensions are the mechanism by which you can extend Google Wave by adding your own creations. Extensions are of two types : Gadgets and Robots. Gadgets are like mini-applications that run within the Google Wave client. They can be typically thought of as a UI which several participants can share at the same time. A Robot is a full blown participant in the Wave. It can be aware of most things happening in the Wave by subscribing to Events. It also has a lot of potential to modify things in the Wave like messages, etc. This article focuses on writing a Robot. If you are interested in a good summary of Google Wave Extensions and some key differences between a Gadget and a Robot, read it here.

OK, so enough of theory. Let us get down to coding. If you are new to developing with the Google Plugin for Eclipse, please read my GAEJ Experiments episodes to setup your environment (Episode 1and Episode 5)

Let us move on to coding now. Launch Eclipse and follow the steps given next.

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 WaveRobotVersion2 and I suggest you go with the same name so that things are consistent with the rest of the article. 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 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 all highlighted files (in yellow) the above files to your machine. Once you have downloaded the files, follow these steps to setup your Project Build Path and runtime correctly.

1. Copy all the 4 JAR files to the WEB-INF\lib folder of your Eclipse Project.

2. Right-click on the Project in the Project Hierarchy. Select Properties and then Java Build Path. Click on Add JARs and then select the 4 JAR files from your Project WEB-INF\lib folder as shown below and click on OK.

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 files.

Writing the Google Wave Robot Servlet : MyFirstGoogleWaveRobot.java

Let us first create our Robot Java class. The steps are straightforward and given below. All we need to do is write our class that extends the com.google.wave.api.AbstractRobot class and provide the appropriate implementations for the Robot Profile and the events that you are interested in responding to.

Follow these steps:

1. Create a new Java class within the package listed below: com.waveexperiments.robot (You are free to go with your package name). The New Java Class dialog is shown below. I have named the class MyFirstGoogleWaveRobot as shown below. Note that the Superclass has been selected as com.google.wave.api.AbstractRobot.

Click on Finish. This will generate standard boilerplate code.

2. The next step is to override certain methods of the AbstractRobot class, so that our Robot can provide the implementations for them. All the Wave events that you are interested in capturing like Participants got added, removed, Blip submitted, Document changed, etc can be handled by your Robot by simply overriding those methods and providing your implementation.

3. From Eclipse, select Source -> Override/Implement Methods … This will bring up a dialog as shown below. Also shown are the methods that we are interested in overriding and I have selected those. They are listed below too:

  • getRobotAvatarURL()
  • getRobotProfilePageURL()
  • onBlipSubmitted(…)
  • onWaveletParticipantsChanged(…)
  • onWaveletSelfAdded(…)

I will discuss the methods in a short while, but for now, simply click on OK. And replace the code generated with the one listed below:

package com.waveexperiments.robot;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.wave.api.AbstractRobot;
import com.google.wave.api.Blip;
import com.google.wave.api.event.BlipSubmittedEvent;
import com.google.wave.api.event.OperationErrorEvent;
import com.google.wave.api.event.WaveletParticipantsChangedEvent;
import com.google.wave.api.event.WaveletSelfAddedEvent;

public class MyFirstGoogleWaveRobot extends AbstractRobot {

 @Override
 protected String getRobotName() {
 return "Google Wave Robot Version 2.0";
 }

 @Override
 protected String getRobotAvatarUrl() {
 return "http://waverobotv2.appspot.com/_wave/myimage.jpg";
 }

 @Override
 protected String getRobotProfilePageUrl() {
 return "http://waverobotv2.appspot.com";
 }

 @Override
 public void onBlipSubmitted(BlipSubmittedEvent event) {
 Blip blip = event.getBlip();
 blip.append("\n You typed: "+blip.getContent());
 }

 @Override
 public void onWaveletParticipantsChanged(WaveletParticipantsChangedEvent event) {
 event.getWavelet().reply("\nHello from Wave Robot Version 2.0 to all the participants.");
 }

 @Override
 public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
 event.getWavelet().reply("\nHello. I am version 2.0 of the Robot API");
 }
}

Let us discuss the main points in the code

1. Your Robot class needs to extend the com.google.wave.api.AbstractRobot class

2. Your Robot needs to identify itself by providing Profile information. Profile information includes the following:

1. A Name for your Robot

2. A custom image for your Robot

3. A profile page for your Robot (a URL)

If you provide these, then the Google Wave client is able to retrieve them and set it for your Robot when it is added as a participant. This makes the Robot look more professional.

The Profile information is primarily provided by writing implementations for the 3 methods:

a. getRobotName() : This returns the name for the Robot. You can return any name that identifies your Robot

b. getRobotAvatarURL() : This is the image url that contains an image for your Robot. I have included here the url where the image can be found. In this case, I have used the same AppEngine application where I am hosting the Robot application.

c.  getRobotProfilePageURL(): This is a web page url where one can find more information about the Robot.

The next thing in our Robot coding is to decide which events we want to respond to. The Wave sends us events that we can subscribe to i.e. write event handlers for, so that when the event occurs, Wave will automatically invoke our method implementation and pass parameters into that method that will contain enough information for us to react to it. Let us look at the events that we are processing here.

3. We want our Robot to do is to announce itself to everyone when it is added to the Wave. This is done by overriding the onWaveletSelfAdded method. Here the implementation simply gets the wavelet from the event parameter passed and replies to it with a text message.

4. We want our Robot to send a hello/introductory message whenever the list of participants change. This is done by overriding the onWaveletParticipantsChanged method. Here we again simply reply to the wavelet with a message.

5. Finally, when anyone submits a blip (message) to the Wave, we want our Robot to get notified about it. This is done by overriding the onBlipSubmitted method. Here we extract out the content that was submitted. And then simply echo it back. But over here, you can do various kinds of processing like reacting to the command, fetching some information from the database, sending out an email, tweet, etc. The possibilities are endless. But most of the time, this is the place where you might be providing the bulk of your Robot logic.

That is all to writing a Robot but we still have some configuration to do to let Google Wave know that we have a Robot running in our GAEJ Application. And we will do that through the standard servlet entries in the web.xml

Configuring the Robot Servlet

We need to add the Robot Servlet <servlet/> and <servlet-mapping/> entry to theweb.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>WaveRobotV2</servlet-name>

<servlet-class>com.waveexperiments.robot.MyFirstGoogleWaveRobot</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>WaveRobotV2</servlet-name>

<url-pattern>/_wave/*</url-pattern>

</servlet-mapping>

In the above fragment, you will note that url-pattern /_wave/robot/jsonrpc has to be mapped to the Robot Servlet that you have written. This is because the Google Wave system will invoke this url to communicate with your Robot using its protocol.

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 waverobotv2.

To deploy the application, follow these steps (they should be familiar to you now):

  1. Click on the Deploy Icon in the Toolbar.
  2. In the Deploy dialog, provide your Email and Password. Do not click on Deploy button yet.
  3. 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 waverobotv2]
  4. 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.
  5. Finally, you should see the message “Deployment completed successfully”.

Wave Robot in Action

Your application is going to be available at the http://yourapplicationid.appspot.com. In my case, the application is available at http://waverobotv2.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 as shown below:

ep7-11

When you do that, currently you are the only participant (myself) as shown in the screen below:

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 waverobotv2@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 (with the icon and all, since the Profile Servlet is invoked behind the scenes by the Google Wave system). Since the Robot got added, it received a WAVELET_SELF_ADDED Event and since we had coded our Robot Servlet to receive that event and print out the greeting message. And it will also get the event for WAVELET_PARTICIPANTS_CHANGED i.e. it got added, so it will print the message for that also.

Next I start typing a message “Hello Robot Version 2.0 ” as shown below. But note that I have still not pressed the Done button. Till the Done button is pressed, the BLIP_SUBMITTED is not fired.


Once I click on the Done button, the BLIP_SUBMITTED event is fired and our Robot Servlet gets the event. On receiving the event, it simply echoes back the message as shown below:

That is all there is to writing a Google Wave Robot. The example here is not useful as such but the intent of the article is to make sure you get your Robot running and have the right setup in place. Several examples are available over the web in which talented programmers are writing Robots that do clever stuff. Just look around and get inspired.

Till the next episode….

Follow

Get every new post delivered to your Inbox.