-
Notifications
You must be signed in to change notification settings - Fork 6
/
prayer.py
437 lines (387 loc) · 18.2 KB
/
prayer.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
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
import random
import json
from dbms.rdb import db
from dbms.models import Intent, BibleVerse
from events import PrayerEvent, UserEvent
from facebook import user_utils, utils
from translations.user import user_gettext
import time
displayed_prayers_limit = 5
displayed_intentions_limit = 5
max_intentions = 10
max_prayers = 10
class PrayerWebhook(object):
@staticmethod
def handle_message(sender_id, message):
response_message = None
text = message['text']
lower_text = unicode(text.lower())
initialized_prayers = Intent.query.filter_by(user_id = sender_id, description = '').all()
if initialized_prayers != []:
prayer = initialized_prayers[0]
response_message = utils.response_buttons(
user_gettext(sender_id, u"You requested a prayer for: %(value)s ?", value=text),
[
{
'title': user_gettext(sender_id, u"Yes"),
'payload': UserEvent.payload(UserEvent.CONFIRM_INTENTION, {'prayer_id': prayer.id, 'description': text})
},
{
'title': user_gettext(sender_id, u"No"),
'payload': UserEvent.payload(UserEvent.DELETE_INTENTION, {'prayer_id': prayer.id})
},
]
)
# Quick replies buttons does not work in Web interface
#response_message = utils.quick_buttons(
# user_gettext(sender_id, u"You requested a prayer for: %(value)s ?", value=text),
# [
# {
# 'content_type': 'text',
# 'title': user_gettext(sender_id, u"Yes"),
# 'payload': UserEvent.payload(UserEvent.CONFIRM_INTENTION, {'prayer_id': prayer.id, 'description': text})
# },
# {
# 'content_type': 'text',
# 'title': user_gettext(sender_id, u"No"),
# 'payload': UserEvent.payload(UserEvent.DELETE_INTENTION, {'prayer_id': prayer.id})
# },
# ]
#)
elif lower_text in user_gettext(sender_id, 'help') or user_gettext(sender_id, 'prayer') in lower_text or lower_text in 'help':
# Buttons limited to 3 values
# So we need to create multiple bubbles in one message
options1 = [
{
'title': user_gettext(sender_id, u"I want to pray"),
'payload': UserEvent.payload(UserEvent.WANT_TO_PRAY)
}
]
commited_prayers = Intent.query.filter_by(commiter_id = sender_id).first()
if commited_prayers :
options1.append({
'title': user_gettext(sender_id, u"Who do I pray for ?"),
'payload': UserEvent.payload(UserEvent.MY_PRAYERS)
})
options2 = [
{
'title': user_gettext(sender_id, u"Please pray for me"),
'payload': UserEvent.payload(UserEvent.PRAY_FOR_ME)
}
]
intentions = Intent.query.filter_by(user_id = sender_id).first()
if intentions :
options2.append({
'title': user_gettext(sender_id, u"My intentions"),
'payload': UserEvent.payload(UserEvent.MY_INTENTIONS)
}
)
text_list = [ user_gettext(sender_id, u"Maybe you can pray for someone ?") ]
text_list.append( user_gettext(sender_id, u"Or maybe you need a prayer ?") )
options_set = [ options1 ]
options_set.append( options2 )
response_message = utils.response_multiple_bubbles_buttons( text_list, options_set )
elif lower_text in user_gettext(sender_id, u"bible"):
bibleVerses = BibleVerse.query.all()
verse = random.choice(bibleVerses)
if (verse == None):
verse = BibleVerse( user_gettext( u"God is light; in him there is no darkness at all."), "1 J 1,5b")
response_message = utils.response_text("\"" + verse.text + "\" " + verse.address)
else:
response_message = utils.response_text(user_utils.user_name(sender_id) +
user_gettext(sender_id, u", God bless you!\nType 'prayer' to see prayer options or 'Bible' to get Bible verse for you."))
response = json.dumps({
'recipient': { 'id' : sender_id },
'message': response_message
})
return response
@staticmethod
def handle_postback(sender_id, postback):
payload = json.loads(postback['payload'])
if 'user_event' in payload:
event_key = payload['user_event']
event = UserEvent(event_key)
callbacks = PrayerWebhook.handle_user_event(sender_id, event, payload)
elif 'prayer_event' in payload:
event_key = payload['prayer_event']
event = PrayerEvent(event_key)
user_id = payload['user_id']
prayer_id = payload['prayer_id']
callbacks = PrayerWebhook.handle_prayer_event(sender_id, user_id, prayer_id, event, payload)
# commit DB changes
db.session.commit()
db.session.flush()
response_callbacks = map(map_callback, callbacks.items())
return response_callbacks
@staticmethod
def handle_user_event(sender_id, event, payload):
if event == UserEvent.CONFIRM_INTENTION:
prayer_id = payload['prayer_id']
description_value = payload['description']
intent = Intent.query.filter_by(id = prayer_id).first()
intent.description = description_value
return {
sender_id : utils.response_text(user_gettext(sender_id, u"You'll be notified when somebody wants to pray for you")),
}
elif event == UserEvent.DELETE_INTENTION:
prayer_id = payload['prayer_id']
intent = Intent.query.filter_by(id = prayer_id).first()
if intent:
db.session.delete(intent)
return {
sender_id : utils.response_text(user_gettext(sender_id, u"I've deleted a prayer request")),
}
else:
return {
sender_id: utils.response_text(user_gettext(sender_id, u"There is no such prayer request")),
}
elif event == UserEvent.PRAY_FOR_ME:
# check how many intention user already provided
int_cnt = Intent.query.filter(Intent.user_id == sender_id).count()
if int_cnt < max_intentions:
intent = Intent(sender_id, '')
intent.ts = int(time.time())
db.session.add(intent)
return {
sender_id : utils.response_text(user_gettext(sender_id, u"What is your prayer request ?")),
}
else:
return {
sender_id : utils.response_text(user_gettext(sender_id, u"You've reached maximum intentions count. Please come back in few days.")),
}
elif event == UserEvent.WANT_TO_PRAY:
pray_cnt = Intent.query.filter(Intent.commiter_id == sender_id ).count()
if pray_cnt < max_prayers:
prayers = Intent.query.filter(Intent.commiter_id == 0 ).filter(Intent.user_id != sender_id).limit(displayed_prayers_limit).all()
prayer_elements = map(map_prayer, prayers)
if prayer_elements == []:
return {
sender_id : utils.response_text(user_gettext(sender_id, u"There're no prayer requests")),
}
else:
return {
sender_id : utils.response_elements(prayer_elements),
}
else:
return {
sender_id : utils.response_text(user_gettext(sender_id, u"You've reached maximum prayers count. Please pray in your already selected intentions first.")),
}
elif event == UserEvent.MY_PRAYERS:
prayer_elements = map_said_prayer_multiple_bubbles( sender_id )
if prayer_elements == []:
return {
sender_id : utils.response_text(user_gettext(sender_id, u"There're no prayer requests")),
}
else:
return {
sender_id : utils.response_elements(prayer_elements),
}
elif event == UserEvent.MY_INTENTIONS:
all_intentions = []
options_set = []
for intention in Intent.query.filter_by(user_id=sender_id)[0:displayed_intentions_limit]:
buttons = [
{
'title': user_gettext(sender_id, u"Delete"),
'payload': UserEvent.payload(UserEvent.DELETE_INTENTION, {'prayer_id': intention.id})
},
]
if intention.commiter_id > 0:
buttons.append( {
'title': user_gettext(sender_id, u"Send thank you"),
'payload': UserEvent.payload(UserEvent.THANK_FOR_PRAYER, {'prayer_id': intention.id})
},
)
if options_set:
options_set.append( buttons )
else:
options_set = [ buttons ]
if all_intentions:
all_intentions.append( intention.description )
else:
all_intentions = [ intention.description ]
response_message = utils.response_multiple_bubbles_buttons(all_intentions, options_set)
if all_intentions == []:
return {
sender_id : utils.response_text( user_gettext( sender_id, u"You don't have any intentions" ) ),
}
else:
return {
sender_id : response_message
}
elif event == UserEvent.THANK_FOR_PRAYER:
prayer_id = payload['prayer_id']
intent = Intent.query.filter_by(id = prayer_id).first()
if intent:
commiter_id = intent.commiter_id
prayer_desc = intent.description
user_name = user_utils.user_name(sender_id)
commiter_name = user_utils.user_name(commiter_id)
return {
sender_id : utils.response_text(user_gettext(sender_id, u"User %(name)s will be notified about your thankfulness.", name=commiter_name)),
commiter_id : utils.response_text(user_gettext(commiter_id, u"User %(name)s wants to thank you for your prayer in the following request: %(desc)s.", name=user_name, desc=prayer_desc)),
}
else:
return {
sender_id: utils.response_text(user_gettext(sender_id, u"There is no such prayer request")),
}
@staticmethod
def handle_prayer_event(sender_id, user_id, prayer_id, event, payload):
sender_name = user_utils.user_name(sender_id)
user_name = user_utils.user_name(user_id)
intent = Intent.query.filter_by(id = prayer_id).one_or_none()
if intent:
intent_description = intent.description
#.encode('utf-8')
if event == PrayerEvent.I_PRAY:
intent.commiter_id = sender_id
return {
# This is dictionary - no duplicates allowed
# When user_id == sender_id then only second message will be send
sender_id : utils.response_text(user_gettext(sender_id, u"You're subscribed for the prayer request from user %(name)s", name=user_name)),
user_id : utils.response_text(user_gettext(user_id, u"User %(name)s will be praying in your following request: %(desc)s", name=sender_name, desc=intent_description)),
}
elif event == PrayerEvent.DID_PRAY:
# Should confirmed prayer be deleted or just marked as confirmed and deleted separatelly
# by scheduled script ?
# It should be the same way as in CONFIRM_PRAY option
#db.session.delete(intent)
intent.confirmed = 1
return {
# This is dictionary - no duplicates allowed
# When user_id == sender_id then only second message will be send
sender_id : utils.response_text(user_gettext(sender_id, u"User %(name)s has been notified that you've prayed for him/her. Thank you", name=user_name)),
user_id : utils.response_text(user_gettext(user_id, u"User %(name)s has prayed in your request: %(desc)s", name=sender_name, desc=intent_description)),
}
elif event == PrayerEvent.ENSURE_PRAY:
# This is dictionary - no duplicates allowed
# When user_id == sender_id then only second message will be send
return {
sender_id : utils.response_text(user_gettext(sender_id, u"User %(name)s has been ensured that you pray for him", name=user_name)),
user_id : utils.response_text(user_gettext(user_id, u"User %(name)s wants to ensure you about his prayer in the following request: %(desc)s", name=sender_name, desc=intent_description)) ,
}
elif event == PrayerEvent.GIVE_UP:
intent.commiter_id = 0
return {
sender_id : utils.response_text(user_gettext(sender_id, u"Thank you for your will of praying. User %(name)s won't be notified about you giving up.", name=user_name)),
}
elif event == PrayerEvent.DONT_CONFIRM_PRAY:
return {
sender_id : utils.response_text(user_gettext(sender_id, u"Please pray. Someone is counting on you.\nI will ask you again tomorrow.")),
}
else:
return {
sender_id: utils.response_text(user_gettext(sender_id, u"There is no such prayer request")),
}
def map_callback(callback):
sender_id = callback[0]
response_message = callback[1]
return json.dumps({
'recipient': { 'id' : sender_id },
'message': response_message
})
def map_prayer(prayer):
user_id = prayer.user_id
return {
'title': user_utils.user_name(user_id),
'subtitle': prayer.description,
'buttons': [
{
'title': user_gettext(user_id, u"I am praying"),
'payload': PrayerEvent.payload(PrayerEvent.I_PRAY, prayer.id, user_id)
}
],
'image_url': user_utils.img_url(user_id)
}
def map_said_prayer_multiple_bubbles(sender_id):
elements = None
for prayer in Intent.query.filter_by(commiter_id=sender_id).limit(displayed_prayers_limit).all():
user_id = prayer.user_id
# some limit for checking images
# ConnectionError: HTTPSConnectionPool(host='graph.facebook.com', port=443):
# Max retries exceeded with url: /v2.8/me/thread_settings?access_token=xxx
# (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fd377cb5350>:
# Failed to establish a new connection: [Errno -2] Name or service not known',))
single_bubble_button = {
"title": prayer.description,
"subtitle": user_utils.user_name(user_id),
"image_url": user_utils.img_url(user_id),
"buttons": [
{
'type' : "postback",
'title': user_gettext(user_id, u"I've prayed"),
'payload': PrayerEvent.payload(PrayerEvent.DID_PRAY, prayer.id, user_id)
},
{
'type': "postback",
'title': user_gettext(user_id, u"Ensure about your prayer"),
'payload': PrayerEvent.payload(PrayerEvent.ENSURE_PRAY, prayer.id, user_id)
},
{
'type': "postback",
'title': user_gettext(user_id, u"Stop your prayer"),
'payload': PrayerEvent.payload(PrayerEvent.GIVE_UP, prayer.id, user_id)
},
]
}
if elements:
elements.append( single_bubble_button )
else:
elements = [ single_bubble_button ]
return elements
# Facebook API samples
#{
# "recipient":{
# "id":"USER_ID"
# },
# "message":{
# "attachment":{
# "type":"template",
# "payload":{
# "template_type":"generic",
# "elements":[
# {
# "title":"Welcome to Peter\'s Hats",
# "item_url":"https://petersfancybrownhats.com",
# "image_url":"https://petersfancybrownhats.com/company_image.png",
# "subtitle":"We\'ve got the right hat for everyone.",
# "buttons":[
# {
# "type":"web_url",
# "url":"https://petersfancybrownhats.com",
# "title":"View Website"
# },
# {
# "type":"postback",
# "title":"Start Chatting",
# "payload":"DEVELOPER_DEFINED_PAYLOAD"
# }
# ]
# }
# ]
# }
# }
# }
#}'
#curl -X POST -H "Content-Type: application/json" -d '{
# "recipient":{
# "id":"USER_ID"
# },
# "message":{
# "text":"Pick a color:",
# "quick_replies":[
# {
# "content_type":"text",
# "title":"Red",
# "payload":"DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED"
# },
# {
# "content_type":"text",
# "title":"Green",
# "payload":"DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_GREEN"
# }
# ]
# }
#}' "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"