Home > Google, Java, Open Source > Google Wave Robots: A Pictured Java Tutorial

Google Wave Robots: A Pictured Java Tutorial

September 24th, 2009


This post is a reference to Google’s own Java Tutorial for Google Wave Robots with an example of robot code that performs word replacement.

In my previous post, I have also briefly explained what Google Wave is about.

What you need to run a simple Java Wave Robot.

1) Eclipse IDE + JDK 6
2) Google Wave Java Client libraries
3) Google App Engine Account

Yes, you only require 3 items but most importantly, to access Google Wave, you need a Google Wave Id.

1) Eclipse IDE + JDK 6

Download Eclipse IDE here : http://www.eclipse.org/downloads/

eclipse_download

Download JDK 6 here : http://java.sun.com/javase/downloads/index.jsp

jdk6_download

Install the Google Plugin and the App Engine SDK:

eclipse_update

Depending on the version of Eclipse IDE you downloaded, you will need to follow Google Wave’s guide to update using either one of the following URL:
http://dl.google.com/eclipse/plugin/3.5
or
http://dl.google.com/eclipse/plugin/3.4

eclipse_update_site

2) Google Wave Java Client libraries
Download the Java client libraries from http://code.google.com/p/wave-robot-java-client/downloads/list
Jar files required are :

  • wave-robot-api-.jar
  • json.jar
  • jsonrpc.jar

Save those files to your project library under \WEB-INF\lib

java_wave_client_lib

3) Code your Wave Robot

You can use Google Wave Java Tutorial source code directly if you want to, but I figure that I need to make some difference and hence thought of a simple words replacement robot.

hack1

hack2

My customization is highlighted in bold, you would want to change it with your own code.

Following is my source code of MarvyServlet (pardon me for the profanity in the source code, but this is a profanity filter robot afterall 🙂 )

package net.marvinlee.marvy;

import com.google.wave.api.*;

public class MarvyServlet extends AbstractRobotServlet {

      @Override
      public void processEvents(RobotMessageBundle bundle) {
        Wavelet wavelet = bundle.getWavelet();

        if (bundle.wasSelfAdded()) {
          Blip blip = wavelet.appendBlip();
          TextView textView = blip.getDocument();
          textView.append("I'm here to ensure you have a nice and polite wave.");
        }

        for (Event e: bundle.getEvents()) {

          if (e.getType() == EventType.WAVELET_PARTICIPANTS_CHANGED) {
            Blip blip = wavelet.appendBlip();
            TextView textView = blip.getDocument();
            textView.append("Hi, everybody! I will be censoring profanities in your conversation.");
          }

          if (e.getType() == EventType.BLIP_SUBMITTED) {
            Blip blip = e.getBlip();
            TextView textView = blip.getDocument();
            String blipText = textView.getText();
            textView.delete();
            textView.append(replaceProfanity(blipText));
          }
        }
      }

        private static String replaceProfanity(String text)
        {
            String[] vulgars = {"shit", "damn", "fark"};
            String[] vulgarReplacement = {"sh#t", "darn", "f##k"};
            String niceText = "";

            System.out.println("Input:" + text);
            for (int i=0; i< vulgars.length; i++)
            {
                String key = vulgars[i];
                if (text.indexOf(key) > 0)
                {
                    text = text.replaceAll(vulgars[i], vulgarReplacement[i]);
                    System.out.println(vulgars[i] + " replaced with " + vulgarReplacement[i] );
                }

                niceText = text;
            }

            System.out.println("Output:" + niceText);
            return niceText;
        } 

    }

capabilities.xml file under _wave

<?xml version="1.0" encoding="utf-8"?>
<w:robot xmlns:w="http://wave.google.com/extensions/robots/1.0">
  <w:capabilities>
    <w:capability name="WAVELET_PARTICIPANTS_CHANGED" content="true" />
    <w:capability name="BLIP_SUBMITTED" content="true" />
  </w:capabilities>
  <w:version>1</w:version>
</w:robot>

web.xml under WEB-INF

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>Marvy</servlet-name>
        <servlet-class>net.marvinlee.marvy.MarvyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Marvy</servlet-name>
        <url-pattern>/_wave/robot/jsonrpc</url-pattern>
    </servlet-mapping>
</web-app>

appengine-web.xml – marvinleebot is my application name, and is unique in Google App Engine.
If you do not have a Google App Engine yet, follow step 4) below and come back again.

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>marvinleebot</application>
    <version>1</version>

    <!-- Configure java.util.logging -->
    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
    </system-properties>

</appengine-web-app>

You can make other changes by viewing the Wave JavaDoc.

4) Deploy to Google App Engine
You will need to create an application in your Google App Engine first if you have not.

create_app_engine

You might also need to verify your account with a mobile number, just follow through the steps.
Create your app here : https://appengine.google.com/start
Your app name has to be unique so take some time to figure one or if you’re like me, just a lame bot name like marvinleebot might do the trick.

Once you’re done with your app name and created successfully, make sure that your appengine-web.xml file contains your own app name.

<application><your_app_name_here></application>

When you’re ready, just hit the Deploy App Engine Project button.

eclipse_google_plugin

Submit your App Engine Account Email and Password.

deploy_app_engine

5) Test your Wave Robot

As I’ve mentioned earlier, the single most important thing for it to work is to have a Google Wave Id.

If you have one, you can login Wave Sandbox at http://wavesandbox.com/.

wave_sandbox_login

If you do not have a Google Wave Id yet, request one here.

Create a new wave, and then add the bot you have created by adding a participant : <your_app_name>@appspot.com

This is how the bot looks like and how my marvinleebot worked (click to enlarge). I was using a test Wave Id from Nazroll.

wave_sandbox

You can also monitor your bot/app from your App Engine dashboard.
app_engine

 Follow me on twitter.





Google, Java, Open Source

, , , , ,

You might like the following posts too :

  • » Continuous Enterprise Development in Java by Andrew Lee Rubinger, Aslak Knutsen
  • » Running JUnit tests in Maven with Surefire Plugin and skipTests in default packaging
  • » Quick Guide to JUnit multiple parameter tests with Parameterized annotation
  • » Getting Started with OpenShift - easy reference by Steven Pousty & Katie J. Miller
  • » Java Cookbook, 3rd Edition by Ian F. Darwin
    1. September 24th, 2009 at 22:14 | #1

      Hey Great Tutorial..
      Step by step with screenshots 🙂
      Thanks for the tutorial. I have bookmarked. Although I have not got access to Google Wave.

    2. Vincent
      October 22nd, 2009 at 09:01 | #2

      Thanks for the step-by-step tutorial. This is something I’m looking for. Keep up the good work!

    1. November 5th, 2009 at 20:01 | #1