Twilio's request parameter for speech

点点圈 提交于 2019-12-23 12:17:07

问题


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.


回答1:


[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.




回答2:


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



来源:https://stackoverflow.com/questions/44748229/twilios-request-parameter-for-speech

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!