コード例 #1
0
def connect_call(request):
    sid = request.POST['CallSid']
    caller = request.POST['Caller']
    r = twilio.Response()
    log = Click2Call_Log.objects.filter(
        caller_number=caller,
        timestamp__gt=datetime.now() -
        timedelta(minutes=3)).order_by('-timestamp')[:1]
    log = log[0] if len(log) else None

    if not log or log.connected:
        r.append(
            tts(
                _("Sorry, this number is a DoctorCOM public number. Please "
                  "call back the Answering Service.")))
        return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
    log.sid = sid
    log.connected = True
    log.save()

    provider_qs = Provider.objects.filter(mobile_phone=log.called_number)
    if provider_qs:
        provider = provider_qs.get()
        ProviderIVR_OutsideInit(request, log.caller_number, provider, log)
        return ProviderIVR_ForwardCall(request)

    # for privacy reasons caller id always our c2c number
    caller_id = settings.TWILIO_C2C_NUMBER

    dial = twilio.Dial(log.called_number,
                       action='',
                       callerId=caller_id,
                       timeout=120)
    r.append(dial)
    return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
コード例 #2
0
ファイル: iceinfo.py プロジェクト: stephica/iceinfo
 def nokmenu(self, var=None, **params):
     msisdn = urllib.quote(cherrypy.request.params['From'])
     item = int(urllib.quote(cherrypy.request.params['item']))
     digit = urllib.quote(cherrypy.request.params['Digits'])
     r = twiml.Response()
     r.say('thankyou')
     if digit == "1":
         r.redirect("/iceinfo/clinician/playnok?item=" + str(item))
     elif digit == "2":
         r.redirect("/iceinfo/clinician/playnok?item=" + str(item + 1))
     elif digit == "3":
         r.say(
             "You have reached the end of this entry. Press 1 to return to the beginning or hang up now."
         )
         r.gather(action="/iceinfo/clinician/completemenu",
                  numDigits=1,
                  timeout=15,
                  method="GET")
     elif digit == "4":
         noks = find(msisdn, 'nok')
         num = noks[item]['number']
         r.dial(number=num)
     elif digit == "5":
         num = ""
         noks = find(msisdn, 'nok')
         d = twiml.Dial(callerId="02033224232")
         for nok in noks:
             n = twiml.Number(nok['number'])
             d.append(n)
         r.append(d)
     return str(r)
コード例 #3
0
 def testAddConference(self):
     """ add a conference to a dial """
     r = Response()
     d = twiml.Dial()
     d.append(twiml.Conference("My Room"))
     r.append(d)
     r = self.strip(r)
     assert_equal(r, '<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Conference>My Room</Conference></Dial></Response>')
コード例 #4
0
 def testAddNumber(self):
     """ add a number to a dial """
     r = Response()
     d = twiml.Dial()
     d.append(twiml.Number("1231231234"))
     r.append(d)
     r = self.strip(r)
     assert_equal(r, '<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Number>1231231234</Number></Dial></Response>')
コード例 #5
0
 def testDial(self):
     """ should redirect the call"""
     r = Response()
     r.append(twiml.Dial("1231231234"))
     r = self.strip(r)
     self.assertEquals(
         r,
         '<?xml version="1.0" encoding="UTF-8"?><Response><Dial>1231231234</Dial></Response>'
     )
コード例 #6
0
 def testImproperNesting(self):
     """ bad nesting """
     verb = twiml.Gather()
     self.assertRaises(TwimlException, verb.append, twiml.Gather())
     self.assertRaises(TwimlException, verb.append, twiml.Record())
     self.assertRaises(TwimlException, verb.append, twiml.Hangup())
     self.assertRaises(TwimlException, verb.append, twiml.Redirect())
     self.assertRaises(TwimlException, verb.append, twiml.Dial())
     self.assertRaises(TwimlException, verb.append, twiml.Conference(""))
     self.assertRaises(TwimlException, verb.append, twiml.Sms(""))
コード例 #7
0
ファイル: test_twiml.py プロジェクト: B-Rich/twilio-python
 def improperAppend(self, verb):
     self.assertRaises(TwimlException, verb.append, twiml.Say(""))
     self.assertRaises(TwimlException, verb.append, twiml.Gather())
     self.assertRaises(TwimlException, verb.append, twiml.Play(""))
     self.assertRaises(TwimlException, verb.append, twiml.Record())
     self.assertRaises(TwimlException, verb.append, twiml.Hangup())
     self.assertRaises(TwimlException, verb.append, twiml.Reject())
     self.assertRaises(TwimlException, verb.append, twiml.Redirect())
     self.assertRaises(TwimlException, verb.append, twiml.Dial())
     self.assertRaises(TwimlException, verb.append, twiml.Conference(""))
     self.assertRaises(TwimlException, verb.append, twiml.Sms(""))
     self.assertRaises(TwimlException, verb.append, twiml.Pause())
コード例 #8
0
ファイル: test_twiml.py プロジェクト: kazuar/twilio-python
 def testAddNumberStatusCallbackEvent(self):
     """ add a number to a dial with status callback events"""
     r = Response()
     d = twiml.Dial()
     d.append(
         twiml.Number("1231231234",
                      statusCallback="http://example.com",
                      statusCallbackEvent="initiated completed"))
     r.append(d)
     r = self.strip(r)
     assert_equal(
         r,
         '<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Number statusCallback="http://example.com" statusCallbackEvent="initiated completed">1231231234</Number></Dial></Response>'
     )
コード例 #9
0
def join(conference_uuid):
    """
    TwiML Endpoint for for Twilio join callbacks

    Has two modes depending on the value of POST variable 'Digits':
      - != 1: Ask the caller to press 1 to join the bridge
      - == 1: Join the caller to the conference bridge

    """
    if validate_twiml() == False:
        abort(403, 'Validation failed')
    log.debug("received join on {}".format(conference_uuid))
    conference = Conference(uuid=conference_uuid)
    conference_name = conference.get_name()
    log.debug(
        "using conference_uuid {}, determined conference_name is: {}".format(
            conference_uuid, conference_name))
    call_sid = request.forms.get('CallSid')
    log.debug("caller has call_sid: {}".format(call_sid))
    phone_number = request.forms.get('To')
    log.debug("caller has phone_number: {}".format(phone_number))
    r = twiml.Response()
    digits = request.forms.get('Digits')
    if digits:
        log.debug("caller has entered digits: {}".format(digits))
    if digits == '1':
        log.debug("caller has pressed one to join")
        conference.add(call_sid, phone_number)
        r.say("Now joining you to the conference bridge named, {}".format(
            conference_name))
        d = twiml.Dial()
        d.append(twiml.Conference(conference_uuid))
        r.append(d)
        log.info("caller is now entering conference_uuid: {}".format(
            conference_uuid))
        return str(r)
    else:
        log.info("caller will be prompted to join")
        with r.gather(timeout=10, numDigits=1) as g:
            g.say(
                "You have been requested to join the conference bridge named, {}"
                .format(conference_name))
            g.say("To accept, please press one")
        r.redirect('{}/twiml/join/{}'.format(
            application.config.Twiml.callback_base_url, conference_uuid),
                   method='POST')
        log.debug("sending join prompt now")
        log.debug("sending twiml: {}".format(str(r)))
        return str(r)
コード例 #10
0
ファイル: iceinfo.py プロジェクト: stephica/iceinfo
 def menu(self, var=None, **params):
     msisdn = urllib.quote(cherrypy.request.params['From'])
     digits = urllib.quote(cherrypy.request.params['Digits'])
     if digits == "1":
         r = twiml.Response()
         r.redirect("/iceinfo/clinician/history")
     elif digits == "2":
         r = twiml.Response()
         r.play(urllib.unquote(find(msisdn, 'name')))
         r.say(
             "has registered" + str(find(msisdn, 'nokcount')) +
             "contacts, ICE Info will now ring these contacts and connect you to the first to answer."
         )
         num = ""
         noks = find(msisdn, 'nok')
         d = twiml.Dial(callerId="02033224232")
         for nok in noks:
             n = twiml.Number(nok['number'])
             d.append(n)
         r.append(d)
     return str(r)
コード例 #11
0
ファイル: views_generic.py プロジェクト: cnzhuran/mdcom
def callBackUser(request, message):
	callerId = Provider.objects.get(id=request.session['provider_id']).mdcom_phone
	r = twilio.Response()
	callSID = request.POST['CallSid']
	log = callLog.objects.get(callSID=callSID)
	if(message.urgent):
		log.call_source = 'CB'
	else:
		log.call_source = 'CC'
	log.save()
	provider_qs = Provider.objects.filter(mobile_phone=message.callback_number)
	if(provider_qs):
		from views_provider import ProviderIVR_OutsideInit, ProviderIVR_ForwardCall
		provider = provider_qs.get()
		ProviderIVR_OutsideInit(request, log.caller_number, provider, log)
		return ProviderIVR_ForwardCall(request)
	dial = twilio.Dial(message.callback_number,
				callerId=callerId,
				timeout=120)
	r.append(dial)
	CallbackLog(message=message).save()
	return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
コード例 #12
0
 def joinroom(self, var=None, **params):
     room = str(urllib.unquote(cherrypy.request.params['room']))
     user = str(urllib.unquote(cherrypy.request.params['user']))
     if room == "welcome-test" and user == "guest-test":
         r = twiml.Response()
         r.say(
             "Please record a short message after the tone, it will then be played back to you"
         )
         r.record(action="http://voxirc.sammachin.com/twiliotest/recorded",
                  maxLength="6")
     else:
         print user + " entered " + room
         leaveurl = "http://ec2.sammachin.com/pusher2talk/leaveroom?room={0}&user={1}".format(
             room, user)
         c = twiml.Conference(room, waitUrl="", beep="false")
         r = twiml.Response()
         d = twiml.Dial(action=leaveurl)
         d.append(c)
         r.append(d)
         p = pusher.Pusher()
         p["presence-" + room].trigger('join', {'user': user})
         adduser(room, user)
     return str(r)
コード例 #13
0
def twiMLCall_callback(request):
    form = TwiMLCallbackForm(request.POST)
    r = twilio.Response()
    if (not form.is_valid()):
        r.append(
            tts(
                _("A system error has occurred. "
                  "Please contact the administrator or try it later.")))
        return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)

    callSid = form.cleaned_data["CallSid"]
    caller_user_id = form.cleaned_data["caller_user_id"]
    called_number = form.cleaned_data["called_number"]
    called_user_id = form.cleaned_data["called_user_id"]
    called_practice_id = form.cleaned_data["called_practice_id"]

    # decide called number or called user.
    called_user = None
    if called_user_id:
        called_users = MHLUser.objects.filter(pk=called_user_id)
        if (not called_users):
            r.append(tts(_("The person you called doesn't exist.")))
            return HttpResponse(str(r),
                                mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
        called_user = called_users[0]
        called_number = called_user.mobile_phone
    elif called_practice_id:
        called_practice = PracticeLocation.objects.filter(
            id=called_practice_id)
        if not called_practice:
            r.append(tts(_("The practice you called doesn't exist.")))
            return HttpResponse(str(r),
                                mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
        called_number = called_practice[0].practice_phone

    # decide which number is caller_number.
    caller_mhluser = MHLUser.objects.get(pk=caller_user_id)
    caller_provider = user_is_provider(caller_mhluser)

    caller_manager = None
    caller_mgrs = Office_Manager.objects.filter(user__user=caller_mhluser)
    if caller_mgrs:
        caller_manager = caller_mgrs[0]

    caller_number = caller_mhluser.mobile_phone
    if not caller_provider and caller_manager:
        staffs = OfficeStaff.objects.filter(user=caller_mhluser)
        if (staffs.count() != 1):
            pass
#
        staff = staffs[0]
        if staff.caller_anssvc == 'MO':
            caller_number = staff.user.mobile_phone
        elif staff.caller_anssvc == 'OF':
            caller_number = staff.office_phone
        elif staff.caller_anssvc == 'OT':
            caller_number = staff.user.phone
        else:
            pass

    log = Click2Call_Log(caller=caller_mhluser,
                         caller_number=caller_number,
                         callid=callSid,
                         called_number=called_number,
                         called_user=called_user,
                         source='APP')
    log.save()

    # decide which number is caller_id.
    caller_id = settings.TWILIO_CALLER_ID
    if (caller_provider and caller_provider.mdcom_phone):
        caller_id = caller_provider.mdcom_phone
    else:
        if caller_manager:
            if caller_manager and caller_manager.user.current_practice and \
              caller_manager.user.current_practice.mdcom_phone:
                caller_id = caller_manager.user.current_practice.mdcom_phone

    # check called person/number, decide call process
    if called_user:
        called_provider = user_is_provider(called_user)
        if (not called_provider):
            #office managers can get phone calls too, but they must have mobile phone
            manager_info = OfficeStaff.objects.filter(user=called_user)
            if (manager_info.count() > 0
                    and manager_info[0].user.mobile_phone):
                called_manager = manager_info[0]

        if (called_provider):
            # Send the call through the IVR
            ProviderIVR_OutsideInit(request, log.caller_number,
                                    called_provider, log)
            request.session['Caller'] = caller_id
            return ProviderIVR_ForwardCall(request)
        elif called_manager:
            # Send the call through the IVR
            from MHLogin.DoctorCom.IVR.views_practice import \
             PracticeIVR_ForwardCall, PracticeIVR_OutsideInit
            PracticeIVR_OutsideInit(request, log.caller_number, called_manager,
                                    log)
            request.session['click2call'] = True
            request.session['Caller'] = caller_id
            return PracticeIVR_ForwardCall(request)

    dial = twilio.Dial(called_number, callerId=caller_id, timeout=120)
    r.append(dial)
    return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
コード例 #14
0
ファイル: views_provider_v2.py プロジェクト: cnzhuran/mdcom
def ProviderIVR_ForwardCall_New(request, provider=None, twilioResponse=None):
    """
	new version of ProviderIVR_ForwardCall - forward call to dialed user as per user preference
	called by ProviderIVR_Main_New
	Steps:
	1. get name
	2. Dial - based on provider.forward_voicemail state to Mobile, office, other (VM already handled)
	3. After dialed: handle failure by going to LeaveMsg or hangup?
	"""
    assert (request.session['provider_id'])
    provider = Provider.objects.get(id=request.session['provider_id'])
    logger.debug(
        '%s: ProviderIVR_ForwardCall_New is initiated provider %s state %s substate %s POST %s'
        % (request.session.session_key, provider,
           request.session.get('ivr2_state', None),
           request.session.get('ivr2_sub_state', None), str(request.POST)))
    callEnded = _checkCallbackDuration(request, False)

    # check user forwarding preference
    forward = provider.forward_voicemail
    if (forward == 'VM'):
        # go to voicemail
        request.session['ivr2_state'] = 'ProviderIVR_LeaveMsg_New'
        logger.debug(
            '%s: ProviderIVR_ForwardCall_New forwarding to voicemail of %s' %
            (request.session.session_key, provider))
        return ProviderIVR_LeaveMsg_New(request)

    # State processing - 1. get caller name or ask for it
    r = twilioResponse or twilio.Response()
    if ('ivr2_sub_state' not in request.session):
        request.session['ivr2_sub_state'] = 'ProviderIVR_ForwardCall_Start'
    else:
        logger.debug(
            '%s: ProviderIVR_ForwardCall_New sub_state %s' %
            (request.session.session_key, request.session['ivr2_sub_state']))
    if (request.session['ivr2_sub_state'] == 'ProviderIVR_ForwardCall_Start'):
        request.session['ivr2_sub_state'] = 'ProviderIVR_ForwardCall_GetName'
        # is this a doctorcom user with recorded name?
        callSID = request.POST['CallSid']
        log = callLog.objects.get(callSID=callSID)
        if (log.mdcom_caller and isinstance(log.mdcom_caller, Provider)):
            if (log.mdcom_caller.vm_config.count()):
                prov_vmconfig = log.mdcom_caller.vm_config.get()
                if (prov_vmconfig.config_complete):
                    logger.debug('%s/%s: Found the caller\'s name!' % (
                        request.session.session_key,
                        request.session['ivr2_state'],
                    ))
                    log.caller_spoken_name = prov_vmconfig.name
                    log.save()
                    # got the recorded name; just go back to the next step - Dial
                    return ProviderIVR_ForwardCall_New(request, provider,
                                                       r)  # next step
                else:
                    logger.debug(
                        '%s/%s: ProviderIVR_ForwardCall_New GetName: Caller\'s '
                        'vm_config incomplete!' %
                        (request.session.session_key,
                         request.session['ivr2_state']))
            else:
                logger.debug(
                    '%s/%s: ProviderIVR_ForwardCall_New GetName: unsuitable '
                    'number of vm_config objects found: %i' %
                    (request.session.session_key,
                     request.session['ivr2_state'],
                     log.mdcom_caller.vm_config.count()))
        else:
            logger.debug(
                '%s/%s: ProviderIVR_ForwardCall_New GetName: mdcom_caller %s '
                'either isn\'t defined or doesn\'t seem to be a Provider' %
                (request.session.session_key, request.session['ivr2_state'],
                 str(log.mdcom_caller)))

        # Not a user with a name recording. Get one.
        # ivr2_state already set to ProviderIVR_ForwardCall_GetName
        request.session[
            'ivr2_Record_prompt_str'] = 'Please say your name after the tone.'
        request.session['ivr2_Record_maxLength'] = 4
        request.session['ivr2_Record_timeout'] = 2
        request.session['ivr2_Record_leadSilence'] = 1
        return getQuickRecordingNew(request)

    elif (request.session['ivr2_sub_state'] ==
          'ProviderIVR_ForwardCall_GetName'):
        request.session['ivr2_sub_state'] = 'ProviderIVR_ForwardCall_Dial'
        # save the caller name
        callSID = request.POST['CallSid']
        log = callLog.objects.get(callSID=callSID)
        if (not log.caller_spoken_name):
            log.caller_spoken_name = request.session['ivr2_Record_recording']
            del request.session['ivr2_Record_recording']
            log.save()
        # Okay, let's find number to dial!
        user_number = None
        if (forward == 'MO'):
            user_number = provider.user.mobile_phone
        elif (forward == 'OF'):
            user_number = provider.office_phone
        elif (forward == 'OT'):
            user_number = provider.user.phone
        logger.debug(
            '%s/%s: ProviderIVR_ForwardCall_New Dial user number is \'%s\' forward %s'
            % (request.session.session_key, request.session['ivr2_state'],
               user_number, forward))

        if (not user_number):
            # no flags were set.
            if (provider.user.mobile_phone):
                user_number = provider.user.mobile_phone
            else:
                return ProviderIVR_LeaveMsg_New(request, r)
        # when dial action is done, we go back to its actionurl - which is here with state Dial
        dial = twilio.Dial(
            action=reverse('ProviderIVR_ForwardCall_New'),
            timeout=22,
            timeLimit=14400,  # 4 hours
            callerId=_makeUSNumber(request.session['Caller']))
        # we also want to allow call vetting
        dial.append(
            twilio.Number(user_number,
                          url=reverse('ProviderIVR_ForwardCall_Vet')))
        r.append(dial)
        # If the call did not connect, we go to LeaveMsg
        r.append(twilio.Redirect(reverse('ProviderIVR_LeaveMsg_New')))
        return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)

    elif (request.session['ivr2_sub_state'] == 'ProviderIVR_ForwardCall_Dial'):
        # done with forward call
        del request.session['ivr2_sub_state']
        # need to get parent call log for caller and connected; also update child log for duration
        (log, plog) = _getCallLogOrParent(request)
        if log:
            logger.debug(
                '%s/%s: ProviderIVR_ForwardCall_New state connected %s' %
                (request.session.session_key, request.session['ivr2_state'],
                 str(log.call_connected)))
        else:
            logger.debug(
                '%s/%s: ProviderIVR_ForwardCall_New state no log SID %s' %
                (request.session.session_key, request.session['ivr2_state'],
                 request.POST['CallSid']))
        if ('DialCallStatus' in request.POST):
            if (request.POST['DialCallStatus'] == 'completed'):
                # update child log call duration
                diallog = callLog.objects.get(
                    callSID=request.POST['DialCallSid'])
                if diallog:
                    diallog.call_duration = request.POST['DialCallDuration']
                    diallog.save()
                    logger.debug(
                        '%s: ProviderIVR_ForwardCall_New update child diallog '
                        'dialSid %s duration %s' %
                        (request.session.session_key,
                         request.POST['DialCallSid'],
                         request.POST['DialCallDuration']))
                else:
                    logger.debug(
                        '%s: ProviderIVR_ForwardCall_New diallog not found: dialSid %s duration %s'
                        % (request.session.session_key,
                           request.POST['DialCallSid'],
                           request.POST['DialCallDuration']))
                # Do nothing so that the second leg call continues un-interrupted
                return HttpResponse(str(r),
                                    mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
            else:
                # (request.POST['DialCallStatus'] != 'completed'):
                logger.debug(
                    '%s/%s: ProviderIVR_ForwardCall_New DialStatus not answered'
                    % (request.session.session_key,
                       request.session['ivr2_state']))
                if (request.POST['DialCallStatus'] == 'failed'):
                    # TODO: Figure out how to deal with connection problems. Most
                    # likely, send an email to the user and administrators.
                    logger.debug(
                        '%s/%s: ProviderIVR_ForwardCall_New DialStatus failed'
                        % (request.session.session_key,
                           request.session['ivr2_state']))
                    subject = 'ProviderIVR_ForwardCall Call Forward DialStatus Fail'
                    message = 'ProviderIVR_ForwardCall got DialStatus failed. Post data: %s' \
                     % (str(request.POST),)
                    mail_admins(subject=subject,
                                message=message,
                                fail_silently=False)
                # else request.POST['DialStatus'] == 'busy' or request.POST['DialStatus'] == 'no-answer'
                return ProviderIVR_LeaveMsg_New(request, r)
        # if we connected (in Vet), we hang up here
        if (log.call_connected):
            r.append(twilio.Hangup())
            return HttpResponse(str(r),
                                mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
        # if not connected, we go to LeaveMsg
        return ProviderIVR_LeaveMsg_New(request, r)
コード例 #15
0
ファイル: views_provider.py プロジェクト: cnzhuran/mdcom
def ProviderIVR_ForwardCall(request):
    """Forward the call to the dialed user, as per the user's preferences.

	Uses the following session variables:
		ProviderIVR_ForwardCall_state: The most recent "state" of execution. May
			have the following values:
				- None/Undefined/Empty String: First execution of this function.
				- Getting_Name: Getting the caller's name, if one hasn't been
						defined.
				- Dialing: Phone(s) have been dialed, and waiting for a response Caller
	"""
    r = twilio.Response()
    request.session.modified = True
    provider = Provider.objects.get(id=request.session['provider_id'])

    if ('CallStatus' in request.POST
            and request.POST['CallStatus'] == 'completed'):
        if ('Duration' in request.POST):
            callSID = request.POST['CallSid']
            log = callLog.objects.get(callSID=callSID)
            log.call_duration = request.POST['Duration']
            log.save()

    if (not 'ProviderIVR_ForwardCall_forward' in request.session):
        request.session[
            'ProviderIVR_ForwardCall_forward'] = provider.forward_voicemail
    forward = provider.forward_voicemail
    if (forward == 'VM'):
        return ProviderIVR_LeaveMsg(request)

    if (not 'ProviderIVR_ForwardCall_state' in request.session
            or not request.session['ProviderIVR_ForwardCall_state']):
        # New call. First, check to see if we should go straight to voicemail

        # Okay, the call isn't going to voicemail directly. Now, set state.
        request.session['ProviderIVR_ForwardCall_state'] = 'Getting_Name'

        # Now, get the caller's name

        # Is the user a DoctorCom user with a recorded name?
        callSID = request.POST['CallSid']
        log = callLog.objects.get(callSID=callSID)

        if (log.mdcom_caller and isinstance(log.mdcom_caller, Provider)):
            if (log.mdcom_caller.vm_config.count()):
                prov_vmconfig = log.mdcom_caller.vm_config.get()
                if (prov_vmconfig.config_complete):
                    logger.debug('%s/%s: Found the caller\'s name!' % (
                        request.session.session_key,
                        request.session['ProviderIVR_ForwardCall_state'],
                    ))
                    log.caller_spoken_name = prov_vmconfig.name
                    log.save()
                    return ProviderIVR_ForwardCall(
                        request)  # restart execution of this function
                else:
                    logger.debug('%s/%s: Caller\'s vm_config incomplete!' % (
                        request.session.session_key,
                        request.session['ProviderIVR_ForwardCall_state'],
                    ))
            else:
                logger.debug(
                    '%s/%s: An unsuitable number of vm_config objects found: %i'
                    % (
                        request.session.session_key,
                        request.session['ProviderIVR_ForwardCall_state'],
                        log.mdcom_caller.vm_config.count(),
                    ))
        else:
            logger.debug(
                '%s/%s: mdcom_caller %s either isn\'t defined or doesn\'t '
                'seem to be a Provider' % (
                    request.session.session_key,
                    request.session['ProviderIVR_ForwardCall_state'],
                    str(log.mdcom_caller),
                ))

        # Okay, it's not a user with a name recording. Get one.
        request.session['ivr_call_stack'].append('ProviderIVR_ForwardCall')
        request.session['ivr_makeRecording_prompt'] = \
         tts('Please say your name after the tone.')
        request.session['ivr_makeRecording_maxLength'] = 4
        request.session['ivr_makeRecording_timeout'] = 2
        request.session['ivr_makeRecording_leadSilence'] = 1
        return getQuickRecording(request)

    if (request.session['ProviderIVR_ForwardCall_state'] == 'Getting_Name'):
        request.session['ProviderIVR_ForwardCall_state'] = 'Dialed'

        logger.debug('%s/%s: Set session to %s' % (
            request.session.session_key,
            request.session['ProviderIVR_ForwardCall_state'],
            request.session['ProviderIVR_ForwardCall_state'],
        ))

        callSID = request.POST['CallSid']
        log = callLog.objects.get(callSID=callSID)
        if (not log.caller_spoken_name):
            log.caller_spoken_name = request.session.pop(
                'ivr_makeRecording_recording')
            log.save()

        logger.debug('%s/%s: got provider \'%s\' \'%s\' with id %s' % (
            request.session.session_key,
            request.session['ProviderIVR_ForwardCall_state'],
            provider.first_name,
            provider.last_name,
            provider.pk,
        ))
        logger.debug(
            '%s/%s: Provider phone is \'%s\' and forward_other is \'%s\'' % (
                request.session.session_key,
                request.session['ProviderIVR_ForwardCall_state'],
                provider.user.phone,
                str(provider.forward_other),
            ))

        # Okay, let's dial!
        user_number = None
        if (forward == 'MO'):
            logger.debug('%s/%s: provider.forward_mobile True' % (
                request.session.session_key,
                request.session['ProviderIVR_ForwardCall_state'],
            ))
            user_number = provider.user.mobile_phone
            logger.debug('%s/%s: Setting user_number to \'%s\'' % (
                request.session.session_key,
                request.session['ProviderIVR_ForwardCall_state'],
                provider.user.mobile_phone,
            ))
        elif (forward == 'OF'):
            logger.debug('%s/%s: provider.forward_office True' % (
                request.session.session_key,
                request.session['ProviderIVR_ForwardCall_state'],
            ))
            user_number = provider.office_phone
            logger.debug('%s/%s: Setting user_number to \'%s\'' % (
                request.session.session_key,
                request.session['ProviderIVR_ForwardCall_state'],
                provider.office_phone,
            ))
        elif (forward == 'OT'):
            logger.debug('%s/%s: provider.forward_other True' % (
                request.session.session_key,
                request.session['ProviderIVR_ForwardCall_state'],
            ))
            user_number = provider.user.phone
            logger.debug('%s/%s: Setting user_number to \'%s\'' % (
                request.session.session_key,
                request.session['ProviderIVR_ForwardCall_state'],
                provider.user.phone,
            ))

        logger.debug('%s/%s: Tried to get called\'s number. Got \'%s\'' % (
            request.session.session_key,
            request.session['ProviderIVR_ForwardCall_state'],
            user_number,
        ))
        logger.debug(
            '%s/%s: Provider phone is \'%s\' and forward_other is \'%s\'' % (
                request.session.session_key,
                request.session['ProviderIVR_ForwardCall_state'],
                provider.user.phone,
                str(provider.forward_other),
            ))

        if (not user_number):
            # no flags were set.
            if (provider.user.mobile_phone):
                user_number = provider.user.mobile_phone
            else:
                return ProviderIVR_LeaveMsg(request)

        dial = twilio.Dial(
            action=reverse('ProviderIVR_ForwardCall'),
            timeout=22,
            timeLimit=14400,  # 4 hours
            callerId=request.session['Caller'])
        dial.append(
            twilio.Number(user_number,
                          url=reverse('ProviderIVR_ForwardCall_VetAnswer')))
        r.append(dial)
        r.append(twilio.Redirect(reverse('ProviderIVR_LeaveMsg')))
        return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)

    if (request.session['ProviderIVR_ForwardCall_state'] == 'Dialed'):
        callSID = request.POST['CallSid']
        log = callLog.objects.get(callSID=callSID)

        if (log.call_connected):
            r.append(twilio.Hangup())
            return HttpResponse(str(r),
                                mimetype=settings.TWILIO_RESPONSE_MIMETYPE)

        return ProviderIVR_LeaveMsg(request)

    r = twilio.Response()
    if (request.POST['DialStatus'] != 'answered'):
        if (request.POST['DialStatus'] == 'failed'):
            # TODO: Figure out how to deal with connection problems. Most
            # likely, send an email to the user and administrators.
            subject = 'ProviderIVR_ForwardCall Call Forward DialStatus Fail'
            message = 'ProviderIVR_ForwardCall got DialStatus failed. Post data: %s' % \
             (str(request.POST),)
            mail_admins(subject=subject, message=message, fail_silently=False)
        # else request.POST['DialStatus'] == 'busy' or request.POST['DialStatus'] == 'no-answer'
        return ProviderIVR_LeaveMsg(request)
    # else: Do nothing so that the call continues un-interrupted
    return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
コード例 #16
0
def PracticeIVR_ForwardCall_New(request, twilioResponse=None):
	"""
	Forward the call to the dialed user, there are no preferences
	for office managers, just call their cell phone
	Done in 3 steps:
	1. get caller name
	2. forward call
	3. if connected, done; if fail to connect, go to LeaveUrgentMsg_New
	"""
	provider = None
	forward = None
	try:
		provider = Provider.objects.get(id=request.session['provider_id'])
		if(not 'ProviderIVR_ForwardCall_forward' in request.session):
			request.session['ProviderIVR_ForwardCall_forward'] = provider.forward_anssvc
		forward = provider.forward_anssvc
	except Provider.DoesNotExist:
		pass
	logger.debug('%s: PracticeIVR_ForwardCall_New provider %s practice %s state %s substate %s' % (
		request.session.session_key, provider, request.session.get('practice_id', None), 
		request.session.get('ivr2_state', None), request.session.get('ivr2_sub_state', None)))

	callSID = request.POST['CallSid']
	# TODO - what if we don't get the callLog?
	log = callLog.objects.get(callSID=callSID)
	log.call_source = 'AS'
	log.save()

	if('click2call' in request.session):
		forward = 'MO'  # hack to keep click2call working

	if(forward == 'VM'):
		request.session['ivr2_state'] = 'PracticeIVR_LeaveUrgentMsg_New'
		logger.debug('%s: PracticeIVR_ForwardCall_New forward to VM - LeaveUrgentMsg_New to %s' % (
			request.session.session_key, provider))
		return PracticeIVR_LeaveUrgentMsg_New(request)

	r = twilioResponse or twilio.Response()
	if ('ivr2_sub_state' not in request.session):
		request.session['ivr2_sub_state'] = 'PracticeIVR_ForwardCall_Start'
	else:
		logger.debug('%s: PracticeIVR_ForwardCall_New sub_state %s' % (
			request.session.session_key, request.session['ivr2_sub_state']))
	# now we get caller name, dial and handle failure to connect by going to leave urgent message
	if (request.session['ivr2_sub_state'] == 'PracticeIVR_ForwardCall_Start'):
		request.session['ivr2_sub_state'] = 'PracticeIVR_ForwardCall_GetName'

		# Is the user a DoctorCom user with a recorded name?
		if (log.mdcom_caller and isinstance(log.mdcom_caller, Provider)):
			if (log.mdcom_caller.vm_config.count()):
				prov_vmconfig = log.mdcom_caller.vm_config.get()
				if (prov_vmconfig.config_complete):
					logger.debug('%s/%s sub %s: Found the caller\'s name! Forwarding call' % (
							request.session.session_key, request.session['ivr2_state'],
							request.session['ivr2_sub_state'],
						))
					log.caller_spoken_name = prov_vmconfig.name
					log.save()
					return PracticeIVR_ForwardCall_New(request, r)  # restart execution of this function
				else:
					logger.debug('%s/%s sub %s: Caller\'s vm_config incomplete!' % (
							request.session.session_key, request.session['ivr2_state'],
							request.session['ivr2_sub_state'],
						))
			else:
				logger.debug('%s/%s sub %s: An unsuitable number of vm_config objects found: %i' % (
						request.session.session_key, request.session['ivr2_state'],
						request.session['ivr2_sub_state'],
						log.mdcom_caller.vm_config.count(),
					))
		else:
			logger.debug('%s/%s sub %s: mdcom_caller %s either isn\'t defined or doesn\'t seem to be a Provider' % (
					request.session.session_key, request.session['ivr2_state'],
					request.session['ivr2_sub_state'],
					str(log.mdcom_caller),
				))

		# Okay, it's not a user with a name recording. Get one.
		request.session['ivr2_Record_prompt_str'] = 'Please say your name after the tone.'
		request.session['ivr2_Record_maxLength'] = 4
		request.session['ivr2_Record_timeout'] = 2
		request.session['ivr2_Record_leadSilence'] = 1
		return getQuickRecordingNew(request)

	elif (request.session['ivr2_sub_state'] == 'PracticeIVR_ForwardCall_GetName'):
		request.session['ivr2_sub_state'] = 'PracticeIVR_ForwardCall_Dial'
		# save caller name
		logger.debug('%s/%s: Set session to %s' % (
				request.session.session_key, request.session['ivr2_state'],
				request.session['ivr2_sub_state']
			))
		if (not log.caller_spoken_name):
			log.caller_spoken_name = request.session['ivr2_Record_recording']
			del request.session['ivr2_Record_recording']
			log.save()
		# now find the number to dial
		user_number = ''
		try:
			office_staff = OfficeStaff.objects.get(user=request.session['provider_id'])  # manager being called
			logger.debug('%s/%s: got office staff \'%s\' \'%s\' with id %s office phone \'%s\'.' % (
					request.session.session_key,
					request.session['ivr2_state'],
					office_staff.user.first_name,
					office_staff.user.last_name,
					office_staff.pk,
					office_staff.user.mobile_phone,
				))
			user_number = office_staff.user.mobile_phone
		except OfficeStaff.DoesNotExist:
			#it's a provider
			if(forward == 'MO'):
				user_number = provider.mobile_phone
			elif(forward == 'OF'):
				user_number = provider.office_phone
			elif(forward == 'OT'):
				user_number = provider.phone

		logger.debug('%s/%s: Setting user_number to \'%s\'' % (
					request.session.session_key,
					request.session['ivr2_state'],
					user_number,
				))

		logger.debug('%s/%s: Tried to get called\'s number. Got \'%s\'' % (
				request.session.session_key,
				request.session['ivr2_state'],
				user_number,
			))

		dial = twilio.Dial(
				action=reverse('PracticeIVR_ForwardCall_New'),
				timeout=22,
				timeLimit=14400,  # 4 hours
				callerId=request.session['Caller'],
			)
		dial.append(twilio.Number(user_number,
				url=reverse('PracticeIVR_ForwardCall_Vet')
			))
		r.append(dial)
		r.append(twilio.Redirect(reverse('PracticeIVR_LeaveUrgentMsg_New')))
		return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)

	elif (request.session['ivr2_sub_state'] == 'PracticeIVR_ForwardCall_Dial'):
		del request.session['ivr2_sub_state']
		(clog, plog) = _getCallLogOrParent(request)
		if clog:
			logger.debug('%s/%s: PracticeIVR_ForwardCall_New state connected %s' % (
				request.session.session_key, request.session['ivr2_state'],
				str(clog.call_connected)))
		else:
			logger.debug('%s/%s: ProviderIVR_ForwardCall_New state no log SID %s' % (
				request.session.session_key, request.session['ivr2_state'],
				request.POST['CallSid']))
		if ('DialCallStatus' in request.POST):
			if (request.POST['DialCallStatus'] == 'completed'):
				# update child log call duration
				diallog = callLog.objects.get(callSID=request.POST['DialCallSid'])
				if diallog:
					diallog.call_duration = request.POST['DialCallDuration']
					diallog.save()
					logger.debug('%s: PracticeIVR_ForwardCall_New update child diallog dialSid %s duration %s' % (
						request.session.session_key, request.POST['DialCallSid'], request.POST['DialCallDuration']))
				else:
					logger.debug('%s: PracticeIVR_ForwardCall_New diallog not found: dialSid %s duration %s' % (
						request.session.session_key, request.POST['DialCallSid'], request.POST['DialCallDuration']))
				# Do nothing so that the second leg call continues un-interrupted
				return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
		if (clog.call_connected):
			r.append(twilio.Hangup())
			return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
		r.append(tts("The provider is not available right now."))
		r.append(twilio.Redirect(reverse('PracticeIVR_LeaveUrgentMsg_New')))
		# redirecting to LeaveUrgentMsg_New
		request.session['ivr2_state'] = 'PracticeIVR_LeaveUrgentMsg_New'
		return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)

	else:
		# else: Do nothing so that the call continues un-interrupted
		return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)