Lions and tigers, and Pythons and Rabbits, oh my!

We’ve recently been doing some great work on projects using Google App Engine, a highly scalable framework for building web apps in Python or Java. We’ve posted before about our friendly talking robot rabbit, Nabaztag. I thought it would be fun (and vaguely useful) to make our rabbit announce when new code was pushed to our repositories on GitHub.

GitHub has a feature called post-receive hooks which you can set up for any repository. You can provide a URL which will receive an HTTP POST with a JSON object describing details of the pushed commits. Here’s an example payload:

  "before": "5aef35982fb2d34e9d9d4502f6ede1072793222d",
  "repository": {
    "url": "",
    "name": "github",
    "description": "You're lookin' at it.",
    "watchers": 5,
    "forks": 2,
    "private": 1,
    "owner": {
      "email": "",
      "name": "defunkt"
  "commits": [
      "id": "41a212ee83ca127e3c8cf465891ab7216a705f59",
      "url": "",
      "author": {
        "email": "",
        "name": "Chris Wanstrath"
      "message": "okay i give in",
      "timestamp": "2008-02-15T14:57:17-08:00",
      "added": ["filepath.rb"]
      "id": "de8251ff97ee194a289832576287d6f8ad74e3d0",
      "url": "",
      "author": {
        "email": "",
        "name": "Chris Wanstrath"
      "message": "update pricing a tad",
      "timestamp": "2008-02-15T14:36:34-08:00"
  "after": "de8251ff97ee194a289832576287d6f8ad74e3d0",
  "ref": "refs/heads/master"

I used the Nabazlib library by Ricardo Varela to deal with communicating with the Nabaztag. One of the nice things about Python is there’s almost always a package for whatever you’re trying to achieve!

I also chose to use Google’s WebApp framework. This makes the app really simple! Here’s app.yaml:

application: nabazhook
version: 1
runtime: python
api_version: 1

- url: .*

Here’s the The constructor to webapp.WSGIApplication accepts a list of url patterns. This maps the URL / to the Hook class. Then I defined a method post on the class which will be called whenever GitHub POSTs data to the URL.

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from django.utils import simplejson as json

from nabazlib.Nabaztag import Nabaztag

class Hook(webapp.RequestHandler):

    def post(self):

        #get the credentials for the nazaztag
        u = self.request.get("id")
        p = self.request.get("secret")

        #get the JSON payload. this is stored in the POST variable "payload"
        jsonstr = self.request.get("payload")

        #parse it
        obj = json.loads(jsonstr)

        #read out some interesting data
        reponame = obj['repository']['name']
        author = obj['commits'][0]['author']['name']
        msg = obj['commits'][0]['message']
        numcommits = len(obj['commits'])

        #construct the string we want to send to the rabbit
        txt = "Git alert! Git alert! %s just pushed %d commits to %s" %(author,numcommits,reponame)

        #construct the nabaztag object and send the text
        myNabaztag = Nabaztag(u,p)

application = webapp.WSGIApplication([
    ('/', Hook)
], debug=True)

def main():

if __name__ == '__main__':

I deployed a test version of the app to – so by adding a post-receive URL to any repository like this, I can make Nabaztag talk.

And that’s all there is to it! The code is available on GitHub if you want to play around with it. If you have any more suggestions of things to do with Nabby, do share them in the comments…

Matt Mayer

Matt Mayer is a founder at ReignDesign. Matt is from the UK and was based in Shanghai for ten years. He is now living in Bangkok, Thailand.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this post