def next_responses(session_id, answer, domain, auth=None): if answer: xformsresponse = answer_question(session_id, _tf_format(answer), domain, auth) else: xformsresponse = next(session_id, domain, auth) for resp in _next_responses(xformsresponse, session_id, domain, auth): yield resp
def handle(self, msg): def _format(text): # touchforms likes ints to be ints so force it if necessary try: return int(text) except ValueError: return text def _next(xformsresponse, message, session): # if there's a valid session id (typically on a new form) # update our mapping session.modified_time = datetime.utcnow() session.save() if xformsresponse.event.type == "question": # send the next question message.respond(xformsresponse.event.text_prompt) if xformsresponse.event.datatype == "info": #We have to deal with Trigger/Label type messages expecting an 'ok' type response. So auto-send that and move on to the next question. response = api.answer_question(int(session.session_id),_format('ok')) return _next(response, message, session) return True elif xformsresponse.event.type == "form-complete": session.end_time = datetime.utcnow() session.ended = True session.save() form_complete.send(sender="smsforms", session=session, form=xformsresponse.event.output) return True response = None #check if this Connection is in a form session: session = self.get_session(msg) trigger = self.get_trigger_keyword(msg) if not trigger and not session: #obviously we hope that a trigger is not the zero ('0') character. return elif trigger and session: session.ended=True session.save() #mark old session as 'done' and follow process for creating a new one session = None if not trigger and session: trigger = session.trigger response = api.answer_question(int(session.session_id),_format(msg.text)) elif trigger and not session: session = self.create_session_and_save(msg, trigger) form = trigger.xform response = api.start_form_session(form.file.path) session.session_id = response.session_id session.save() return _next(response, msg, session)
def _next_responses(xformsresponse, session_id, auth=None): if xformsresponse.is_error: yield xformsresponse elif xformsresponse.event.type == "question": yield xformsresponse if xformsresponse.event.datatype == "info": # We have to deal with Trigger/Label type messages # expecting an 'ok' type response. So auto-send that # and move on to the next question. response = answer_question(int(session_id),'ok', auth) for additional_resp in _next_responses(response, session_id, auth): yield additional_resp elif xformsresponse.event.type == "form-complete": sms_form_complete.send(sender="touchforms", session_id=session_id, form=xformsresponse.event.output) yield xformsresponse
def _next_responses(xformsresponse, session_id, auth=None): if xformsresponse.is_error: yield xformsresponse elif xformsresponse.event.type == "question": yield xformsresponse if xformsresponse.event.datatype == "info": # We have to deal with Trigger/Label type messages # expecting an 'ok' type response. So auto-send that # and move on to the next question. response = answer_question(int(session_id), 'ok', auth) for additional_resp in _next_responses(response, session_id, auth): yield additional_resp elif xformsresponse.event.type == "form-complete": sms_form_complete.send(sender="touchforms", session_id=session_id, form=xformsresponse.event.output) yield xformsresponse
def _next(xformsresponse, message, session): # if there's a valid session id (typically on a new form) # update our mapping session.modified_time = datetime.utcnow() session.save() if xformsresponse.event.type == "question": # send the next question message.respond(xformsresponse.event.text_prompt) if xformsresponse.event.datatype == "info": #We have to deal with Trigger/Label type messages expecting an 'ok' type response. So auto-send that and move on to the next question. response = api.answer_question(int(session.session_id),_format('ok')) return _next(response, message, session) return True elif xformsresponse.event.type == "form-complete": session.end_time = datetime.utcnow() session.ended = True session.save() form_complete.send(sender="smsforms", session=session, form=xformsresponse.event.output) return True
def next_responses(session_id, answer, auth=None): xformsresponse = answer_question(session_id, _tf_format(answer), auth) for resp in _next_responses(xformsresponse, session_id, auth): yield resp