gunslinger
gunslinger

Reputation: 118

Twilio's request parameter for speech

The documentation for the <Gather> tag (Python) says that:

If you chose to gather digits from the caller, Twilio's request to your application will include a Digits parameter containing which numbers your caller entered during the .

But I can't find anywhere what is the parameter if the choice is to gather speech to be able to branch the call logic based on the speech sent by the user.

I tried Speech and speech, but didn't work.

My code:

from flask import Flask, request
from TwilioPhoneCall import app
from twilio.twiml.voice_response import VoiceResponse, Gather, Say
from twilio.twiml.messaging_response import MessagingResponse, Message


@app.route('/', methods=['GET', 'POST'])
def message():
    resp = VoiceResponse()

    gather = Gather(input='speech', timeout=3, hints='yes, no', action='/gather')
    gather.say('Hi, do you wanna go out tonight?'+
               ' Answer yes or no.')
    resp.append(gather)

    resp.redirect('/')

    return str(resp)

@app.route('/gather', methods=['GET', 'POST'])
def gather():
     resp = VoiceResponse()

     if 'Speech' in request.values:
         choice = request.values['Speech']

         if choice == 'yes':
             resp.say('Yay! See you later!')
             resp.sms('She replied yes!', to='myphonenumber')
             return str(resp)

         elif choice == 'no':
             resp.say('Oh... Ok.')
             resp.sms('She replied no.', to='myphonenumber')
             return str(resp)

          else:
              resp.say('Sorry, but the options are yes or no.')

    resp.redirect('/')

    return str(resp)

I already tried the exact same code with dtmf (Digits) and worked fine, my problem is with speech:

After the user speech his response the program will loop back to the first gather.say as if no input was made.

Upvotes: 4

Views: 999

Answers (2)

Alex Baban
Alex Baban

Reputation: 11702

The correct parameter name is SpeechResult not Speech.

   @app.route('/gather', methods=['GET', 'POST'])
    def gather():
         resp = VoiceResponse()

         if 'SpeechResult' in request.values:
             choice = request.values['SpeechResult']

             if choice == 'Yes.':
                 resp.say('Yay! See you later!')
                 resp.sms('She replied yes!', to='myphonenumber')
                 return str(resp)

             elif choice == 'No.':
                 resp.say('Oh... Ok.')
                 resp.sms('She replied no.', to='myphonenumber')
                 return str(resp)

              else:
                  resp.say('Sorry, but the options are yes or no.')

        resp.redirect('/')

        return str(resp)

Twilio blog: Introducing Speech Recognition https://www.twilio.com/blog/2017/05/introducing-speech-recognition.html

Upvotes: 2

miknik
miknik

Reputation: 5941

[SpeechResult] is the return value you are looking for which contains the transcribed text.

Twilio also returns [Confidence] with a score between 0 and 1 (to 8 decimal places in my experience) for the likely indicated accuracy of the transcription.

Upvotes: 2

Related Questions