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)
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>')
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>' )
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)
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)
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)
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)