Dial by Voice - Python


This application will prompt the caller for a phone number via speech input and connect to the phone number recognized using ASR providing an additional level of accessibility to your users.

What do you need to run this code?

View the full code on our Github here!

Additionally, you will need the SignalWire Python SDK and the Requests module.

How to Run the Application

Run pre-built from Docker Hub

docker pull signalwire/snippets-call-text-proxy:python

Build On Docker

docker build -t snippets-call-text-proxy

Run your Image

docker run --publish 5000:5000 --env-file .env snippets-call-text-proxy

Build and Run Natively

To run the application, execute export FLASK_APP=app.py then run flask run.

You may need to use an SSH tunnel for testing this code if running on your local machine. – we recommend ngrok. You can learn more about how to use ngrok here.

Step By Step Code Walkthrough

This application all runs in one app.py file.

The Application

This code is incredibly simple to implement! The first route /dial-prompt handles incoming http requests for a provisioned phone number, prompts users to say a number to call, and asks them if they wish to dial it. If the number to dial can not be understood, they will be prompted again to repeat the number.

# accepts web requests to `/dial-prompt`
@app.route('/dial-prompt', methods=['GET', 'POST'])
def dial_prompt():

    # Initialize VoiceResponse
    response = VoiceResponse()

    # check if user input was provided via dtmf entry
    if "SpeechResult" in request.values:

        # Read speech result
        speech_result = request.values.get("SpeechResult")

        # Validate Number Provided
        number = re.sub("[^0-9]", "", speech_result)

        # Make E164
        if len(number) == 11:
            number = "+" + number

        # Assume US Number, Make E164
        elif len(number) == 10:
            number = "+1" + number

        # We did not determine a valid phone number
            response.say("I am sorry, I did not understand you.  Please try again.", voice="man")
            return str(response)

        # Prompt user
        gather = Gather(action='/dial-verify?number_to_dial=' + number, input='speech', speechTimeout="auto", timeout="10", method='GET')

        # Append say to gather to produce TTS
        gather.say("We detected " + speech_result + " Would you like me to connect you? ")

        # Append the gather

        # Hangup the call


        # Prompt user
        gather = Gather(action='/dial-prompt', input='speech', speechTimeout="auto",  timeout="10", method='GET')

        # Append say to gather to produce TTS
        gather.say("What number would you like to dial?")

        # Append the gather 

        # Hangup the call

    # return response
    return str(response)

The second route is /dial-verify and it dials the number after a successful Yes is detected and verified by the user in the previous route. If the user does not say Yes. they get a goodbye message instead.

# accept web requests to '/dial-verify'
@app.route('/dial-verify', methods=['GET', 'POST'])
def dial_verify():

    # Initialize VoiceResponse
    response = VoiceResponse()

    if "SpeechResult" in request.values:
        if request.values.get("SpeechResult") == "Yes.":

            # Read the passed in number to dial from the request
            number_to_dial = request.values.get("number_to_dial")

            # Append the gather
            return str(response)

    response.say("OK. Thank you for calling goodbye!")
    return str(response)

Wrap Up

Dial by Voice is a powerful tool to have in a variety of use-cases and SignalWire's API makes it easy to implement into any project.

Required Resources:

Github Repo
Python SignalWire SDK

Sign Up Here

If you would like to test this example out, you can create a SignalWire account and space here.

Please feel free to reach out to us on our Community Slack or create a Support ticket if you need guidance!

What’s Next

Check out the full code on our SignalWire Github Repo!

Did this page help you?