Skip to content

Instantly share code, notes, and snippets.

@holtwick
Created January 7, 2010 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holtwick/271195 to your computer and use it in GitHub Desktop.
Save holtwick/271195 to your computer and use it in GitHub Desktop.
# -*- coding: UTF-8 -*-
# Copyright 2009 Dirk Holtwick
# http://www.holtwick.it
VERSION = '1'
import logging
from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document
import os
import cgi
import urllib
import random
import base64
import wsgiref.handlers
import re
from google.appengine.ext import db
from google.appengine.api import xmpp
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from django.utils import simplejson
from model import *
from push import sendPushNotification
### Push
def pushNotification(user, title, waveid, author, change, count):
for xdevice in XDevice.all().filter("user = ", user):
# Empty?
if not change.strip():
logging.info("Empty message, skip push")
return
# Compose alert message
alertMsg = '%s\n@%s\n%s' % (
title,
author.split("@")[0],
" ".join(change.strip().split()))
if len(alertMsg) > 100:
alertMsg = alertMsg[:100] + '...'
body = ""
sendPushNotification(
xdevice.devicetoken,
dict(aps = {
"alert": alertMsg,
"sound" : "w.caf",
}),
production = xdevice.isproduction,
async = True)
### Events
_rxVerifyKey = re.compile("\[waveboard\-([^\]]+)\]", re.M | re.I | re.DOTALL)
def OnBlipSubmitted(event, context):
properties = event.properties
logging.info("OnBlipSubmitted")
# properties = event.properties
logging.info("Properties: %r", properties)
xwave = None
root = context.GetRootWavelet()
if root:
waveid = root.GetWaveId()
# Wave entry
xwave = getWaveById(waveid)
if xwave:
# Update Title
title = root.GetTitle()
if title <> xwave.title:
xwave.title = title
xwave.counter += 1
xwave.put()
else:
logging.warn("Root Wavelet has not been found!")
# Get blips text
doc = context.GetBlipById(properties['blipId']).GetDocument()
modifiedBy = event.modifiedBy
text = doc.GetText()
logging.info("Blip Text: %r", text)
# Users to validate?
didVerify = False
for key in _rxVerifyKey.findall(text):
logging.info("Testing for verification key %r > %r", key, event.modifiedBy)
for xuser in XUser.gql("WHERE question=:1", key):
wuser = event.modifiedBy
if wuser not in xuser.waveaddresses:
logging.info("User %r", wuser)
xuser.isactive = True
xuser.waveaddress = wuser
if xuser.waveaddresses:
xuser.waveaddresses = list(xuser.waveaddresses) + [wuser]
else:
xuser.waveaddresses = [wuser]
xuser.question = uid()
xuser.put()
msg = "Thank you! Waveboard Robot has verified your account with the address '%s'" % wuser
doc.SetText(msg)
didVerify = True
# Something to push
if xwave and not didVerify:
for xnotification in XNotification.all().filter("waveid = ", xwave.waveid):
if (xnotification.waveaddress in xwave.participants) and (
xnotification.waveaddress not in (APPID + "@appspot.com", modifiedBy)):
logging.info("PUSH to %s", xnotification.waveaddress)
pushNotification(
xnotification.user,
xwave.title,
xwave.waveid,
modifiedBy,
text[:500],
"count")
def OnRobotAdded(event, context):
properties = event.properties
logging.info("OnRobotAdded")
root = context.GetRootWavelet()
waveid = root.GetWaveId()
title = root.GetTitle()
logging.info("ID %r Title %r", id, title)
participants = root.GetParticipants()
if participants:
participants = list(participants)
else:
participants = []
logging.info("Participants %r", participants)
xwave = getWaveById(waveid)
if xwave is None:
xwave = XWave(
waveid=waveid,
)
xwave.isactive = True
xwave.title = title
xwave.participants = participants
xwave.put()
msg = '\nPress here to configure Waveboard iPhone Push Notifications '
doc = context.GetRootWavelet().CreateBlip().GetDocument()
doc.SetText(msg)
doc.SetAnnotation(document.Range(1, len(msg) - 1),
"link/manual",
"http://%s/?waveid=%s" % (HOST, waveid))
def OnRobotRemoved(event, context):
properties = event.properties
logging.info("OnRobotRemoved")
root = context.GetRootWavelet()
waveid = root.GetWaveId()
title = root.GetTitle()
logging.info("ID %r Title %r", id, title)
xwave = getWaveById(waveid)
if xwave is not None:
for xnotification in XNotification.all().filter("waveid = ", xwave.waveid):
xnotification.delete()
xwave.delete()
def OnParticipantsChanged(event, context):
properties = event.properties
logging.info("OnParticipantsChanged %r", properties)
root = context.GetRootWavelet()
waveid = root.GetWaveId()
xwave = getWaveById(waveid)
if xwave:
participants = root.GetParticipants()
if participants:
participants = list(participants)
else:
participants = []
logging.info("Participants %r", participants)
xwave.participants = participants
xwave.put()
### Setup
if __name__ == '__main__':
botInstance = robot.Robot(
name="Waveboard Push Robot",
version=VERSION,
image_url='http://%s/static/pushicon.png' % HOST,
profile_url='http://www.getwaveboard.com/push/')
botInstance.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
botInstance.RegisterHandler(events.WAVELET_SELF_REMOVED, OnRobotRemoved)
botInstance.RegisterHandler(events.WAVELET_PARTICIPANTS_CHANGED, OnParticipantsChanged)
botInstance.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmitted)
botInstance.Run([])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment