A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.twilio.com/docs/voice/tutorials/how-to-gather-user-input-via-keypad/python below:

Gather User Input via Keypad (DTMF Tones) in Python

Gather User Input via Keypad (DTMF Tones) in Python

In this guide, we'll show you how to gather user input during a phone call through the phone's keypad (using DTMF (link takes you to an external page) tones) in your Python application. By applying this technique, you can create interactive voice response (IVR (link takes you to an external page) ) systems and other phone based interfaces for your users.

The code snippets in this guide are written using the Flask web framework (link takes you to an external page) and the Twilio Python SDK. Let's get started!

This guide assumes you have already set up your web application to receive incoming phone calls. If you still need to complete this step, check out this guide. It should walk you through the process of buying a Twilio number and configuring your app to receive incoming calls.

The <Gather> TwiML verb allows us to collect input from the user during a phone call. Gathering user input through the keypad is a core mechanism of Interactive Voice Response (IVR) systems where users can press "1" to connect to one menu of options and press "2" to reach another. These prompts can be accompanied by voice prompts to the caller, using the TwiML <Say> and <Play> verbs. In this example, we will prompt the user to enter a number to connect to a certain department within our little IVR system.

2

from twilio.twiml.voice_response import VoiceResponse, Gather

7

@app.route("/voice", methods=['GET', 'POST'])

9

"""Respond to incoming phone calls with a menu of options"""

10

# Start our TwiML response

13

# Start our <Gather> verb

14

gather = Gather(num_digits=1)

15

gather.say('For sales, press 1. For support, press 2.')

18

# If the user doesn't select an option, redirect them into a loop

23

if __name__ == "__main__":

If the user doesn't enter any input after a configurable timeout, Twilio will continue processing the TwiML in the document to determine what should happen next in the call. When the end of the document is reached, Twilio will hang up the call. In the above example, we use the <Redirect> verb to have Twilio request the same URL again, repeating the prompt for the user

If a user were to enter input with the example above, the user would hear the same prompt over and over again regardless of what button you pressed. By default, if the user does enter input in the <Gather>, Twilio will send another HTTP request to the current webhook URL with a POST parameter containing the Digits entered by the user. In the sample above, we weren't handling this input at all. Let's update that logic to also process user input if it is present.

1

from flask import Flask, request

2

from twilio.twiml.voice_response import VoiceResponse, Gather

7

@app.route("/voice", methods=['GET', 'POST'])

9

"""Respond to incoming phone calls with a menu of options"""

10

# Start our TwiML response

13

# If Twilio's request to our app included already gathered digits,

15

if 'Digits' in request.values:

16

# Get which digit the caller chose

17

choice = request.values['Digits']

19

# <Say> a different message depending on the caller's choice

21

resp.say('You selected sales. Good for you!')

24

resp.say('You need support. We will help!')

27

# If the caller didn't choose 1 or 2, apologize and ask them again

28

resp.say("Sorry, I don't understand that choice.")

30

# Start our <Gather> verb

31

gather = Gather(num_digits=1)

32

gather.say('For sales, press 1. For support, press 2.')

35

# If the user doesn't select an option, redirect them into a loop

40

if __name__ == "__main__":

You may want to have an entirely different endpoint in your application handle the processing of user input. This is possible using the "action" attribute of the <Gather> verb. Let's update our example to add a second endpoint that will be responsible for handling user input.

1

from flask import Flask, request

2

from twilio.twiml.voice_response import VoiceResponse, Gather

7

@app.route("/voice", methods=['GET', 'POST'])

9

"""Respond to incoming phone calls with a menu of options"""

10

# Start our TwiML response

13

# Start our <Gather> verb

14

gather = Gather(num_digits=1, action='/gather')

15

gather.say('For sales, press 1. For support, press 2.')

18

# If the user doesn't select an option, redirect them into a loop

24

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

26

"""Processes results from the <Gather> prompt in /voice"""

27

# Start our TwiML response

30

# If Twilio's request to our app included already gathered digits,

32

if 'Digits' in request.values:

33

# Get which digit the caller chose

34

choice = request.values['Digits']

36

# <Say> a different message depending on the caller's choice

38

resp.say('You selected sales. Good for you!')

41

resp.say('You need support. We will help!')

44

# If the caller didn't choose 1 or 2, apologize and ask them again

45

resp.say("Sorry, I don't understand that choice.")

47

# If the user didn't choose 1 or 2 (or anything), send them back to /voice

52

if __name__ == "__main__":

The action attribute takes a relative URL which would point to another route your server is capable of handling. Now, instead of conditional logic in a single route, we use actions and redirects to handle our call logic with separate code paths.

If you're building call center type applications in Python, you might enjoy our IVR Phone Tree (Flask) tutorial, which implements a full sample application using these techniques.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4