def confirm_praying_for_intention(): query_prayers = Intent.query.filter(Intent.commiter_id > 0, Intent.confirmed == 0).all() for prayer in query_prayers: if prayer.commiter_id != None and prayer.commiter_id != "": options = [{ 'title': user_gettext(prayer.commiter_id, u"Yes"), 'payload': PrayerEvent.payload(PrayerEvent.DID_PRAY, prayer.id, prayer.user_id) }, { 'title': user_gettext(prayer.commiter_id, u"No"), 'payload': PrayerEvent.payload(PrayerEvent.DONT_CONFIRM_PRAY, prayer.id, prayer.user_id) }] response_message = utils.response_buttons( user_gettext(prayer.commiter_id, u"Did You pray in below request ?\n%(value)s", value=prayer.description), options) response = json.dumps({ 'recipient': { 'id': prayer.commiter_id }, 'message': response_message }) api = FacebookApi() api.post("/me/messages", response)
def test_response_buttons(self): text = "push the button" buttons = [ { "title": "Yes", "payload": "press_yes" }, { "title": "No", "payload": "press_no" }, ] res = utils.response_buttons(text, buttons) assert res == json.dumps({"attachment": {"type": "template", "payload": {"template_type": "button", "text": text, "buttons": [ { "type":"postback", "title": "Yes", "payload": "press_yes" }, { "type":"postback", "title": "No", "payload": "press_no" }, ] }}})
def confirm_praying_for_intention(): query_prayers = Intent.query.filter( Intent.commiter_id > 0, Intent.confirmed == 0 ).all(); for prayer in query_prayers: if prayer.commiter_id != None and prayer.commiter_id != "": options = [ { 'title': user_gettext( prayer.commiter_id, u"Yes" ), 'payload': PrayerEvent.payload(PrayerEvent.DID_PRAY, prayer.id, prayer.user_id) }, { 'title': user_gettext( prayer.commiter_id, u"No" ), 'payload': PrayerEvent.payload(PrayerEvent.DONT_CONFIRM_PRAY, prayer.id, prayer.user_id) } ] response_message = utils.response_buttons( user_gettext( prayer.commiter_id, u"Did You pray in below request ?\n%(value)s", value=prayer.description ), options ) response = json.dumps({ 'recipient': { 'id' : prayer.commiter_id }, 'message': response_message }) api = FacebookApi(); api.post("/me/messages", response);
def test_response_buttons(self): text = "push the button" buttons = [ { "title": "Yes", "payload": "press_yes" }, { "title": "No", "payload": "press_no" }, ] res = utils.response_buttons(text, buttons) assert res == json.dumps({ "attachment": { "type": "template", "payload": { "template_type": "button", "text": text, "buttons": [ { "type": "postback", "title": "Yes", "payload": "press_yes" }, { "type": "postback", "title": "No", "payload": "press_no" }, ] } } })
def handle_message(sender_id, message): response_message = None text = message['text'].encode('utf-8') lower_text = 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.UPDATE_PRAYER, { 'prayer_id': prayer.id, 'description': text }) }, { 'title': user_gettext(sender_id, u"No"), 'payload': UserEvent.payload(UserEvent.DELETE_PRAYER, {'prayer_id': prayer.id}) }, ]) elif lower_text in user_gettext(sender_id, 'help') or user_gettext( sender_id, 'pray') in lower_text: commited_prayers = Intent.query.filter_by(commiter_id=sender_id) options = [ { 'title': user_gettext(sender_id, u"Please pray for me"), 'payload': UserEvent.payload(UserEvent.PRAY_FOR_ME) }, { 'title': user_gettext(sender_id, u"I want to pray"), 'payload': UserEvent.payload(UserEvent.WANT_TO_PRAY) }, ] if commited_prayers != []: options.append({ 'title': user_gettext(sender_id, u"Who do I pray for?"), 'payload': UserEvent.payload(UserEvent.MY_PRAYERS) }) response_message = utils.response_buttons( user_gettext(sender_id, u"Please choose what do you need?"), options) else: response_message = utils.response_text( user_gettext( sender_id, u"Sorry but I don't understand you.\nType 'help' to get additional information." )) response = json.dumps({ 'recipient': { 'id': sender_id }, 'message': response_message }) return response
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
def handle_message(sender, message): response = None text = message['text'].encode('utf-8') lower_text = text.lower() sender_id = sender['id'] initialized_prayers = db.fetch_history({ "user_id": sender_id, "description": "" }) if initialized_prayers != []: prayer = initialized_prayers[0] response_message = utils.response_buttons( "Czy na pewno chcesz żeby ktoś się pomodlił w następującej intencji: " + text + "?", [ { "type": "postback", "title": "Tak", "payload": json.dumps({ "user_event": "update_prayer", "prayer_id": prayer[label_id], "description": text }) }, { "type": "postback", "title": "Nie", "payload": json.dumps({ "user_event": "delete_prayer", "prayer_id": prayer[label_id] }) }, ]) response = json.dumps({ 'recipient': { 'id': sender_id }, 'message': response_message }) elif lower_text in ['help', 'pomoc' ] or 'modl' in lower_text or 'pray' in lower_text: commited_prayers = db.fetch_history({"commiter_id": sender_id}) options = [ { "type": "postback", "title": "Potrzebuję modlitwy", "payload": json.dumps({"user_event": "pray_for_me"}) }, { "type": "postback", "title": "Chcę się pomodlić", "payload": json.dumps({"user_event": "want_to_pray"}) }, ] if commited_prayers != []: options.append({ "type": "postback", "title": "Za kogo się modlę?", "payload": json.dumps({"user_event": "prayers"}) }) response_message = utils.response_buttons( "Proszę wybierz czego potrzebujesz?", options) response = json.dumps({ 'recipient': { 'id': sender_id }, 'message': response_message }) elif text == 'info': resp_text = systools.system_info() response_message = utils.response_text("Version: " + resp_text) response = json.dumps({ 'recipient': { 'id': sender_id }, 'message': response_message }) else: response_message = utils.response_text( "Niestety Cię nie rozumiem.\nWpisz 'pomoc' żeby uzyskać dodatkowe informacje." ) response = json.dumps({ 'recipient': { 'id': sender_id }, 'message': response_message }) return response
def handle_message(sender, message): response = None text = message['text'].encode('utf-8') lower_text = text.lower() sender_id = sender['id'] initialized_prayers = db.fetch_history({"user_id": sender_id, "description": ""}) if initialized_prayers != []: prayer = initialized_prayers[0] response_message = utils.response_buttons( "Czy na pewno chcesz żeby ktoś się pomodlił w następującej intencji: " + text + "?", [ { "type":"postback", "title":"Tak", "payload": json.dumps({"user_event": "update_prayer", "prayer_id": prayer[label_id], "description": text}) }, { "type":"postback", "title":"Nie", "payload": json.dumps({"user_event": "delete_prayer", "prayer_id": prayer[label_id]}) }, ] ) response = json.dumps({ 'recipient': { 'id' : sender_id }, 'message': response_message }) elif lower_text in ['help', 'pomoc'] or 'modl' in lower_text or 'pray' in lower_text: commited_prayers = db.fetch_history({"commiter_id": sender_id}) options = [ { "type":"postback", "title":"Potrzebuję modlitwy", "payload": json.dumps({"user_event": "pray_for_me"}) }, { "type":"postback", "title":"Chcę się pomodlić", "payload": json.dumps({"user_event": "want_to_pray"}) }, ] if commited_prayers != []: options.append({ "type":"postback", "title":"Za kogo się modlę?", "payload": json.dumps({"user_event": "prayers"}) }) response_message = utils.response_buttons( "Proszę wybierz czego potrzebujesz?", options ) response = json.dumps({ 'recipient': { 'id' : sender_id }, 'message': response_message }) elif text == 'info': resp_text = systools.system_info() response_message = utils.response_text("Version: " + resp_text) response = json.dumps({ 'recipient': { 'id' : sender_id }, 'message': response_message }) else: response_message = utils.response_text("Niestety Cię nie rozumiem.\nWpisz 'pomoc' żeby uzyskać dodatkowe informacje.") response = json.dumps({ 'recipient': { 'id' : sender_id }, 'message': response_message }) return response
def handle_message(sender, message): response = None text = message['text'].encode('utf-8') lower_text = text.lower() sender_id = sender['id'] # initialized_prayers = db.fetch_history({"user_id": sender_id, "description": ""}) 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), [ { "type":"postback", "title": user_gettext(sender_id, u"Yes"), "payload": json.dumps({"user_event": "update_prayer", "prayer_id": prayer.id, "description": text}) }, { "type":"postback", "title": user_gettext(sender_id, u"No"), "payload": json.dumps({"user_event": "delete_prayer", "prayer_id": prayer.id}) }, ] ) response = json.dumps({ 'recipient': { 'id' : sender_id }, 'message': response_message }) elif lower_text in user_gettext(sender_id, u'help') or user_gettext(sender_id, u'pray') in lower_text: # commited_prayers = db.fetch_history({"commiter_id": sender_id}) commited_prayers = Intent.query.filter_by(commiter_id = sender_id) options = [ { "type":"postback", "title": user_gettext(sender_id, u"Please pray for me"), "payload": json.dumps({"user_event": "pray_for_me"}) }, { "type":"postback", "title": user_gettext(sender_id, u"I want to pray"), "payload": json.dumps({"user_event": "want_to_pray"}) }, ] if commited_prayers != []: options.append({ "type":"postback", "title": user_gettext(sender_id, u"Who do I pray for?"), "payload": json.dumps({"user_event": "prayers"}) }) response_message = utils.response_buttons( user_gettext(sender_id, u"Please choose what do you need?"), options ) response = json.dumps({ 'recipient': { 'id' : sender_id }, 'message': response_message }) elif text == 'info': resp_text = systools.system_info() response_message = utils.response_text("Version: " + resp_text) response = json.dumps({ 'recipient': { 'id' : sender_id }, 'message': response_message }) else: response_message = utils.response_text(user_gettext(sender_id, u"Sorry but I don't understand you.\nType 'help' to get additional information.")) response = json.dumps({ 'recipient': { 'id' : sender_id }, 'message': response_message }) return response
def handle_user_event(sender_id, event_type, payload): if event_type == 'update_prayer': # TODO: update prayer in DB id_value = payload["prayer_id"] description_value = payload["description"] #db.update_description(id_value, description_value) intent = Intent.query.filter_by(id = id_value).first() intent.description = description_value db.session.commit() return { sender_id : utils.response_text(user_gettext(sender_id, u"You'll be notified when somebody wants to pray for you")), } elif event_type == 'delete_prayer': # TODO: delete prayer from DB id_value = payload["prayer_id"] #db.delete(id_value) intent = Intent.query.filter_by(id = id_value).first() db.session.delete(intent) db.session.commit() return { sender_id : utils.response_text(user_gettext(sender_id, u"I've deleted a prayer request")), } elif event_type == 'pray_for_me': # data_line = dict( # user_id=sender_id, # commiter_id="", # ts=1234, # description="", # ) # db.insert_row(data_line) intent = Intent(sender_id, "") intent.ts = 1234 db.session.add(intent) db.session.commit() response_message = utils.response_buttons( user_gettext(sender_id, u"What is your prayer request?"), [ { "type":"postback", "title":"Odwołaj modlitwę", #nie ma wersji angielskiej "payload": json.dumps({"user_event": "delete_prayer"}) } ]) return { sender_id : response_message } elif event_type == 'want_to_pray': # prayers = db.fetch_history({"commiter_id": ""}, displayed_prayers_limit) prayers = Intent.query.limit(displayed_prayers_limit).all() #print("Fetched prayers: " + json.dumps(prayers)) prayer_elements = map(map_prayer, prayers) return { sender_id : utils.response_elements(prayer_elements), } elif event_type == 'prayers': # commited_prayers = db.fetch_history({"commiter_id": sender_id}) commited_prayers = Intent.query.filter_by(commiter_id = sender_id) prayer_elements = map(map_said_prayer, commited_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), }