Beispiel #1
0
def playstation(request):
    dig = request.GET['Digits']
    skipintro = False
    if 'played' in request.GET: skipintro = True
    try:
        song = stationnextsonginternal(t.id)
        sid = song.id
    except:
        rn = Song.objects.last().id
        x = True
        while x:
            sid = random.randint(9000, rn)
            if Song.objects.get(id=sid).filetype == 'mp3': x = False
    resp = VoiceResponse()
    if not skipintro:
        resp.say(
            f"Thank you for chosing station number {dig}. At any point you can press 1 followed by the pound key to dislike and skip this song. Please be kind. The algorithm is still in development.",
            voice='woman',
            language='en-gb')
    gather = Gather(action='/twilio/dislike/' + str(dig) + '/' + str(sid),
                    method='GET',
                    numDigits='1')
    resp.append(gather)
    gather.play('http://5f1c429e.ngrok.io/twilio/playsong/' + str(sid))
    resp.redirect(f'/twilio/playstation?Digits={dig}&played=yes', method='GET')
    return HttpResponse(resp)
Beispiel #2
0
    def execute(self):
        resp = self.handle_hours()
        if resp is not None:
            return str(resp)

        resp = VoiceResponse()

        gather = Gather()
        gather.play(self.section_data['play_sample'], digits=1)

        pause = 2
        if 'pause' in self.section_data.keys():
            pause = int(self.section_data['pause'])

        gather.pause(length=pause)

        resp.append(gather)

        if ('no_input_max_loops' in self.section_data.keys()
                and self.loop_count >= int(
                    self.section_data['no_input_max_loops'])):
            if 'no_input_action_on_max_loops' in self.section_data.keys():
                resp.redirect(
                    self.url_of(
                        '/ivr/action/' +
                        self.section_data['no_input_action_on_max_loops']))
            else:
                resp.hangup()

        if 'no_input_sample' in self.section_data.keys():
            resp.play(self.section_data['no_input_sample'])

        resp.redirect(self.url_of(f'/ivr/menu?loop_count={self.loop_count}'))

        return str(resp)
Beispiel #3
0
    def test_nested_say_play_pause(self):
        """ a gather with a say, play, and pause """
        g = Gather()
        g.say('Hey')
        g.play(url='hey.mp3')
        g.pause()

        r = VoiceResponse()
        r.append(g)

        assert_equal(
            self.strip(r),
            '<?xml version="1.0" encoding="UTF-8"?><Response><Gather><Say>Hey</Say><Play>hey.mp3</Play><Pause /></Gather></Response>'
        )
Beispiel #4
0
def voice(text):
    if "static" in text:
        return send_file(text, mimetype='text/xml', as_attachment=True)
    else:
        """Respond to incoming phone calls and mention the caller's city"""
        intro = False
        number = request.values['From']  # Get caller's number
        state = request.values['FromState']  # Get caller's state

        # Start our TwiML response
        resp = VoiceResponse()

        if 'Digits' in request.values:
            # Get which digit the caller chose
            choice = request.values['Digits']

            # <Say> a different message depending on the caller's choice
            if choice == '1':
                resp.play('static/joinspeech.mp3')
                addNumber(number, state)
                return str(resp)
            elif choice == '2':
                resp.play('static/leavespeech.mp3')
                removeNumber(number, state)
                return str(resp)
            elif choice == '3':
                resp.play('static/moreinfospeech.mp3')
                send_message(
                    number,
                    "For more information, please visit our website at COVcall.tech"
                )
                return str(resp)
            else:
                # If the caller didn't choose 1 or 2, apologize and ask them again
                intro = True
                resp.play('static/redospeech.mp3')

        # Read intro message aloud to the caller
        if not intro:
            resp.play('static/introspeech.mp3')

        # Start our <Gather> verb
        gather = Gather(num_digits=1)
        gather.play('static/newgatherspeech.mp3')
        resp.append(gather)

        # If the user doesn't select an option, redirect them into a loop
        resp.redirect('/voice')

        return str(resp)  # Dummy return statement
Beispiel #5
0
def ans_call():
    """Respond to incoming phone calls with a brief message."""
    # Start our TwiML response
    resp = VoiceResponse()

    # Read a message aloud to the caller
    #resp.say("Thank you for calling! Have a great day.", voice='alice')

    gather = Gather(
        input='speech', 
        timeout=3,
        hints='coconutt, chatbots',
        action = '/commands',)
    gather.play('https://storage.googleapis.com/coconutt-voz/bienvenido.mp3')
    resp.append(gather)    
    
    return(str(resp))
Beispiel #6
0
def test(request):
    resp = VoiceResponse()

    gather = Gather(action="/api/v1/ivr/test/route_call/", method="POST", num_digits=1, timeout=10)
    gather.play(url="https://s3-ap-southeast-1.amazonaws.com/media.dellarobbiathailand.com/ivr/audio-welcome.mp3")
    gather.play(url="https://s3-ap-southeast-1.amazonaws.com/media.dellarobbiathailand.com/ivr/audio-sales.mp3")
    gather.play(url="https://s3-ap-southeast-1.amazonaws.com/media.dellarobbiathailand.com/ivr/audio-customer-service.mp3")
    gather.play(url="https://s3-ap-southeast-1.amazonaws.com/media.dellarobbiathailand.com/ivr/audio-accounting.mp3")
    resp.append(gather)

    return HttpResponse(resp)
Beispiel #7
0
def initialCall(To, conference_name):
    if conference_name in LANGUAGE_LIST:

        if 'delay' in conference_name:
            group_status = conferenceStatus['delay']
        else:
            group_status = conferenceStatus['live']

        if group_status == 'off-air':
            return str(group_is_closed())

        response = VoiceResponse()
        dial = Dial()
        dial.conference(muted=True,
                        beep=False,
                        end_conference_on_exit=False,
                        start_conference_on_enter=False,
                        max_participants=250,
                        trim="do-not-trim",
                        wait_url="/conference/onHold",
                        wait_method="GET",
                        name=conference_name)
        response.append(dial)

        return str(response)
    print('To: {}'.format(To))
    group = get_group_by_did(To)
    # print(group)
    if group:
        print(get_conference_status_by_group(group))
        if get_conference_status_by_group(group) == 'on-air':
            response = VoiceResponse()
            gather = Gather(action="/conference/gatherDigit",
                            method="GET",
                            num_digits=1,
                            timeout=30)
            gather.play(group['ivr_audio'])
            response.append(gather)
            return str(response)

    return str(group_is_closed())
Beispiel #8
0
def answer_view(request):
    """Respond to phone call from user"""
    response = VoiceResponse()

    # Create an entry for this new call.
    number = request.GET.get("From")
    call = Call(phone_number=number)
    call.save()

    # Play message and record user's choice.
    gather = Gather(action="/caller/collect/",
                    numDigits=1,
                    method="GET",
                    timeout=10,
                    enhanced=True)
    gather.play(_get_audio_url(CALL_INTRO_AUDIO))
    response.append(gather)

    # End the call if user doesn't give us anything.
    response.play(_get_audio_url(CALL_TIMEOUT_AUDIO))
    return TwimlResponse(response)
Beispiel #9
0
def voice_three(request):
    """
    Play voice 3 and do actions accordingly
    """
    call_sid = None
    recording_url = None
    if request.method == "POST":
        call_sid = request.POST.get('CallSid', None)
        recording_url = request.POST.get('RecordingUrl', None)
    if request.method == "GET":
        call_sid = request.GET.get('CallSid', None)
        recording_url = request.GET.get('RecordingUrl', None)

    if recording_url:
        call_detail = CallDetail.objects.get(call_sid=call_sid)
        call_detail.comment = recording_url
        call_detail.save()
    twiml = VoiceResponse()
    gather = Gather(num_digits=1, action='/VoiceFour')
    gather.play('http://roelofvandijk.com/mp33/IVR/PNGK-speakOrEndCall.mp3')
    twiml.append(gather)
    return HttpResponse(str(twiml))
Beispiel #10
0
def incoming_twilio_call(request):  # pylint: disable=too-many-branches, too-many-statements
    response = VoiceResponse()

    if request.method == 'POST':  # pylint: disable=too-many-nested-blocks
        now = timezone.now()

        integration_match = None

        post_dict = request.POST.dict()

        source = post_dict['From']
        destination = post_dict['To']

        for integration in Integration.objects.filter(type='twilio'):
            if 'phone_number' in integration.configuration and (
                    source == integration.configuration['phone_number'] or
                    destination == integration.configuration['phone_number']):
                integration_match = integration

                if source == integration.configuration['phone_number']:
                    destination = post_dict['From']
                    source = post_dict['To']

                    post_dict['From'] = source
                    post_dict['To'] = destination

        if 'CallStatus' in post_dict:
            incoming = IncomingCallResponse(source=source)
            incoming.receive_date = now

            incoming.message = ''

            if 'Digits' in post_dict:
                incoming.message = post_dict['Digits'].strip()

            if 'SpeechResult' in post_dict:
                if incoming.message:
                    incoming.message += ' | '

                incoming.message += post_dict['SpeechResult'].strip()

            incoming.transmission_metadata = post_dict

            if integration_match is not None:
                incoming.integration = integration_match

            if incoming.message.strip() == '':
                incoming.message = None

            incoming.save()

        if integration_match is not None:
            integration_match.process_incoming(post_dict)

            for call in OutgoingCall.objects.filter(
                    destination=source,
                    sent_date=None,
                    send_date__lte=timezone.now(),
                    integration=integration_match).order_by('send_date'):
                if call.next_action != 'gather':
                    if call.message is not None and call.message != '':
                        if call.message.lower().startswith(
                                'http://') or call.message.lower().startswith(
                                    'https://'):
                            response.play(
                                call.message.replace('\n', ' ').replace(
                                    '\r', ' ').split(' ')[0])
                        else:
                            response.say(call.message)
                    elif call.file is not None and call.file != '':
                        pass

                call.sent_date = timezone.now()
                call.save()

                if call.next_action == 'pause':
                    response.pause(length=call.pause_length)
                elif call.next_action == 'gather':
                    args = {
                        'input': call.gather_input,
                        'barge_in': True,
                        'num_digits': 1
                    }

                    if call.gather_timeout is not None:
                        args['timeout'] = call.gather_timeout

                    # if call.gather_finish_on_key is not None:
                    #    args['finish_on_key'] = call.gather_finish_on_key

                    # if call.gather_num_digits is not None:
                    #    args['num_digits'] = call.gather_num_digits

                    if call.gather_speech_timeout is not None:
                        args['speech_timeout'] = call.gather_speech_timeout

                    if call.gather_speech_model is not None:
                        args['speech_model'] = call.gather_speech_model

                    gather = Gather(**args)

                    if call.message is not None and call.message != '':
                        if call.message.lower().startswith(
                                'http://') or call.message.lower().startswith(
                                    'https://'):
                            gather.play(call.message.replace(
                                '\n', ' ').replace('\r', ' ').split(' ')[0],
                                        loop=call.gather_loop)
                        else:
                            gather.say(call.message, loop=call.gather_loop)
                    elif call.file is not None and call.file != '':
                        pass

                    response.append(gather)

                    break
                elif call.next_action == 'hangup':
                    response.hangup()
                    break

            if len(response.verbs) == 0:  # pylint: disable=len-as-condition
                log_metadata = {}

                log_metadata['phone_number'] = post_dict.get('To', None)
                log_metadata['direction'] = post_dict.get('Direction', None)
                log_metadata['call_status'] = post_dict.get('CallStatus', None)

                log('twilio:incoming_twilio_call',
                    'Sending empty voice response back to Twilio. (Tip: verify that you are not stuck on a process response or other card awaiting user input.)',
                    tags=['twilio', 'voice', 'warning'],
                    metadata=log_metadata)

    return HttpResponse(str(response), content_type='text/xml')
Beispiel #11
0
 def add_gandalf_to_response(response):
     gather = Gather(num_digits=5, action=url_for('.password_entered'))
     challenge_clip = pick_random_file("challenge")
     gather.play(challenge_clip)
     response.append(gather)
     response.redirect(url_for(".gatekeeper", notFirstCall='true'))
def process_speech():
    caller_phone_number = request.values.get('From')
    user_id = request.values.get('CallSid')
    twilio_asr_language = request.values.get('twilio_asr_language', 'en-AU')
    apiai_language = request.values.get('apiai_language', 'en')
    prior_text = request.values.get('prior_text', 'Prior text missing')
    input_text = request.values.get('SpeechResult', '')
    confidence = float(request.values.get('Confidence', 0.0))
    hostname = request.url_root
    print "Twilio Speech to Text: " + input_text + " Confidence: " + str(
        confidence)
    #Check for any blanks between digits (both for employee number and mobile number)
    if re.search(r'\b\d{1,16}\b', input_text):
        input_text = re.sub('(?<=\d) (?=\d)', '', input_text)
        print "Changed input: " + input_text
    sys.stdout.flush()
    resp = VoiceResponse()

    if (confidence >= 0.0):
        # Step 1: Call Dialogflow for intent analysis
        intent_name, output_text, product_name, emp_id = apiai_text_to_intent(
            apiai_client_access_key, input_text, user_id, apiai_language)

        # Step 2: Speech input processing by Twilio
        values = {'prior_text': output_text}
        qs2 = urllib.urlencode(values)
        action_url = '/process_speech?' + qs2
        gather = Gather(input="speech",
                        hints=hints,
                        language=twilio_asr_language,
                        speechTimeout="auto",
                        action=action_url,
                        method="POST")
        values = {"text": output_text}
        qs1 = urllib.urlencode(values)
        print 'In-progress: Before Google tts'
        gather.play(hostname + 'goog_text2speech?' + qs1)
        print 'In progress: After Google tts'
        resp.append(gather)

        # Transfer to General services if employee number is not provided
        if intent_name == 'no_employee_number_cartwright':
            resp.dial('+61450178418')
            resp.redirect('/process_close')

        # Transfer for default fallback intent (*******To Check with Chris*******)
        #if intent_name == 'Default Fallback Intent':
        #print 'reached default intent. Transfering...'
        #resp.dial('+61280490603')
        #resp.redirect('/process_close')

        # Perform employee number validation
        if intent_name == 'get_employee_number_cartwright':
            #Validate employee number
            if (str(emp_id)[:2]) != '10':
                resp.dial('+61450178418')
                resp.redirect('/process_close')

        # Transfer to routepoint based in intent and product
        print 'Intent :' + intent_name
        if intent_name != '' and product_name != '':
            if (str(int(emp_id))[:2]) != '10':
                resp.dial('+61450178418')
                resp.redirect('/process_close')
            else:
                phone_number = getroutepoint(intent_name, product_name)
                resp.dial(phone_number)
                resp.redirect('/process_close')

        # If gather is missing (no speech input), redirect to process incomplete speech via Dialogflow
        values = {
            'prior_text': output_text,
            'twilio_asr_language': twilio_asr_language,
            'apiai_language': apiai_language,
            'SpeechResult': '',
            'Confidence': 0.0
        }
        qs3 = urllib.urlencode(values)
        action_url = '/process_speech?' + qs3
        resp.redirect(action_url)

    # When confidence of speech recogniton is not enough, replay the previous conversation
    else:
        output_text = prior_text
        values = {"prior_text": output_text}
        qs2 = urllib.urlencode(values)
        action_url = "/process_speech?" + qs2
        gather = Gather(input="speech",
                        hints=hints,
                        language=twilio_asr_language,
                        speechTimeout="auto",
                        action=action_url,
                        method="POST")
        values = {"text": output_text}
        qs1 = urllib.urlencode(values)
        print 'Before Google tts'
        gather.play(hostname + 'goog_text2speech?' + qs1)
        print 'After Google tts read'
        resp.append(gather)
        values = {"prior_text": output_text}
        qs2 = urllib.urlencode(values)
        action_url = "/process_speech?" + qs2
        resp.redirect(action_url)
    print str(resp)
    return str(resp)
Beispiel #13
0
def start():
    caller_phone_number = request.values.get('From')
    user_id = request.values.get('CallSid')
    polly_voiceid = request.values.get('polly_voiceid', "Joanna")
    twilio_asr_language = request.values.get('twilio_asr_language', "en-US")
    apiai_language = request.values.get('apiai_language', "en")
    caller_name = registered_users.get(caller_phone_number, " ")
    hostname = request.url_root

    # Initialize API.AI Bot
    headers = {
        'authorization': "Bearer " + apiai_client_access_key,
        'content-type': "application/json"
    }
    payload = {
        'event': {
            'name': 'book_hotel_welcome',
            'data': {
                'user_name': caller_name
            }
        },
        'lang': apiai_language,
        'sessionId': user_id
    }
    response = requests.request("POST",
                                url=apiai_url,
                                data=json.dumps(payload),
                                headers=headers,
                                params=apiai_querystring)
    print(response.text)
    output = json.loads(response.text)
    output_text = output['result']['fulfillment']['speech']
    output_text = output_text.decode("utf-8")
    resp = VoiceResponse()
    # Prepare for next set of user Speech
    values = {"prior_text": output_text}
    qs = urllib.urlencode(values)
    action_url = "/process_speech?" + qs
    gather = Gather(input="speech",
                    hints=hints,
                    language=twilio_asr_language,
                    timeout="3",
                    action=action_url,
                    method="POST")
    # TTS the bot response
    values = {
        "text": output_text,
        "polly_voiceid": polly_voiceid,
        "region": "us-east-1"
    }
    qs = urllib.urlencode(values)
    gather.play(hostname + 'polly_text2speech?' + qs)
    resp.append(gather)

    # If gather is missing (no speech), redirect to process speech again
    values = {
        "prior_text": output_text,
        "polly_voiceid": polly_voiceid,
        "twilio_asr_language": twilio_asr_language,
        "apiai_language": apiai_language,
        "SpeechResult": "",
        "Confidence": 0.0
    }
    qs = urllib.urlencode(values)
    action_url = "/process_speech?" + qs
    resp.redirect(action_url)
    print str(resp)
    return str(resp)
Beispiel #14
0
def process_speech():
    user_id = request.values.get('CallSid')
    polly_voiceid = request.values.get('polly_voiceid', "Joanna")
    twilio_asr_language = request.values.get('twilio_asr_language', "en-US")
    apiai_language = request.values.get('apiai_language', "en")
    prior_text = request.values.get('prior_text', "Prior text missing")
    prior_dialog_state = request.values.get('prior_dialog_state',
                                            "ElicitIntent")
    input_text = request.values.get("SpeechResult", "")
    confidence = float(request.values.get("Confidence", 0.0))
    hostname = request.url_root
    print "Twilio Speech to Text: " + input_text + " Confidence: " + str(
        confidence)
    sys.stdout.flush()

    resp = VoiceResponse()
    if (confidence > 0.5):
        # Step 1: Call Bot for intent analysis - API.AI Bot
        intent_name, output_text, dialog_state = apiai_text_to_intent(
            apiai_client_access_key, input_text, user_id, apiai_language)

        # Step 2: Construct TwiML
        if dialog_state in ['in-progress']:
            values = {
                "prior_text": output_text,
                "prior_dialog_state": dialog_state
            }
            qs2 = urllib.urlencode(values)
            action_url = "/process_speech?" + qs2
            gather = Gather(input="speech",
                            hints=hints,
                            language=twilio_asr_language,
                            timeout="3",
                            action=action_url,
                            method="POST")
            values = {
                "text": output_text,
                "polly_voiceid": polly_voiceid,
                "region": "us-east-1"
            }
            qs1 = urllib.urlencode(values)
            gather.play(hostname + 'polly_text2speech?' + qs1)
            resp.append(gather)

            # If gather is missing (no speech), redirect to process incomplete speech via the Bot
            values = {
                "prior_text": output_text,
                "polly_voiceid": polly_voiceid,
                "twilio_asr_language": twilio_asr_language,
                "apiai_language": apiai_language,
                "SpeechResult": "",
                "Confidence": 0.0
            }
            qs3 = urllib.urlencode(values)
            action_url = "/process_speech?" + qs3
            resp.redirect(action_url)
        elif dialog_state in ['complete']:
            values = {
                "text": output_text,
                "polly_voiceid": polly_voiceid,
                "region": "us-east-1"
            }
            qs = urllib.urlencode(values)
            resp.play(hostname + 'polly_text2speech?' + qs)
            resp.hangup()
        elif dialog_state in ['Failed']:
            values = {
                "text": "I am sorry, there was an error.  Please call again!",
                "polly_voiceid": polly_voiceid,
                "region": "us-east-1"
            }
            qs = urllib.urlencode(values)
            resp.play(hostname + 'polly_text2speech?' + qs)
            resp.hangup()
    else:
        # We didn't get STT of higher confidence, replay the prior conversation
        output_text = prior_text
        dialog_state = prior_dialog_state
        values = {
            "prior_text": output_text,
            "polly_voiceid": polly_voiceid,
            "twilio_asr_language": twilio_asr_language,
            "apiai_language": apiai_language,
            "prior_dialog_state": dialog_state
        }
        qs2 = urllib.urlencode(values)
        action_url = "/process_speech?" + qs2
        gather = Gather(input="speech",
                        hints=hints,
                        language=twilio_asr_language,
                        timeout="3",
                        action=action_url,
                        method="POST")
        values = {
            "text": output_text,
            "polly_voiceid": polly_voiceid,
            "region": "us-east-1"
        }
        qs1 = urllib.urlencode(values)
        gather.play(hostname + 'polly_text2speech?' + qs1)
        resp.append(gather)

        values = {
            "prior_text": output_text,
            "polly_voiceid": polly_voiceid,
            "twilio_asr_language": twilio_asr_language,
            "apiai_language": apiai_language,
            "prior_dialog_state": dialog_state
        }
        qs2 = urllib.urlencode(values)
        action_url = "/process_speech?" + qs2
        resp.redirect(action_url)
    print str(resp)
    return str(resp)
def welcome():
	cli = request.values.get('From')
	call_id = request.values.get('CallSid')
	asr_lang = request.values.get('asr_lang', 'en-IN')
	lang_code = request.values.get('lang_code', 'en')
	hostname = request.url_root
	
	# Check for HOOP (hours of operations)
	start = datetime.time(0, 00)
	end = datetime.time(23, 59)
	timestamp = datetime.datetime.now().time()
	resp = VoiceResponse()
	
	if (end <= timestamp >= start):
		# If call time not within hours of operation, play appropriate prompt and transfer to general line
		values = {"text": 'Hi! The Care For me team is currently closed, the team is opened from 8:30 till 6 P M weekdays, please hold and I’ll transfer your call to the General Customer Service Number.'}
		print 'In start: before Google TTS'
		resp.play(hostname + 'goog_text2speech?' + qs)
		print 'In start: after Google TTS'
		resp.dial('+61280490603')
		return str(resp)
		
	else:
		# If call within business hours, triggering Dialogflow "Welcome" event
		#Setting Google Dialogflow Credentials and invoking SDK
		service_account_info = json.loads(credentials_dgf)
		credentials = service_account.Credentials.from_service_account_info(service_account_info)
		session_client = dialogflow.SessionsClient(credentials=credentials)
		session = session_client.session_path(project_id, call_id)
		event_input = dialogflow.types.EventInput(name='Welcome', language_code=lang_code)
		query_input = dialogflow.types.QueryInput(event=event_input)
		response = session_client.detect_intent(session=session, query_input=query_input)
		print response	
		output_text = response.query_result.fulfillment_text
		output_text = output_text.decode('utf-8')
		print output_text
		resp = VoiceResponse()
		
		# Prepare for collecting subsequent user input
		values = {'prior_text': output_text}
		qs = urllib.urlencode(values)
		action_url = '/process_speech?' + qs
		gather = Gather(input="speech", hints=hints, language=asr_lang, speechTimeout="auto", action=action_url, method="POST")
		
		# Welcome prompt played to callers during office hours
		values = {"text": output_text}
		qs = urllib.urlencode(values)
		print 'In start: before Google TTS'
		gather.play(hostname + 'goog_text2speech?' + qs)
		print 'In start: after Google TTS'
		resp.append(gather)
		
		# If user input is missing after welcome prompt (no speech input), redirect to collect speech input again
		values = {'prior_text': output_text, 
			  'asr_lang': asr_lang, 
			  'lang_code': lang_code, 
			  'SpeechResult': '', 
			  'Confidence': 0.0
			 }
		qs = urllib.urlencode(values)
		action_url = '/process_speech?' + qs
		resp.redirect(action_url)
		print str(resp)
		return str(resp)
Beispiel #16
0
def process_speech():
    caller_phone_number = request.values.get('From')
    user_id = request.values.get('CallSid')
    twilio_asr_language = request.values.get('twilio_asr_language', 'en-AU')
    apiai_language = request.values.get('apiai_language', 'en')
    prior_text = request.values.get('prior_text', 'Prior text missing')
    input_text = request.values.get('SpeechResult', '')
    confidence = float(request.values.get('Confidence', 0.0))
    hostname = request.url_root
    print "Twilio Speech to Text: " + input_text + " Confidence: " + str(
        confidence)
    #Check for any blanks between digits (both for employee number and mobile number)
    if re.search(r'\b\d{1,16}\b', input_text):
        input_text = re.sub('(?<=\d) (?=\d)', '', input_text)
        print "Changed input: " + input_text
    sys.stdout.flush()
    resp = VoiceResponse()

    if (confidence >= 0.0):
        # Step 1: Call Dialogflow for intent analysis
        intent_name, output_text, category, dealer_code = apiai_text_to_intent(
            apiai_client_access_key, input_text, user_id, apiai_language)

        # Step 2: Speech input processing by Twilio
        values = {'prior_text': output_text}
        qs2 = urllib.urlencode(values)
        action_url = '/process_speech?' + qs2
        gather = Gather(input="speech",
                        hints=hints,
                        language=twilio_asr_language,
                        speechTimeout="auto",
                        action=action_url,
                        method="POST")
        values = {"text": output_text}
        qs1 = urllib.urlencode(values)
        print 'In-progress: Before Google tts'
        gather.play(hostname + 'goog_text2speech?' + qs1)
        print 'In progress: After Google tts'
        resp.append(gather)

        # If gather is missing (no speech input), redirect to process incomplete speech via Dialogflow
        values = {
            'prior_text': output_text,
            'twilio_asr_language': twilio_asr_language,
            'apiai_language': apiai_language,
            'SpeechResult': '',
            'Confidence': 0.0
        }
        qs3 = urllib.urlencode(values)
        action_url = '/process_speech?' + qs3
        resp.redirect(action_url)

    # When confidence of speech recogniton is not enough, replay the previous conversation
    else:
        output_text = prior_text
        values = {"prior_text": output_text}
        qs2 = urllib.urlencode(values)
        action_url = "/process_speech?" + qs2
        gather = Gather(input="speech",
                        hints=hints,
                        language=twilio_asr_language,
                        speechTimeout="auto",
                        action=action_url,
                        method="POST")
        values = {"text": output_text}
        qs1 = urllib.urlencode(values)
        print 'Before Google tts'
        gather.play(hostname + 'goog_text2speech?' + qs1)
        print 'After Google tts read'
        resp.append(gather)
        values = {"prior_text": output_text}
        qs2 = urllib.urlencode(values)
        action_url = "/process_speech?" + qs2
        resp.redirect(action_url)
    print str(resp)
    return str(resp)
Beispiel #17
0
def process_speech():
    caller_phone_number = request.values.get('From')
    user_id = request.values.get('CallSid')
    polly_voiceid = request.values.get('polly_voiceid', "Nicole")
    twilio_asr_language = request.values.get('twilio_asr_language', 'en-IN')
    apiai_language = request.values.get('apiai_language', 'en')
    prior_text = request.values.get('prior_text', 'Prior text missing')
    input_text = request.values.get('SpeechResult', '')
    confidence = float(request.values.get('Confidence', 0.0))
    hostname = request.url_root
    print "Twilio Speech to Text: " + input_text + " Confidence: " + str(
        confidence)
    #Check for any blanks between digits (both for employee number and mobile number)
    if re.search(r'\b\d{1,16}\b', input_text):
        input_text = re.sub('(?<=\d) (?=\d)', '', input_text)
        print "Changed input: " + input_text
    sys.stdout.flush()
    resp = VoiceResponse()

    if (confidence >= 0.0):
        # Step 1: Call Dialogflow for intent analysis
        intent_name, output_text, product_name, emp_id, intent_stage, dialog_state = apiai_text_to_intent(
            apiai_client_access_key, input_text, user_id, apiai_language)

        if intent_name == 'get_employee_number_cartwright':
            output_text = '<speak>The employee number you provided is <say-as interpret-as="digits">' + emp_id + '</say-as>. Please confirm by saying Yes or No </speak>'

        # Step 2: Speech input processing by Twilio
        values = {'prior_text': output_text}
        qs2 = urllib.urlencode(values)
        action_url = '/process_speech?' + qs2
        gather = Gather(input="speech",
                        hints=hints,
                        language=twilio_asr_language,
                        speechTimeout="auto",
                        action=action_url,
                        method="POST")
        values = {
            "text": output_text,
            "polly_voiceid": polly_voiceid,
            "region": "ap-southeast-2"
        }
        qs1 = urllib.urlencode(values)
        print 'In-progress: Before polly tts'
        gather.play(hostname + 'polly_text2speech?' + qs1)
        print 'In progress: After polly tts'
        resp.append(gather)

        # Transfer for default fallback intent
        if intent_name == 'Default Fallback Intent':
            print 'reached default intent. Transfering...'
            resp.dial('+917338856833')

        # Perform employee number validation
        if intent_name == 'get_employee_number_cartwright_yes':
            #Validate employee number
            if (str(emp_id)[:2]) != '10':
                resp.dial('+919840610434')

        # Transfer to routepoint based in intent and product
        print 'Intent :' + intent_name
        if intent_name != '' and product_name != '':
            phone_number = getroutepoint(intent_name, product_name)
            resp.dial(phone_number)
        #if intent_name in ['billing_services_cartwright','sales_services_cartwright']:

        # If gather is missing (no speech input), redirect to process incomplete speech via Dialogflow
        values = {
            'prior_text': output_text,
            "polly_voiceid": polly_voiceid,
            'twilio_asr_language': twilio_asr_language,
            'apiai_language': apiai_language,
            'SpeechResult': '',
            'Confidence': 0.0
        }
        qs3 = urllib.urlencode(values)
        action_url = '/process_speech?' + qs3
        resp.redirect(action_url)

    # When confidence of speech recogniton is not enough, replay the previous conversation
    else:
        output_text = prior_text
        values = {
            "prior_text": output_text,
            "polly_voiceid": polly_voiceid,
            "twilio_asr_language": twilio_asr_language,
            "apiai_language": apiai_language
        }
        qs2 = urllib.urlencode(values)
        action_url = "/process_speech?" + qs2
        gather = Gather(input="speech",
                        hints=hints,
                        language=twilio_asr_language,
                        speechTimeout="auto",
                        action=action_url,
                        method="POST")
        values = {
            "text": output_text,
            "polly_voiceid": polly_voiceid,
            "region": "ap-southeast-2"
        }
        qs1 = urllib.urlencode(values)
        print 'Before calling polly tts'
        gather.play(hostname + 'polly_text2speech?' + qs1)
        print 'After polly tts read'
        resp.append(gather)
        values = {
            "prior_text": output_text,
            "polly_voiceid": polly_voiceid,
            "twilio_asr_language": twilio_asr_language,
            "apiai_language": apiai_language
        }
        qs2 = urllib.urlencode(values)
        action_url = "/process_speech?" + qs2
        resp.redirect(action_url)
    print str(resp)
    return str(resp)
def start():
    caller_phone_number = request.values.get('From')
    user_id = request.values.get('CallSid')
    twilio_asr_language = request.values.get('twilio_asr_language', 'en-AU')
    apiai_language = request.values.get('apiai_language', 'en')
    hostname = request.url_root

    # Check for HOOP (hours of operations)
    start = datetime.time(0, 00)
    end = datetime.time(23, 59)
    timestamp = datetime.datetime.now().time()
    resp = VoiceResponse()
    if (end <= timestamp >= start):
        # If call time not within hours of operation, play appropriate prompt and transfer to general line
        values = {
            "text":
            'Hi! The Care For me team is currently closed, the team is opened from 8:30 till 6 P M weekdays. Please hold and I’ll transfer your call to the General Customer Service Number.'
        }
        print 'In start: before Google TTS'
        resp.play(hostname + 'goog_text2speech?' + qs)
        print 'In start: after Google TTS'
        resp.dial('+61450178418')
        return str(resp)
    else:
        # If call within office hours, triggering Dialogflow "Welcome" event
        headers = {
            'authorization': 'Bearer ' + apiai_client_access_key,
            'content-type': 'application/json'
        }
        payload = {
            'event': {
                'name': 'Welcome'
            },
            'lang': apiai_language,
            'sessionId': user_id
        }
        response = requests.request("POST",
                                    url=apiai_url,
                                    data=json.dumps(payload),
                                    headers=headers,
                                    params=apiai_querystring)
        print response.text
        output = json.loads(response.text)
        output_text = output['result']['fulfillment']['speech']
        output_text = output_text.decode('utf-8')
        resp = VoiceResponse()

        # Prepare for collecting subsequent user input
        values = {'prior_text': output_text}
        qs = urllib.urlencode(values)
        action_url = '/process_speech?' + qs
        gather = Gather(input="speech",
                        hints=hints,
                        language=twilio_asr_language,
                        speechTimeout="auto",
                        action=action_url,
                        method="POST")

        # Welcome prompt played to callers during office hours
        values = {"text": output_text}
        qs = urllib.urlencode(values)
        print 'In start: before Google TTS'
        gather.play(hostname + 'goog_text2speech?' + qs)
        print 'In start: after Google TTS'
        resp.append(gather)

        # If user input is missing after welcome prompt (no speech input), redirect to collect speech input again
        values = {
            'prior_text': output_text,
            'twilio_asr_language': twilio_asr_language,
            'apiai_language': apiai_language,
            'SpeechResult': '',
            'Confidence': 0.0
        }
        qs = urllib.urlencode(values)
        action_url = '/process_speech?' + qs
        resp.redirect(action_url)
        print str(resp)
        return str(resp)
Beispiel #19
0
def start():
    caller_phone_number = request.values.get('From')
    user_id = request.values.get('CallSid')
    polly_voiceid = request.values.get('polly_voiceid', 'Nicole')
    twilio_asr_language = request.values.get('twilio_asr_language', 'en-IN')
    apiai_language = request.values.get('apiai_language', 'en')
    hostname = request.url_root

    # Check for HOOP (hours of operations)
    start = datetime.time(0, 00)
    end = datetime.time(23, 59)
    timestamp = datetime.datetime.now().time()
    resp = VoiceResponse()
    if (end <= timestamp >= start):
        # If call time not within hours of operation, play appropriate prompt and transfer to general line
        values = {
            "text":
            'Our office hours are from 08:30 AM till 18:00 PM on weekdays. Kindly hold while we transfer your call to our general assistance line and a customer service representative will assist you',
            "polly_voiceid": polly_voiceid,
            "region": "ap-southeast-2"
        }
        qs = urllib.urlencode(values)
        print 'In start: before polly TTS'
        resp.play(hostname + 'polly_text2speech?' + qs)
        print 'In start: after polly TTS'
        #resp.append(gather)
        resp.dial('+919840610434')
        return str(resp)
    else:
        # If call within office hours, triggering Dialogflow "Welcome" event
        headers = {
            'authorization': 'Bearer ' + apiai_client_access_key,
            'content-type': 'application/json'
        }
        payload = {
            'event': {
                'name': 'Welcome'
            },
            'lang': apiai_language,
            'sessionId': user_id
        }
        response = requests.request("POST",
                                    url=apiai_url,
                                    data=json.dumps(payload),
                                    headers=headers,
                                    params=apiai_querystring)
        print response.text
        output = json.loads(response.text)
        output_text = output['result']['fulfillment']['speech']
        output_text = output_text.decode('utf-8')
        resp = VoiceResponse()

        # Prepare for collecting subsequent user input
        values = {'prior_text': output_text}
        qs = urllib.urlencode(values)
        action_url = '/process_speech?' + qs
        gather = Gather(input="speech",
                        hints=hints,
                        language=twilio_asr_language,
                        speechTimeout="auto",
                        action=action_url,
                        method="POST")

        # Welcome prompt played to callers during office hours
        values = {
            "text": output_text,
            "polly_voiceid": polly_voiceid,
            "region": "ap-southeast-2"
        }
        qs = urllib.urlencode(values)
        print 'In start: before polly TTS'
        gather.play(hostname + 'polly_text2speech?' + qs)
        print 'In start: after polly TTS'
        resp.append(gather)

        # If user input is missing after welcome prompt (no speech input), redirect to collect speech input again
        values = {
            'prior_text': output_text,
            'polly_voiceid': polly_voiceid,
            'twilio_asr_language': twilio_asr_language,
            'apiai_language': apiai_language,
            'SpeechResult': '',
            'Confidence': 0.0
        }
        qs = urllib.urlencode(values)
        action_url = '/process_speech?' + qs
        resp.redirect(action_url)
        print str(resp)
        return str(resp)