As a temporary work-around for Wave slow-downs and as a method of
education, I've decided to build a quick little robot that will delete
the second blip (the one after the root blip) when the blip count is
greater than 20 and then archives the data from that blip into the end
of the root blip. This makes the root blip act as a chat-log and the
conversation can run smoothly without having the massive slow-down
that comes with lots of blips (ever tried hanging out in a public wave
without 200+ blips? it's horrendous to type). It's simple and has no
support for anything but a single reply chain.

So, I went after Python, thinking that I could learn Python whilst at
it, I spent a few hours getting used to the language and the API, and
then I set to work. I then realized a fatal flaw in my plan... There
is no support for deleting blips in the Python API. I was shocked, and
set about to correct it, since the API was in Python instead of hex
(jars are no fun). Alas, I didn't know the correct JSON-RPC message to
send to delete a blip, so I gave up on python, seeing as how Java had
a delete function.

So I move to Java, and build it again, this time with classes and
confusion and things not just working  (I fell in love with Python's
"Just work" theory). So, I was saddened, but intrepid, and completed
what should have been a successful launch. BUT, whenever I got a
"wavelet.getRootBlip()" and then tried a "getChildren()" or
"getChildBlipIds()." I got absolutely nothing, mainly in the form of
the slightly infamous "Null Pointer Exception on everything" error in
the API.

So, I either did something wrong, or there's a work-around somewhere,
or there's something else that might be able to help me. So I leave it
to you, the gurus, to help me solve my quandry. The code is posted for
both the Python and the Java versions (in their current forms).

Python (Obvious the worse of the two, it's not even close to being
complete, nor testing, but I need a way to delete blips before I can
test it or move to try to complete it):
from waveapi import events
from waveapi import model
from waveapi import robot

def OnBlipSubmitted(properties, context):
        wavesAdjusted = context.GetWavelets();
        for wave in wavesAdjusted:
                CheckLength(wave, context)

def OnRobotAdded(properties, context):
        """Invoked when the robot has been added."""
        root_wavelet = context.GetRootWavelet()
        root_wavelet.CreateBlip().GetDocument().SetText("I'm alive!")

def CheckLength(wavelet, context):
        rootBlipId = wavelet.GetRootBlipId()
        rootBlip = context.GetBlipById(rootBlipId)
        childBlipIds = rootBlip.GetChildBlipIds()
        for id in childBlipIds:
                counter = counter + 1

        archiveAmount = counter - 20;
        if archiveAmount > 0:
                for i in range(0, 5):
                        archivedBlip = context.GetBlipById(childBlipIds[i])
                        chatText = archivedBlip.GetDocument().GetText()
                        creator = archivedBlip.getCreator


if __name__ == '__main__':
        myRobot = robot.Robot('appName',
                image_url='http://appName.appspot.com/icon.png',
                version='1',
                profile_url='http://appName.appspot.com/')
        myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
        myRobot.RegisterHandler(events.WAVELET_BLIP_SUBMITTED,
OnBlipSubmitted)
        myRobot.Run()

Java (Null Pointer Exceptions, but works otherwise):

import java.util.ArrayList;
import java.util.List;
import java.io.*;

import com.google.wave.api.*;

@SuppressWarnings("serial")

public class ConnectyServlet extends AbstractRobotServlet {
        private Users users = new Users();

        private TextView createTextBlip(Wavelet wavelet) {
                return wavelet.appendBlip().getDocument();
        }
        private List<String> genOne(String user) {
                List<String> temp = new ArrayList<String>();
                temp.add(user);
                return temp;
        }
        private void checkUserCommand(Blip blip, Wavelet wavelet,
RobotMessageBundle bundle) {
                String command = 
blip.getDocument().getText().toString().toLowerCase
();
                if (command.startsWith("!managechat")) {
                        blip.delete();
                        wavelet.setTitle("~Managed Chat~");
                        wavelet.setDataDocument("chatManaged", "true");
                        processChat(wavelet, bundle);
                }
        }
        private void processChat(Wavelet wavelet, RobotMessageBundle bundle)
{
                Blip rootBlip = wavelet.getRootBlip();
                List<String> childrenIds = rootBlip.getChildBlipIds();
                List<Blip> children = new ArrayList<Blip>();
                for (String id: childrenIds) {
                        children.add(bundle.getBlip(wavelet.getWaveId(),
wavelet.getWaveletId(), id));
                }
                TextView rootDoc = rootBlip.getDocument();
                while (children.size() > 20) {
                        Blip archived = children.get(0);
                        String archivedText = archived.getDocument().getText();
                        rootDoc.append(archived.getCreator()+" at
"+archived.getLastModifiedTime()+": "+archivedText+"\n");
                        archived.delete();
                }
        }
        @Override
        public void processEvents(RobotMessageBundle bundle) {
                Wavelet wavelet = bundle.getWavelet();

                for (Event e: bundle.getEvents()) {
                        switch (e.getType()) {
                                case BLIP_SUBMITTED:
                                        checkUserCommand(e.getBlip(), wavelet, 
bundle);
                                        String managed = "";
                                        try {
                                                managed = 
wavelet.getDataDocument("chatManaged");
                                                if (managed.equals("true")) {
                                                        processChat(wavelet, 
bundle);
                                                }
                                        } catch(Exception error) {
                                        }
                                        break;
                        }
                }
        }
}

At first, I was getting a legit Null Reference on the DataDocument, so
I hacked that into working order, and I'm not sure if the recurring
check will work, since I can't get it to initialize.

The command idea is that a user creates a blip saying: "!manageChat"
and then the bot deletes that message and processes the chat, getting
the messages down to 20 and appending to the root blip what the
deleted messages said and the time that they were said.

I first tried rootBlip.getChildren(), but kept getting null errors. I
then tried rootBlip.getChildIds(), but again null errors. It's either
my code is failing, or the API is failing. Anyone got a clue on either
how to fix it?

--

You received this message because you are subscribed to the Google Groups 
"Google Wave API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-wave-api?hl=.


Reply via email to