示例#1
0
def do(self, test=False):
    """Send the SMS message."""

    client = self.dyad.study.twilio_number.client()

    success = -1
    tem = Template(self.created_by_script.script_body)
    con = Context(self.create_observation_context())
    message = tem.render(con)
    to_number = international_string(self.user.userprofile.mobile)
    from_number = self.dyad.study.twilio_number.number()

    try:
        success = 1
        result = client.sms.messages.create(
            to=to_number,
            from_=from_number,
            body=message,
            status_callback=current_site_url() + reverse('sms_callback'),
        )

        self.add_data(key="external_id", value=result.sid)

    except TwilioException as e:
        success = -1
        result = str(e)
        self.add_data(key="failure", value=result)

    self.update(success)
    return (success, result)
示例#2
0
def do(self, test=False):
    """Send the SMS message."""

    client = self.dyad.study.twilio_number.client()

    success = -1
    tem = Template(self.created_by_script.script_body)
    con = Context(self.create_observation_context())
    message = tem.render(con)
    to_number = international_string(self.user.userprofile.mobile)
    from_number = self.dyad.study.twilio_number.number()

    try:
        success = 1
        result = client.sms.messages.create(to=to_number,
                        from_=from_number,
                        body=message,
                        status_callback=current_site_url() + reverse('sms_callback'),)

        self.add_data(key="external_id", value=result.sid)

    except TwilioException as e:
        success = -1
        result = str(e)
        self.add_data(key="failure", value=result)

    self.update(success)
    return (success, result)
示例#3
0
def initialise_call(request, observation_token):
    """Accept inbound POST from Twilio, make a Reply and redirect to play.
    :type request: a
    :type observation_token: string
    :rtype: b
    """

    observation = get_object_or_404(Observation, token=observation_token)
    reply = observation.make_reply(request, entry_method="twilio",
        external_id=request.POST.get('CallSid', None))
    url = current_site_url() + reverse('play', args=(reply.token, 0))
    return HttpResponseRedirect(url)
示例#4
0
    def create_observation_context(self):
        """Return a dictionary which can be used as in a Context() call to fill in templates."""

        url = None
        if self.link():
            url = current_site_url() + self.link()

        return {
            'url': url,
            'observation': self,
            'study': self.dyad.study,
            'script': self.created_by_script,
            'user': self.dyad.user,
            'userprofile': self.dyad.user.userprofile,
        }
示例#5
0
    def create_observation_context(self):
        """Return a dictionary which can be used as in a Context() call to fill in templates."""

        url = None
        if self.link():
            url = current_site_url() + self.link()

        return {
            'url': url,
            'observation': self,
            'study': self.dyad.study,
            'script': self.created_by_script,
            'user': self.dyad.user,
            'userprofile': self.dyad.user.userprofile,
        }
示例#6
0
    def _send_sms_reminder(self):

        client = self.observation.dyad.study.twilio_number.client()

        to_number = self.observation.user.userprofile.mobile()
        from_number = self.observation.dyad.study.twilio_number.number()
        _, message = hlp.format_message_content("", self.reminder.message, self.observation)

        try:
            result = client.sms.messages.create(to=to_number, from_=from_number, body=message,
                    status_callback=current_site_url() + reverse('sms_callback'))
            self.observation.add_data(key="external_id", value=result.sid)
            self.observation.add_data(key="reminder",
                value="%s (sent to number ending %s)" % (message, to_number[-3:]))

            return (True, str(result.sid))

        except TwilioException as e:
            return (False, str(e))
示例#7
0
    def _send_sms_reminder(self):

        client = self.observation.dyad.study.twilio_number.client()

        to_number = self.observation.user.userprofile.mobile()
        from_number = self.observation.dyad.study.twilio_number.number()
        _, message = hlp.format_message_content("", self.reminder.message,
                                                self.observation)

        try:
            result = client.sms.messages.create(
                to=to_number,
                from_=from_number,
                body=message,
                status_callback=current_site_url() + reverse('sms_callback'))
            self.observation.add_data(key="external_id", value=result.sid)
            self.observation.add_data(key="reminder",
                                      value="%s (sent to number ending %s)" %
                                      (message, to_number[-3:]))

            return (True, str(result.sid))

        except TwilioException as e:
            return (False, str(e))
示例#8
0
def link(self):
    """Return the uri of the first Twiml page for Twilio to consume."""
    twiml = current_site_url() + reverse('initialise_call', args=(self.token,))
    return twiml
示例#9
0
def play(request, reply_token, question_index=0):
    """Rewritten controller to present Twiml for each question and save responses."""

    question_index = int(question_index)
    reply = Reply.objects.get(token=reply_token)
    if not reply:
        return reply_to_twilio(say_or_play_phrase(twiml.Response(),
            "No reply found for this token."))
    asker = reply.asker

    repeat_url = current_site_url() + reverse('play', args=(reply.token, question_index))
    next_question_url = current_site_url() + reverse('play', args=(reply.token, question_index + 1))

    questions = asker.questions(reply=reply)

    try:
        thequestion = questions.pop(question_index)
    except IndexError:
        raise TwilioBoxException("There is no question {}".format(question_index))

    if thequestion.index() == len(questions):
        # mark the observation and reply as complete because the last
        # question is required to be a hangup type which doesn't collect
        # any data
        _ = reply.observation and reply.observation.update(1)
        _ = reply.finish(request)

    if not request.POST:
        vfun = thequestion.field_class()(thequestion, reply, request).voice_function
        return reply_to_twilio(
            # note, we will post to the same url as displays the question
            vfun(twiml.Response(), thequestion, repeat_url, reply)
        )
    else:
        answer = save_answer(reply, thequestion, request.POST)
        was_suitable = thequestion.check_telephone_keypad_answer(answer.answer)

        if was_suitable:
            # only move on to the next question if we have a good response
            return HttpResponseRedirect(next_question_url)
        # if the user responds with a * then repeat the question again
        elif answer.answer == "*":
            response = twiml.Response()
            say_or_play_phrase(response, asker.get_phrase('repeating'))
            response.redirect(url=repeat_url, method="GET")
            return reply_to_twilio(response)

        else:  # if it wasn't a suitable response
            reply.add_data('question_error', thequestion.variable_name)
            reply.add_data('incorrect_response', answer.answer)

            errors = reply.replydata_set.filter(key='question_error', value=thequestion.variable_name)
            response = twiml.Response()
            if errors.count() >= MAX_QUESTION_ERRORS:
                say_or_play_phrase(response, asker.get_phrase('you_said'))
                say_or_play_phrase(response, str(answer.answer))
                say_or_play_phrase(response, asker.get_phrase('unsuitable'))
                say_or_play_phrase(response, asker.get_phrase('skipping'))
                response.redirect(url=next_question_url, method="GET")
                return reply_to_twilio(response)
            else:
                say_or_play_phrase(response, asker.get_phrase('unsuitable'))
                say_or_play_phrase(response, asker.get_phrase('repeating'))
                response.redirect(url=repeat_url, method="GET")
                return reply_to_twilio(response)