TUTORIAL BÁSICO GOOGLE WAVE BOTS
A lo
largo de este tutorial crearemos un robot muy simple, que al
invitarlo a un Wave sustituye una tag por la fecha y hora actual.
Comenzaremos
creando un proyecto como se explicó en el tutorial anterior
“TUTORIAL BÁSICO GOOGLE APPLICATION ENGINE”.
Luego
añadiremos a la carpeta /war/WEB-INF/lib del proyecto los
ficheros: wave-robot-api.jar, json.jar y jsonrpc.jar que bajaremos de
http://code.google.com/p/wave-robot-java-client/downloads/list
y lo añadiremos a las libraries del proyecto mediante el menú
Project/properties…/Java Build Path/Libraries/Add jars…
En la carpeta /war crearemos una carpeta _wave para añadir dos ficheros:
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <wagent-profile></wagent-profile> y 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="document_changed" content="true" /> </w:capabilities> <w:version>1</w:version> <w:profile name="RobieBot" imageurl="/robiebot.png" profileurl="/_wave/profile.xml" /> </w:robot> |
donde
vemos que se informa este bot atenderá el evento
document_changed y cuestiones de profile (nombre, logo, descriptor).
Añadimos entonces la imagen y el index.html en el /war.
Vamos a
actualizar el web.xml como vemos a continuación:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>Robiebot</servlet-name> <servlet-class>com.robiebot.RobiebotServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Robiebot</servlet-name> <url-pattern>/_wave/robot/jsonrpc</url-pattern> </servlet-mapping> <servlet> <servlet-name>profile</servlet-name> <servlet-class>com.robiebot.RobiebotProfileServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>profile</servlet-name> <url-pattern>/_wave/robot/profile</url-pattern> </servlet-mapping> </web-app> |
El
servlet principal será Robiebot que atenderá la ruta
estándar /_wave/robot/jsonrpc y el RobiebotProfileServlet
responderá con información de profile también en
una ruta estándar para los bots de Wave.
Veamos
los servlets:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.robiebot; import com.google.wave.api.ProfileServlet; /** * Perfil del robot * * @author jantoniucci * */ public class RobiebotProfileServlet extends ProfileServlet { private static final long serialVersionUID = 2130048805501433709L; @Override public String getRobotAvatarUrl() { return "http://robiebot.appspot.com/robiebot.png"; } @Override public String getRobotName() { return "RobieBot (por ahora sólo una demo para un tutorial de Autentia)"; } @Override public String getRobotProfilePageUrl() { return "http://robiebot.appspot.com/index.html"; } } |
Este
servlet es sencillo, sólo implementa una interfaz
ProfileServlet para resolver la info que el bot entregará a
Wave para que el usuario vea el logo, descripción, etc.
Y ahora
el servlet principal:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
package com.robiebot; import java.text.DateFormat; import java.util.Date; import com.google.wave.api.AbstractRobotServlet; import com.google.wave.api.Blip; import com.google.wave.api.Event; import com.google.wave.api.EventType; import com.google.wave.api.Range; import com.google.wave.api.RobotMessageBundle; import com.google.wave.api.TextView; public class RobiebotServlet extends AbstractRobotServlet { private static final long serialVersionUID = 2666513176199262614L; private static final String TAG_NOW = "<rb:now/>"; @Override public void processEvents(RobotMessageBundle bundle) { if (bundle.wasSelfAdded()) { final Blip blip = bundle.getWavelet().appendBlip(); final TextView textView = blip.getDocument(); textView .append("Hola amigosss!)"); } else { for (Event event : bundle.getEvents()) { if (event.getType() == EventType.DOCUMENT_CHANGED) { TextView document = event.getBlip().getDocument(); if (document.getText().contains(TAG_NOW)) { final int inicioTag = document.getText().indexOf(TAG_NOW); document.replace(new Range(inicioTag, inicioTag + TAG_NOW.length()), DateFormat.getInstance() .format(new Date())); } } } } } } |
Vamos a
analizarlo: implementamos AbstractRobotServlet
un que ya tiene un método para interceptar eventos, que vienen
informados en bundle. Con el bundle.wasSelfAdded()
el Wave nos informa que se acaba de añadir a nuestro bot en un
Wave. Si no fuera el caso
buscamos que si se ha producido un EventType.DOCUMENT_CHANGED
y el texto del documento contiene el tag <rb:now/>
generamos un replace en el documento.
Sólo
nos queda publicarlo e invitarlo a un wave:
Y
entonces genera el blip con el saludo y si escribimos el tag lo
sustituye por la fecha/hora actual.
Con esto
sólo estamos viendo el primer milímetro del ovillo
waves.
¿por
dónde seguir?
-
Tutorial
oficial
http://code.google.com/apis/wave/extensions/robots/java-tutorial.html
-
Blog
GAE Experiments http://gaejexperiments.wordpress.com/
-
Blog
MasteringWave http://www.masteringwave.com/
-
Blog
Google Wave Developer http://googlewavedev.blogspot.com/
-
Presentaciones
oficiales del Google IO 2009
http://code.google.com/events/io/2009/sessions/ProgrammingWithAndForGoogleWave.html
Otros
links interesantes:
-
Introducción
para usuarios http://mashable.com/2009/05/28/google-wave-guide/
-
Guía
de uso no-oficial http://completewaveguide.com/ -
Lista
no-oficial de extensiones y widgets
http://sites.google.com/site/gwaveextensions/extensions-list
-
Robots
de ejemplo oficiales http://wave-samples-gallery.appspot.com/
Que
aproveche!