def inbound_handler(): """ The inbound request handler for consuming HTTP wrapped SMS content from Flowroute's messaging service. Validates proper message content, and takes time to clear the expired reminders before continuing. Retrieves the reminder based on the sender, and marks the 'will_attend' attribute according to whether there is 'yes' or 'no' (case insensitive) anywhere in the message. Responds to the user with a confirmation message and returns 200. """ req = request.json # Take the time to clear out any past reminders try: virtual_tn = req['to'] assert len(virtual_tn) <= 18 sms_from = req['from'] assert len(sms_from) <= 18 req['body'] except (TypeError, KeyError, AssertionError) as e: msg = ("Malformed inbound message: {}".format(req)) log.error({"message": msg, "status": "failed", "exc": str(e)}) return Response('There was an issue parsing your request.', status=400) else: Reminder.clean_expired() try: appt = Reminder.query.filter_by( contact_num=sms_from).one() except NoResultFound: msg = "No existing un-responded reminder for contact {}.".format( sms_from) log.info({"message": msg}) return Response(status=200) else: message = req['body'].upper() if 'YES' in message: appt.will_attend = True confirm = True elif 'NO' in message: appt.will_attend = False confirm = False else: confirm = None db_session.add(appt) try: send_reply.apply_async((appt.id,), {'confirm': confirm}) except ConnectionError as e: log.critical({"message": "unable to connect to redis", "exc": type(e)}) db_session.rollback() return Response(status=500) else: db_session.commit() log.info({"message": ("successfully recorded response from {}, scheduled " "SMS confirmation for appointment {}").format( sms_from, appt.id), "reminder_id": appt.id}) return Response(status=200)
def setup_function(function): if TEST_DB in app.config['SQLALCHEMY_DATABASE_URI']: db_session.rollback() Reminder.query.delete() db_session.commit() else: raise AttributeError(("The production database is turned on. " "Flip settings.DEBUG to True"))
def inbound_handler(): """ The inbound request handler for consuming HTTP wrapped SMS content from Flowroute's messaging service. Validates proper message content, and takes time to clear the expired reminders before continuing. Retrieves the reminder based on the sender, and marks the 'will_attend' attribute according to whether there is 'yes' or 'no' (case insensitive) anywhere in the message. Responds to the user with a confirmation message and returns 200. """ req = request.json # Take the time to clear out any past reminders try: virtual_tn = req['to'] assert len(virtual_tn) <= 18 sms_from = req['from'] assert len(sms_from) <= 18 req['body'] except (TypeError, KeyError, AssertionError) as e: msg = ("Malformed inbound message: {}".format(req)) log.error({"message": msg, "status": "failed", "exc": str(e)}) return Response('There was an issue parsing your request.', status=400) else: Reminder.clean_expired() try: appt = Reminder.query.filter_by(contact_num=sms_from).one() except NoResultFound: msg = "No existing un-responded reminder for contact {}.".format( sms_from) log.info({"message": msg}) return Response(status=200) else: message = req['body'].upper() if 'YES' in message: appt.will_attend = True confirm = True elif 'NO' in message: appt.will_attend = False confirm = False else: confirm = None db_session.add(appt) try: send_reply.apply_async((appt.id, ), {'confirm': confirm}) except ConnectionError as e: log.critical({ "message": "unable to connect to redis", "exc": type(e) }) db_session.rollback() return Response(status=500) else: db_session.commit() log.info({ "message": ("successfully recorded response from {}, scheduled " "SMS confirmation for appointment {}").format( sms_from, appt.id), "reminder_id": appt.id }) return Response(status=200)