Developer42

2009-11-03

My First Wave Robot :: smiley-wave@appspot.com

Filed under: Google, Technology, Wave — Tags: , , , , , , , — Developer42 @ 00:34

smiley-wave@appspot.com first demo

appengine-web.xml

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

web.xml

<?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>Smiley</servlet-name>
		<servlet-class>inverso.google.wave.robot.smiley.SmileyServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Smiley</servlet-name>
		<url-pattern>/_wave/robot/jsonrpc</url-pattern>
	</servlet-mapping>
</web-app>

capabilities.xml

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

SmileyServlet.java

package inverso.google.wave.robot.smiley;
import com.google.wave.api.*;
import java.util.*;
		
//FIX FOR ISSUE 354 is used where the robot suffers from the bug mentioned in the below links
//http://code.google.com/p/google-wave-resources/issues/detail?id=373
//http://code.google.com/p/google-wave-resources/issues/detail?id=354
		
public class SmileyServlet extends AbstractRobotServlet {
		
       /**
	 * Robot's version id & usage info
	 */
	private static final long serialVersionUID = 1L;
	private static final long serialSubVersionUID = 2L; //this is to help me prove that my latest changes 
                                                            //have been uploaded correctly
	private static final String serialVersionID = "Version #" + new Long(serialVersionUID).toString() + "." + 
                                                      new Long(serialSubVersionUID).toString();
	private static final String helpInfo = "\nThis robot is my learning tool for wave development, " +
                                               "so may occasionally break!  Apologies if you're one of " +
                                               "the people for whom it doesn't work!";
	private static HashMap dictionary = null;
		
	/*
	 * Creates the images & registers them against their invoking smileys 
	 * */	
	static {
		final int width = 24;
		final int height = 24;
		//http://commons.wikimedia.org/wiki/Smiley
		dictionary = new HashMap();
		dictionary.put(":)",new Image("http://upload.wikimedia.org/wikipedia/commons/7/79/Face-smile.svg",width,height,":)"));
		dictionary.put(":(",new Image("http://upload.wikimedia.org/wikipedia/commons/0/06/Face-sad.svg",width,height,":("));
		dictionary.put(";)",new Image("http://upload.wikimedia.org/wikipedia/commons/5/57/Face-wink.svg",width,height,";)"));
		dictionary.put(":D",new Image("http://upload.wikimedia.org/wikipedia/commons/1/15/Face-smile-big.svg",width,height,":D"));
	}
	
   /**
     * Handles changes to the wave 
     **/
	@Override 
	public void processEvents(RobotMessageBundle bundle) {		
		
		if (bundle.wasSelfAdded()) {
			final Blip blip = bundle.getWavelet().appendBlip();
			blip.getDocument().delete(); //FIX FOR ISSUE 354
			final TextView textView = blip.getDocument();
			textView.append( serialVersionID );
			textView.append( helpInfo );
		}
		
		for (Event e: bundle.getEvents()) {
			if ((e.getType() == EventType.BLIP_SUBMITTED) || (e.getType() == EventType.WAVELET_SELF_ADDED)) {
				final Blip blip = e.getBlip();
				smile(blip.getDocument());
			}
		}
	    
	}
	
	private void smile(TextView textView){
		final String text = textView.getText();
		Iterator<Map.Entry> i = dictionary.entrySet().iterator();
		while(i.hasNext()){
			Map.Entry pic = i.next();
			String key = pic.getKey();
			int pos = -1;
			while((pos = text.indexOf(key,++pos))>-1){
				textView.delete(new Range(pos, pos + key.length()));
				textView.insertElement(pos,pic.getValue());
			}
		}	
	}
		
}

Advertisements

5 Comments »

  1. One of the guys in Facebook’s Google Wave Developers Group kindly pointed out that I’ve not used serialVersionUID correctly in the above code, and has provided information on its correct usage (here). Before my next revision I’ll read up a bit more on what this field’s for and either correct or remove it; initially I’d only added this to prevent a warning from popping up in Eclipse, so skimmed the documentation, saw it was some form of version id, and ignored the rest of the explanation.
    Thanks to Ugljesa for pointing this out to me.

    Comment by Developer42 — 2009-11-10 @ 22:44

  2. If you make a smiley robot that you can customize (Put images you want) that will be used by me 😀

    Comment by Janko — 2009-12-01 @ 15:28

    • Hey Janko,

      good shout. . . I’ll need to figure out how to keep session data per user, but am pretty sure that’s possible, so should have something here soon. I’ll drop you an email when available :). In the meantime, there are other emoticon bots which you may wish to try (I haven’t tested these out, so not sure if they allow you to add your own images, or what emoticons they support). . . a list of these is available in the See Also section at the bottom of this linked page.

      It may also be good to allow community suggested features to bubble into use somehow (though putting in some kind of peer review ability to avoid bloating the library of emoticons / using copyrighted images). . . if anyone has any suggestions on a good way to incorporate those features, I’d welcome your thoughts.

      Comment by Developer42 — 2009-12-01 @ 20:35

      • Actually I was going to copy your code and put my own smileys, but I thought it would be interesting to make a customizable bot 🙂

        Comment by Janko — 2009-12-02 @ 16:04


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: