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) return { sender_id: utils.response_text( 'Zostaniesz poinformowany gdy ktoś będzie chciał się za Ciebie pomodlić' ), } elif event_type == 'delete_prayer': # TODO: delete prayer from DB id_value = payload["prayer_id"] db.delete(id_value) return { sender_id: utils.response_text('Usunąłem prośbę o modlitwę'), } elif event_type == 'pray_for_me': data_line = dict( user_id=sender_id, commiter_id="", ts=1234, description="", ) db.insert_row(data_line) return { sender_id: utils.response_text('Jaka jest Twoja intencja?'), } elif event_type == 'want_to_pray': prayers = db.fetch_history({"commiter_id": ""}, displayed_prayers_limit) 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}) prayer_elements = map(map_said_prayer, commited_prayers) if prayer_elements == []: return { sender_id: utils.response_text('Brak aktualnych intencji'), } else: return { sender_id: utils.response_elements(prayer_elements), }
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 handle_prayer_event(sender_id, event_type, payload): user_id = payload['user_id'] prayer_id = payload['prayer_id'] prayer = db.fetch(prayer_id) prayer_description = prayer['description'].encode("utf-8") if event_type == 'i_pray': db.update_commiter(prayer_id, sender_id) return { sender_id : utils.response_text('Zostałeś zapisany na modlitwę w intencji użytkownika ' + utils.user_name(user_id)), user_id : utils.response_text('Użytkownik ' + utils.user_name(sender_id) + ' będzie się modlił w Twojej następującej intencji: ' + prayer_description), } elif event_type == 'did_pray': db.delete(prayer_id) return { user_id : utils.response_text('Użytkownik ' + utils.user_name(sender_id) + ' pomodlił się w Twojej intencji: ' + prayer_description), sender_id : utils.response_text('Użytkownik ' + utils.user_name(user_id) + ' został powiadomiony o tym, że pomodliłeś się za niego. Dziękujemy'), } elif event_type == 'send_message': return { user_id : utils.response_text('Użytkownik ' + utils.user_name(sender_id) + ' pamięta o Tobie w modlitwie w następującej intencji: ' + prayer_description), sender_id : utils.response_text('Użytkownik ' + utils.user_name(user_id) + ' został powiadomiony o tym, że pamiętasz o nim w modlitwie'), } elif event_type == 'give_up': db.update_commiter(prayer_id, '') return { sender_id : utils.response_text('Dziękujemy za chęć modlitwy. Użytkownik ' + utils.user_name(user_id) + ' nie zostanie powiadomiony o Twojej rezygnacji'), }
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) return { sender_id : utils.response_text('Zostaniesz poinformowany gdy ktoś będzie chciał się za Ciebie pomodlić'), } elif event_type == 'delete_prayer': # TODO: delete prayer from DB id_value = payload["prayer_id"] db.delete(id_value) return { sender_id : utils.response_text('Usunąłem prośbę o modlitwę'), } elif event_type == 'pray_for_me': data_line = dict( user_id=sender_id, commiter_id="", ts=1234, description="", ) db.insert_row(data_line) return { sender_id : utils.response_text('Jaka jest Twoja intencja?'), } elif event_type == 'want_to_pray': prayers = db.fetch_history({"commiter_id": ""}, displayed_prayers_limit) 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}) prayer_elements = map(map_said_prayer, commited_prayers) if prayer_elements == []: return { sender_id : utils.response_text('Brak aktualnych intencji'), } else: return { sender_id : utils.response_elements(prayer_elements), }
def handle_prayer_event(sender_id, event_type, payload): user_id = payload['user_id'] prayer_id = payload['prayer_id'] prayer = db.fetch(prayer_id) prayer_description = prayer['description'].encode("utf-8") if event_type == 'i_pray': db.update_commiter(prayer_id, sender_id) return { sender_id: utils.response_text( 'Zostałeś zapisany na modlitwę w intencji użytkownika ' + utils.user_name(user_id)), user_id: utils.response_text( 'Użytkownik ' + utils.user_name(sender_id) + ' będzie się modlił w Twojej następującej intencji: ' + prayer_description), } elif event_type == 'did_pray': db.delete(prayer_id) return { user_id: utils.response_text('Użytkownik ' + utils.user_name(sender_id) + ' pomodlił się w Twojej intencji: ' + prayer_description), sender_id: utils.response_text( 'Użytkownik ' + utils.user_name(user_id) + ' został powiadomiony o tym, że pomodliłeś się za niego. Dziękujemy' ), } elif event_type == 'send_message': return { user_id: utils.response_text( 'Użytkownik ' + utils.user_name(sender_id) + ' pamięta o Tobie w modlitwie w następującej intencji: ' + prayer_description), sender_id: utils.response_text( 'Użytkownik ' + utils.user_name(user_id) + ' został powiadomiony o tym, że pamiętasz o nim w modlitwie' ), } elif event_type == 'give_up': db.update_commiter(prayer_id, '') return { sender_id: utils.response_text( 'Dziękujemy za chęć modlitwy. Użytkownik ' + utils.user_name(user_id) + ' nie zostanie powiadomiony o Twojej rezygnacji'), }
def handle_prayer_event(sender_id, event_type, payload): user_id = payload['user_id'] prayer_id = payload['prayer_id'] user_name = utils.user_name(user_id) sender_name = utils.user_name(sender_id) # prayer = db.fetch(prayer_id) prayer = Intent.query.filter_by(id = prayer_id).one_or_none() prayer_description = prayer.description.encode("utf-8") if event_type == 'i_pray': # db.update_commiter(prayer_id, sender_id) prayer.commiter_id=sender_id db.session.commit() return { 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=prayer_description)), } elif event_type == 'did_pray': # db.delete(prayer_id) db.session.delete(prayer) db.session.commit() return { user_id : utils.response_text(user_gettext(user_id, 'User %(name)s has prayed in your request: %(desc)s', name=sender_name, desc=prayer_description)), sender_id : utils.response_text(user_gettext(sender_id, 'User %(name)s has been notified that you\'ve prayed for him/her. Thank you', name=user_name)), } elif event_type == 'send_message': return { user_id : utils.response_text(user_gettext(user_id, 'User %(name)s wants to ensure you about his prayer in the following request: %(desc)s', name=sender_name, desc=prayer_description)), sender_id : utils.response_text(user_gettext(sender_id, 'User %(name)s has been ensured that you pray for him', name=user_name)), } elif event_type == 'give_up': # db.update_commiter(prayer_id, '') prayer.commiter_id='' db.session.commit() return { sender_id : utils.response_text(user_gettext(sender_id, 'Thank you for your will of praying. User %(name)s won\'t be notified about you giving up.', name=user_name)), }
def handle_user_event(sender_id, event, payload): if event == UserEvent.UPDATE_PRAYER: 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_PRAYER: prayer_id = payload['prayer_id'] intent = Intent.query.filter_by(id=prayer_id).first() db.session.delete(intent) return { sender_id: utils.response_text( user_gettext(sender_id, u"I've deleted a prayer request")), } elif event == UserEvent.PRAY_FOR_ME: intent = Intent(sender_id, '') intent.ts = 1234 db.session.add(intent) return { sender_id: utils.response_text( user_gettext(sender_id, u"What is your prayer request?")), } elif event == UserEvent.WANT_TO_PRAY: prayers = Intent.query.filter_by( commiter_id='').limit(displayed_prayers_limit).all() #print('Fetched prayers: ' + json.dumps(prayers)) 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), } elif event == UserEvent.MY_PRAYERS: 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), }
def test_response_text(self): text = "unit tests are so cool" res = utils.response_text(text) assert res == json.dumps({"text": text})
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_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")), }
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_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_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() intent_description = intent.description.encode('utf-8') if event == PrayerEvent.I_PRAY: intent.commiter_id = sender_id return { 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: db.session.delete(intent) return { 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)), 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)), } elif event == PrayerEvent.SEND_MESSAGE: return { 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)), sender_id: utils.response_text( user_gettext( sender_id, u"User %(name)s has been ensured that you pray for him", name=user_name)), } elif event == PrayerEvent.GIVE_UP: intent.commiter_id = '' 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)), }
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), }