-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·477 lines (446 loc) · 23 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# main.py
# Imports the modules required for robot operation
from waveapi import robot
from waveapi import events
from waveapi import appengine_robot_runner
# Imports the element module which allows the robot
# to work with elements
from waveapi import element
# Imports the logging module to send debug logs to
# the appengine logger.
import logging
# Imports the appengine datastore module, allowing
# searching of the datastore.
from google.appengine.ext import db
# Imports the local classes module, which model
# the creation of Main Waves & User Waves in
# the datastore
import classes
# "Placeholders" to stop errors
ROOT_WAVE = True
INDEX_WAVE = True
BUTTON_WAVE = True
myRobot = True
# Gets the "Main Waves" from the datastore
query = db.GqlQuery('SELECT * FROM MainWave')
for i in query:
if i.wave_type == 1:
ROOT_WAVE = i
elif i.wave_type == 2:
INDEX_WAVE = i
elif i.wave_type == 3:
BUTTON_WAVE = i
# Hard Codes the "helpful panel"
PARTICIPANTS = ['[email protected]',
def create_question_wave(q_wave):
'''Appends the required elements & text to
make the question wave.'''
# Sets the title
q_wave.title = 'New Helpdesk Question'
# Adds a "Heading 3" element
q_wave.root_blip.append(element.Line(line_type = 'h3'))
# Adds some instructions
q_wave.root_blip.append('Type a summary of ' +
'your question in ' +
'the box below.\n')
# Adds an Input element, for people to type
# their question summary into.
q_wave.root_blip.append(element.Input('wave-helpdesk:InsertQuestion', ''))
# Adds another Heading element
q_wave.root_blip.append(element.Line(line_type = 'h3'))
# Adds some more instructions
q_wave.root_blip.append('If you have more detail to add, add it here (optional):\n')
# Adds a TextArea element, for question detail. TextArea
# elements expand when more text than fits the box is
#entered. This behavior does not appear in the Input element.
q_wave.root_blip.append(element.TextArea('helpdesk/detail', ''))
# Adds another heading element.
q_wave.root_blip.append(element.Line(line_type = 'h3'))
# Adds some more instructions
q_wave.root_blip.append('Once you are done, click this button:\n')
# Adds the submit button.
q_wave.root_blip.append(element.Button('helpdesk-submitquestion',
'Submit Question'))
q_wave.root_blip.append('\n\nIf you would like to include screenshots/attachments, go into \'edit\' mode (click the "edit" button on the toolbar), then attach the file/screenshot that you would like to use.\n\n')
# Adds a random helpful message.
import random
x = random.choice((1,2,3))
rand_texts = {1:('You might want to check our list of all previous questions: ',
'Helpdesk Index',
('link/wave', INDEX_WAVE.wave_id)),
2:('Think you make good helpdesk material? Ask to join ',
'our group.',
('link/manual', 'http://groups.google.com/group/wave-helpdesk')),
3:('You can install some searches that will let you find all the helpdesk questions, discussions and main waves ',
'here.',
('link/wave', ROOT_WAVE.wave_id)),
}
q_wave.root_blip.append(rand_texts[x][0], [('style/fontStyle', 'italic')])
q_wave.root_blip.append(rand_texts[x][1], [rand_texts[x][2],
('style/fontStyle', 'italic')])
q_wave.root_blip.append(' ', [('link/wave', None),
('style/fontStyle', None)])
q_wave.data_documents['helpdesk-wavetype'] = 'question'
return q_wave
def OnFormButtonClicked(event, wavelet):
'''Handles FormButtonClicked events. '''
# Logs the button name in the appengine logs.
logging.debug('OnFormButtonClicked Called, Button Name %s' % event.button_name)
# Assigns the wavelet's operation queue to opQ for easier access.
opQ = wavelet.get_operation_queue()
# If the button is the 'new question' button:
if event.button_name == 'helpdesk-newquestion':
# Logs that the button is the new question button.
logging.debug('New Question Wave')
# Creates a new wave.
q_wave = myRobot.new_wave(wavelet.domain,
participants = [event.modified_by],
submit = True)
# Calls "create_question_wave" on the new wave (see the function above)
create_question_wave(q_wave)
# Submits the question wave using the active api.
myRobot.submit(q_wave)
# If the button is the helpdesktesting button:
elif event.button_name == 'helpdesktesting-newquestion':
# Does the same as above, but the 3 steps are combined into one.
myRobot.submit(create_question_wave(myRobot.new_wave(wavelet.domain,
participants = [
event.modified_by],
submit=True)))
# if the button is the 'add me' button on the helpdeskdev wave:
elif event.button_name == 'helpdesk-dev-add_me':
# Gets the wave_id
wave_id_box = event.blip.first(element.Input, name = 'Wave Add')
# if that was sucsessful:
if wave_id_box:
# extracts the wave id from the box.
wave_id = wave_id_box.value().serialize()['properties']['value']
# If that was unsucsessful:
if not wave_id:
# end running code
return
else:
# end running code
return
# tries to find the wavelet_id input box.
wavelet_id_box = event.blip.first(element.Input, name = 'Wavelet ID')
# If it found the box:
if wavelet_id_box:
# Extract the wavelet id from the box.
wavelet_id = wavelet_id_box.value().serialize()['properties']['value']
# if it couldn't find the box: extracts the wavelet id from the wave id.
else:
#extract the wavelet id from the wave id
wavelet_id = wave_id.split('!')[0] + '!conv+root'
# The following 8 lines attempt to add the person who asked to the wave
# and make them full access.
try:
wavelet.get_operation_queue().wavelet_add_participant(wave_id, wavelet_id, event.modified_by)
except:
wavelet.root_blip.append('\nOperation Failed')
try:
wavelet.get_operation_queue().wavelet_modify_participant_role(wave_id, wavelet_id, event.modified_by, 'FULL')
except:
wavelet.root_blip.append('\nOperation Failed')
# TODO: Comment from here downwards.
elif event.button_name == 'helpdesk-dev-add_them':
wave_id_box = event.blip.first(element.Input, name = 'Wave Add')
if wave_id_box:
wave_id = wave_id_box.value().serialize()['properties']['value']
if not wave_id:
return
else:
return
wavelet_id_box = event.blip.first(element.Input, name = 'Wavelet ID')
if wavelet_id_box:
wavelet_id = wavelet_id_box.value().serialize()['properties']['value']
else:
wavelet_id = wave_id.split('!')[0] + '!conv+root'
who = event.blip.first(element.Input, name = 'User ID')
if who:
who = who.value().serialize()['properties']['value']
else:
who = event.modified_by
try:
wavelet.get_operation_queue().wavelet_add_participant(wave_id, wavelet_id, who)
except:
wavelet.root_blip.append('\nOperation Failed')
try:
wavelet.get_operation_queue().wavelet_modify_participant_role(wave_id, wavelet_id, who, 'FULL')
except:
wavelet.root_blip.append('\nOperation Failed')
elif event.button_name == 'helpdesk-submitquestion':
logging.debug('New Question Submitted')
questionline = event.blip.first(element.Input)
if questionline:
question = questionline.value().serialize()['properties']['value']
else:
question = wavelet.title
detailbox = event.blip.first(element.TextArea)
if detailbox:
detail = detailbox.value().serialize()['properties']['value']
else:
detail = ''
if (question == 'Type a simple version of your question here') or (not question) or (question == 'New Helpdesk Question'):
wavelet.root_blip.append('\n\nYou need to summarise your question in the question box before submitting.\nIf you are having trouble with the helpdesk, add \'[email protected]\' to this wave.',
[('style/backgroundColor', 'rgb(255, 229, 0)'),
('style/fontWeight', 'bold')])
return
wavelet.root_blip.range(0, len(wavelet.root_blip.text)).delete()
wavelet.title = question
wavelet.root_blip.append('\n')
wavelet.root_blip.append(detail)
opQ.wavelet_add_participant(wavelet.wave_id, wavelet.wavelet_id, '[email protected]')
opQ.wavelet_modify_tag(wavelet.wave_id, wavelet.wavelet_id, 'question')
d_wave = myRobot.new_wave('googlewave.com',
participants = [event.modified_by],
submit = True)
for i in PARTICIPANTS + [event.modified_by]:
opQ.wavelet_add_participant(d_wave.wave_id, d_wave.wavelet_id, i)
d_wave.title = "[PUBLIC DISCUSSION] " + question
opQ.wavelet_modify_tag(d_wave.wave_id, d_wave.wavelet_id, 'discussion')
wavelet.data_documents['helpdesk-questionasker'] = event.modified_by
d_wave.data_documents['helpdesk-questionasker'] = event.modified_by
d_wave.root_blip.append('\n\n%s' % detail)
d_wave.root_blip.append('\n\nOriginal Question Wave', bundled_annotations = [('link/wave', wavelet.wave_id)])
d_wave.root_blip.append('\nIndex of all Questions', bundled_annotations = [('link/wave', INDEX_WAVE.wave_id)])
if wavelet.root_blip.elements:
import urllib2
for e in wavelet.root_blip.elements:
if isinstance(e, element.Attachment):
new = element.Attachment(caption = e.caption,
data = urllib2.urlopen(e.attachmentUrl).read())
wavelet.root_blip.append('\n')
wavelet.root_blip.append(new)
d_wave.root_blip.append('\n')
d_wave.root_blip.append(new)
d_wave.data_documents['helpdesk-wavetype'] = 'discussion'
myRobot.submit(d_wave)
temptext = '\n\nThis wave is public read-only (anyone can see it, but '\
'only participants can edit). The Helpdesk Team will choose the best answer '\
'from the discussion wave, once the discussion has finished, and post it here.\n'
temptext2 = 'Go to the \'Full-Access\' Public Discussion Wave\n'
wavelet.root_blip.append(temptext, [('style/fontStyle', 'italic')])
wavelet.root_blip.append(temptext2, [('link/wave', d_wave.wave_id),
('style/fontStyle', 'italic')])
index = myRobot.fetch_wavelet(INDEX_WAVE.wave_id, INDEX_WAVE.wavelet_id)
r = index.reply()
r.append(element.Line(alignment = element.Line.ALIGN_CENTER))
r.append(question + '\n')
r.append(element.Line(alignment = element.Line.ALIGN_CENTER))
r.append('Question Wave', [('link/wave', wavelet.wave_id)])
r.append(' | ')
r.first(' | ').clear_annotation('link/wave')
r.append('Discussion Wave', [('link/wave', d_wave.wave_id)])
myRobot.submit(index)
wavelet.root_blip.append('Question Submitted to the Helpdesk: ',
[('link/wave', None)])
wavelet.root_blip.append('Go to the Question Index',
[('link/wave', INDEX_WAVE.wave_id),
('style/fontStyle', 'italic')])
rec1 = classes.UserWave(wave_id = d_wave.wave_id,
wavelet_id = d_wave.wavelet_id,
title = d_wave.title,
wave_type = classes.USERWAVE_TYPES['discussion'],
reported_by = event.modified_by)
rec1.put()
rec2 = classes.UserWave(wave_id = wavelet.wave_id,
wavelet_id = wavelet.wavelet_id,
title = question,
wave_type = classes.USERWAVE_TYPES['question'],
discussionwave = rec1,
reported_by = event.modified_by)
rec2.put()
elif event.button_name == 'helpdesk-setupindex':
index = myRobot.new_wave('googlewave.com',
participants = PARTICIPANTS + [event.modified_by],
submit = True)
for i in PARTICIPANTS:
opQ.wavelet_add_participant(index.wave_id, index.wavelet_id, i)
wavelet.participants.set_role('[email protected]', 'READ_ONLY')
index.title = '[INDEX] Wave Helpdesk Questions'
index.root_blip.append('\n\nThis wave is where you will find links to all helpdesk discussions and questions.\n\n')
#global INDEX_WAVE
if INDEX_WAVE:
INDEX_WAVE.delete()
if ROOT_WAVE:
index.root_blip.append(element.Line(alignment = element.Line.ALIGN_RIGHT))
index.root_blip.append('Go To: ', bundled_annotations = [('style/fontSize', '0.9166666666666666em')])
index.root_blip.append('Main Wave', bundled_annotations = [('style/fontSize', '0.9166666666666666em'), ('link/wave', ROOT_WAVE.wave_id)])
if BUTTON_WAVE:
index.root_blip.append(element.Line(alignment = element.Line.ALIGN_RIGHT))
index.root_blip.append('Post a Question: ', bundled_annotations = [('style/fontSize', '0.9166666666666666em')])
index.root_blip.append('Helpdesk Question Button', bundled_annotations = [('style/fontSize', '0.9166666666666666em'), ('link/wave', BUTTON_WAVE.wave_id)])
rec = classes.MainWave(wave_id = index.wave_id,
wavelet_id = index.wavelet_id,
title = index.title,
wave_type = classes.MAINWAVE_TYPES['index'])
rec.put()
q_waves = db.GqlQuery('SELECT * FROM UserWave WHERE wave_type = :1', 1)
for q in q_waves:
r = index.reply(q.title + '\n')
r.append(element.Line(alignment = element.Line.ALIGN_CENTER))
r.append('Question Wave ', bundled_annotations = [('link/wave', q.wave_id)])
r.append('|')
r.append(' Discussion Wave', bundled_annotations = [('link/wave', q.discussionwave.wave_id)])
myRobot.submit(index)
elif event.button_name == 'helpdesk-setupmain':
main = myRobot.new_wave('googlewave.com',
participants = PARTICIPANTS + [event.modified_by],
submit = True)
for i in PARTICIPANTS + [event.modified_by]:
opQ.wavelet_add_participant(main.wave_id, main.wavelet_id, i)
main.title = '[MAIN WAVE] Wave Helpdesk'
main.root_blip.append('\n\nThe main wave for the helpdesk. It will contain links to our favourite/most useful questions.\n\n')
main.participants.set_role('[email protected]', 'READ_ONLY')
#global ROOT_WAVE
if ROOT_WAVE:
ROOT_WAVE.delete()
if INDEX_WAVE:
main.root_blip.append(element.Line(alignment = element.Line.ALIGN_RIGHT))
main.root_blip.append('Go To: ', bundled_annotations = [('style/fontSize', '0.9166666666666666em')])
main.root_blip.append('Index of Questions', bundled_annotations = [('style/fontSize', '0.9166666666666666em'), ('link/wave', INDEX_WAVE.wave_id)])
if BUTTON_WAVE:
main.root_blip.append(element.Line(alignment = element.Line.ALIGN_RIGHT))
main.root_blip.append('Post a Question: ', bundled_annotations = [('style/fontSize', '0.9166666666666666em')])
main.root_blip.append('Helpdesk Question Button', bundled_annotations = [('style/fontSize', '0.9166666666666666em'), ('link/wave', BUTTON_WAVE.wave_id)])
rec = classes.MainWave(wave_id = main.wave_id,
wavelet_id = main.wavelet_id,
title = main.title,
wave_type = classes.MAINWAVE_TYPES['main'])
rec.put()
myRobot.submit(main)
elif event.button_name == 'helpdesk-setupbutton':
button = myRobot.new_wave('googlewave.com',
participants = PARTICIPANTS + [event.modified_by],
submit = True)
for i in PARTICIPANTS + [event.modified_by]:
opQ.wavelet_add_participant(button.wave_id, button.wavelet_id, i)
button.title = '[HELPDESK] Use the button on this wave to post a question'
button.root_blip.append('\n\nClicking the button below will make the helpdesk robot create a new wave for you, where you can ask your question.\n\n')
button.root_blip.append(element.Line(alignment = element.Line.ALIGN_CENTER))
button.root_blip.append(element.Button('helpdesk-newquestion', 'Click to ask a question'),
bundled_annotations = [('style/color','rgb(51, 127, 229)'),
('style/fontWeight', 'bold')])
button.root_blip.append(element.Line(alignment = element.Line.ALIGN_CENTER))
button.root_blip.append('Please do not add Robots to this wave - it will get you suspended from being able to use the helpdesk.\n\n')
#global BUTTON_WAVE
if BUTTON_WAVE:
BUTTON_WAVE.delete()
if ROOT_WAVE:
button.root_blip.append(element.Line(alignment = element.Line.ALIGN_RIGHT))
button.root_blip.append('Go To: ', bundled_annotations = [('style/fontSize', '0.9166666666666666em')])
button.root_blip.append('Main Wave', bundled_annotations = [('style/fontSize', '0.9166666666666666em'), ('link/wave', ROOT_WAVE.wave_id)])
if INDEX_WAVE:
button.root_blip.append(element.Line(alignment = element.Line.ALIGN_RIGHT))
button.root_blip.append('Go To: ', bundled_annotations = [('style/fontSize', '0.9166666666666666em')])
button.root_blip.append('Index of Questions', bundled_annotations = [('style/fontSize', '0.9166666666666666em'), ('link/wave', INDEX_WAVE.wave_id)])
rec = classes.MainWave(wave_id = index.wave_id,
wavelet_id = index.wavelet_id,
title = index.title,
wave_type = classes.MAINWAVE_TYPES['button'])
rec.put()
myRobot.submit(button)
def OnWaveletSelfAdded(event, wavelet):
logging.debug("OnWaveletSelfAdded")
if (not wavelet.title) or (wavelet.title == 'Wave Helpdesk'):
create_question_wave(wavelet)
return
elif wavelet.creator != '[email protected]':
return
elif '[email protected]' not in wavelet.participants:
return
if 'Create Admin Console' in wavelet.title:
wavelet.title = '[ADMIN CONSOLE] - Wave Helpdesk'
wavelet.root_blip.append(element.Button('helpdesk-setupindex', 'NEW INDEX WAVE'))
wavelet.root_blip.append(element.Button('helpdesk-setupmain', 'NEW MAIN WAVE'))
wavelet.root_blip.append(element.Button('helpdesk-setupbutton', 'NEW BUTTON WAVE'))
elif 'Add Testing Button' in wavelet.title:
wavelet.reply().append(element.Button('helpdesktesting-newquestion', 'New Style Question (Test)'))
return
elif 'Setup \'Add Me\'' in wavelet.title:
wavelet.title = 'HelpdeskDev - Add yourself to a wave'
wavelet.root_blip.append(element.Line('h3', None, 'c'))
wavelet.root_blip.append('Enter the Wave ID of the wave you want to be added to:\n')
wavelet.root_blip.append(element.Input(name = 'Wave Add'))
wavelet.root_blip.append('\n\n')
wavelet.root_blip.append(element.Line('h3', None, 'c'))
wavelet.root_blip.append('Enter the Wavelet ID of the wave you want to be added to:\nIf you are\'t sure, leave blank.')
wavelet.root_blip.append(element.Input(name = 'Wavelet ID'))
wavelet.root_blip.append('\n\n')
wavelet.root_blip.append(element.Button('helpdesk-dev-add_me', 'Add Me'))
wavelet.root_blip.append('\n\nIf you need to add someone else, enter their address here, then click \'Add Them\'\n')
wavelet.root_blip.append(element.Input(name = 'User ID'))
wavelet.root_blip.append('\n')
wavelet.root_blip.append(element.Button('helpdesk-dev-add_them', 'Add Them'))
def OnParticipantsChanged(event, wavelet):
logging.info('OnParticipantsChanged Called')
if ('[email protected]' != wavelet.root_blip.creator):
return
if 'helpdesk-questionasker' in wavelet.data_documents.keys():
if wavelet.data_documents['helpdesk-questionasker'] == event.modified_by:
return
if wavelet.modified_by in PARTICIPANTS:
return
badAdded = []
goodRemoved = []
for p in event.participants_added:
if (p.split('@')[1] == 'appspot.com') or (p.split('@')[1] == 'googlewaverobots.com'):
badAdded.append(p)
if p in ['[email protected]', '[email protected]',]:
badAdded.remove(p)
for p in event.participants_removed:
if p in PARTICIPANTS:
goodRemoved.append(p)
if not (badAdded or goodRemoved):
return
for i in badAdded:
wavelet.participants.set_role(i, 'READ_ONLY')
logging.debug('%s now read only' % i)
for i in goodRemoved:
wavelet.participants.add(i)
logging.debug('%s was re-added' % i)
if ('helpdesk-warninguser-%s' % event.modified_by) in wavelet.data_documents:
wavelet.participants.set_role(event.modified_by, 'READ_ONLY')
else:
wavelet.data_documents['helpdesk-warninguser-%s' % event.modified_by] = '!'
def onWaveletTagsChanged(event, wavelet):
if wavelet.data_documents['helpdesk-questiontype']:
wave_type = wavelet.data_documents['helpdesk-questiontype']
else:
pass
pass
#TODO Write syncing code here.
if ROOT_WAVE:
prof_url = 'https://wave.google.com/wave/waveref/%s/%s' % (ROOT_WAVE.wave_id.split('!')[0], ROOT_WAVE.wave_id.split('!')[1])
else:
prof_url = 'http://groups.google.com/group/wave-helpdesk'
myRobot = robot.Robot(name = 'HelpDesk(beta)',
image_url = 'http://wave-helpdesk.appspot.com/avatar.png',
profile_url = prof_url)
myRobot.register_handler(events.FormButtonClicked, OnFormButtonClicked, context = ['ROOT', 'SELF'])
myRobot.register_handler(events.WaveletParticipantsChanged, OnParticipantsChanged, context = 'ROOT')
myRobot.register_handler(events.WaveletSelfAdded, OnWaveletSelfAdded, context = 'ROOT')
try:
import credentials
except:
import credentials_template as credentials
if credentials.V_TOKEN:
myRobot.set_verification_token_info(credentials.V_TOKEN, credentials.S_TOKEN)
if (credentials.KEY) and (credentials.SECRET):
myRobot.setup_oauth(credentials.KEY, credentials.SECRET)
appengine_robot_runner.run(myRobot, debug=True)