| commit 0: | 1e4c7f873843 |
| branch: | default |
Initial commit
8 months ago
Changed (Δ14.1 KB):
raw changeset »
altlawbot.py (49 lines added, 0 lines removed)
app.yaml (11 lines added, 0 lines removed)
assets/icon.png (binary file changed)
1 |
from waveapi import events |
|
2 |
from waveapi import model |
|
3 |
from waveapi import robot |
|
4 |
from waveapi import document |
|
5 |
import logging |
|
6 |
import re |
|
7 |
||
8 |
logger = logging.getLogger('AltLawBot') |
|
9 |
logger.setLevel(logging.DEBUG) |
|
10 |
cases_re = re.compile('\[\[(.+?)\]\]') |
|
11 |
||
12 |
def OnRobotAdded(properties, context): |
|
13 |
"""Invoked when the robot has been added.""" |
|
14 |
root_wavelet = context.GetRootWavelet() |
|
15 |
root_wavelet.CreateBlip().GetDocument().SetText("Hello I am the AltLaw Bot!") |
|
16 |
||
17 |
def OnBlipSubmitted(properties, context): |
|
18 |
blip = context.GetBlipById(properties['blipId']) |
|
19 |
doc = blip.GetDocument() |
|
20 |
text = doc.GetText() |
|
21 |
left = 0 |
|
22 |
try: |
|
23 |
logger.debug('creator: %s' % blip.GetCreator()) |
|
24 |
logger.debug('text: %s' % text) |
|
25 |
except: |
|
26 |
pass |
|
27 |
cases = cases_re.findall(text) |
|
28 |
for case in cases: |
|
29 |
left = text.find(case, left) |
|
30 |
replaceRange = document.Range(left-2, left+len(case)+2) |
|
31 |
linkText = "Replaced" |
|
32 |
linkLocation = "http://www.google.com" |
|
33 |
doc.SetTextInRange(replaceRange, linkText) |
|
34 |
doc.SetAnnotation(document.Range(left-2, left+len(linkText)-2), "link/manual", linkLocation) |
|
35 |
newBlip = doc.AppendInlineBlip() |
|
36 |
newBlip.GetDocument().SetText(linkLocation) |
|
37 |
#text = text.replace(text[replaceRange.start:replaceRange.end], linkText, 1) |
|
38 |
#left += len(linkTxt) |
|
39 |
||
40 |
if __name__ == '__main__': |
|
41 |
myRobot = robot.Robot('AltLawBot', |
|
42 |
image_url='http://altlawbot.appspot.com/assets/icon.png', |
|
43 |
version='2.06', |
|
44 |
profile_url='http://altlawbot.appspot.com/') |
|
45 |
myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded) |
|
46 |
myRobot.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmitted) |
|
47 |
myRobot.Run(debug=True) |
|
48 |
||
49 |
1 |
application: altlawbot |
|
2 |
version: 1 |
|
3 |
runtime: python |
|
4 |
api_version: 1 |
|
5 |
||
6 |
handlers: |
|
7 |
- url: /_wave/.* |
|
8 |
script: altlawbot.py |
|
9 |
- url: /assets |
|
10 |
static_dir: assets |
|
11 |
Up to file-list assets/icon.png:
