Create your First Google Wave Robot with Python in 15 Minutes

In this post you’ll discover how to create your first Google Wave Robot. This tutorial is aimed at complete beginners and should take no more then 15 minutes. By the end of the tutorial you will have a simple Google Wave Robot.

What you will need:

There are a few things that we need to configure, download and sign up for before we can get started.

Python 2.5 or higher

Windows
Before we get started, you’ll need to check that you have got Python 2.5 or higher installed on your system. Click Start –> All Programs and look for “Python X.X”. X.X is your version number. If you cannot find it you do not have python installed on your system. Python can be downloaded from here

Mac  & Linux
Before we get started, you’ll need to check the you have got Python 2.5 or higher installed on your system. Open terminal and type the following command:

python –version

If there version number is less then 2.5, you’ll need to update.

Google App Engine Account

We need somewhere to upload our robot. Currently Google Wave only allow developers to use the Google App Engine – sign up for an account here

Next we have to create a new application. Sign in and click the big “Create an Application” button. Under Application Identifier give your robot a name. Be mindful that this will be the Wave address of your robot.

Google App Engine SDK

The Google App Engine SDK is the program we use to upload our robot to the Google App Engine. You can find the installation files here.

Google Wave Python Client Library

The Google Wave Python Client Library can be downloaded here. We will be using this in a future step.

Setting up our project folder

Create a folder where you would like to store all of your files. The folder you just created will be referred to the project folder for the rest of the tutorial.

App.yaml

App.yaml is the configuration file that the Google App Engine SDK uses. Create a file name App.yaml and paste the following code.

application: appname
version: 1
runtime: python
api_version: 1
- url: /_wave/.*
  script: main.py
- url: /assets
  static_dir: assets

Replace appname (line 1) with the Application Identifier you used when creating your application.

Google Python Client Library

Create a folder in our project folder named waveapi. Extract the contents from the Google Python Client Library into this directory.

Project Folder Overview

Create a blank file named main.py. This is where our main code will go in the next step Let’s now just quickly confirm that your project folder looks like mine. googlewaveprojectfolder1_thumb[1]

If your project folder looks like mine then great, otherwise try and correct it.

Let’s get into some CODING!

Time for the interesting part of the tutorial! This section is spit up into two sections, one is the code that you can copy and paste into main.py and the second is the explanation of the code.

Code

from waveapi import events
from waveapi import events
from waveapi import model
from waveapi import robot
 
def OnRobotAdded(properties, context):
  """Invoked when the robot has been added."""
  root_wavelet = context.GetRootWavelet()
  root_wavelet.CreateBlip().GetDocument().SetText("I'm alive!")
 
if __name__ == '__main__':
  myRobot = robot.Robot('myFirstRobot',
      image_url='http://corythompson.net/wp-content/uploads/CoryThompson.jpg',
      version='1',
      profile_url='http://corythompson.net/')
  myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
  myRobot.Run()

Explanation

Let’s break down this code to make sure we understand what is going on.

from waveapi import events
from waveapi import model
from waveapi import robot

The above code just includes the google wave python library which is needed in creating your robot.

if __name__ == '__main__':
  myRobot = robot.Robot('myFirstRobot',
      image_url='http://corythompson.net/wp-content/uploads/CoryThompson.jpg',
      version='1',
      profile_url='http://corythompson.net/')
  myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
  myRobot.Run()

This code just defines our main function, the reason it is at the bottom of the file is so we can refer to above functions. It starts by creating the robot object with all the profile information, it then registers the event handler “WAVELET_SELF_ADDED” and on that event runs the function OnRobotAdded.

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

This is the function that is ran on “WAVELET_SELF_ADDED”. It’s just create a blip with the text “I’m Alive!”

Uploading and Testing the Application

In the previous step we put the code into main.py and it’s ready for uploading and testing.

Uploading the Code

Windows and Mac
If you haven’t already installed the Google App Engine SDK, do it now! It’s a simple next-next-finish type installation. Open the “Google App Engine SDK Launcher” and click “File -> Add Existing Application”. Browse to your project folder and leave the port as default.

You should now see your application name in the list. Select it and click deploy – you will be prompted for your username and password for your Google App Engine account. Type in your username and password and watch your code being uploaded.

Linux
Extract the Google App Engine SDK file. Now open terminal and browse to where you extracted the files using cd. Type the following command:

python appcfg.py update projectfolder

Replace projectfolder with your project folder. For example mine is “/home/cory/Programming/googlewaveexample/”. You will be prompted for your username and password and then your code will be uploaded.

Testing your application

To test our Google Wave Robot, all we need to do is add a new contact. Click the add button in Google Wave (as shown on right) and type in appname@appspot.com where appname is the Application Identifier Create a new wave and add your robot (name should be “myfirstrobot”). Once added to a Wave it will say “I’m Alive!”

Finishing Up

Congratulations, you have created your first Google wave robot. Please please comment and give me any feedback,errors, anything.

More information and extending

In the next couple of days I will be posting on how to make an emailbot in Google Wave and looking at Google Wave in more detail. If these interest you sign up and get notified once they are posted.

2 Responses to “Create your First Google Wave Robot with Python in 15 Minutes”

  1. Hugh Powell says:

    Nice tutorial, its been a while since i used App Engine. As far as im aware of the windows version of the SDK doesn’t come with the GUI launcher, you post made me have southern look and i found this

    http://code.google.com/p/google-appengine-wx-launcher/

    Its irritatingly only available as SNV so i also downloaded an compiled it

    http://www.importsoul.net/python/google-app-engine-launcher/

    Enjoy and keep up the good posts

  2. [...] tutorial is an extension onto my tutorial “Create your First Google Wave Robot with Python in 15 Minutes“. Although this is not necessary to read to complete this tutorial, It will be [...]

Leave a Reply