Getting the call response from the menu with Twilio

3 min read 07-10-2024
Getting the call response from the menu with Twilio


Unlocking User Choice: Getting Call Responses from a Twilio Menu

Imagine you're building a voice application where users need to navigate a menu to access different features. How do you capture their selections and act upon them? This is where Twilio's power shines, allowing you to seamlessly gather user input from a menu and respond accordingly.

Let's dive into a common scenario where you want to present users with options and then react to their choice.

The Problem: You want to build a voice application where users can choose from a menu of options, but you need a way to capture their selection and proceed based on their input.

Rephrased: How do you build a voice application where users can choose from a menu, and then you can use their choice to trigger different actions?

The Setup

Imagine you're creating a simple call center system. You want users to select from three options:

  1. Press 1 for Sales
  2. Press 2 for Support
  3. Press 3 for Billing

Here's a basic Twilio TwiML code snippet to create this menu:

<Response>
  <Gather input="dtmf" numDigits="1" timeout="5">
    <Say>Welcome! Press 1 for Sales, 2 for Support, or 3 for Billing.</Say>
  </Gather>
  <Say>We didn't understand your choice. Please try again.</Say>
</Response>

This code will:

  1. Welcome the user: The <Say> element will speak the welcome message.
  2. Gather input: The <Gather> element will listen for a single digit (defined by numDigits="1") from the user's keypad. It waits for 5 seconds before moving on (defined by timeout="5").
  3. Handle invalid input: If the user doesn't press a valid digit within the timeout, the <Say> element after the <Gather> element will play the "We didn't understand..." message.

Capturing the Response

But how do you actually get the user's choice from the menu? Here comes the power of TwiML:

<Response>
  <Gather input="dtmf" numDigits="1" timeout="5">
    <Say>Welcome! Press 1 for Sales, 2 for Support, or 3 for Billing.</Say>
  </Gather>
  <Say>You pressed <Say-as interpret-as="digits"><Gather input="dtmf" numDigits="1" action="/handle-response" method="POST"></Gather></Say-as>.</Say>
</Response>

In this code:

  1. We use Gather again: The Gather element now has an action and method attribute.
  2. action attribute: This attribute tells Twilio where to send the user's response, in this case, /handle-response.
  3. method attribute: The method attribute specifies how the response should be sent, in this case, using the HTTP POST method.

Handling the Response

When the user presses a digit, Twilio will send the response to the /handle-response URL. You'll need to set up a web endpoint (using your favorite server-side language) to receive this POST request. The request will contain a parameter called Digits holding the user's input.

For example, in Python using Flask:

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/handle-response', methods=['POST'])
def handle_response():
  digits = request.form['Digits']
  if digits == '1':
    # Handle Sales
    return jsonify({"message": "Connecting you to Sales..."})
  elif digits == '2':
    # Handle Support
    return jsonify({"message": "Connecting you to Support..."})
  elif digits == '3':
    # Handle Billing
    return jsonify({"message": "Connecting you to Billing..."})
  else:
    return jsonify({"message": "Invalid choice. Please try again."})

if __name__ == '__main__':
  app.run(debug=True)

This code:

  1. Receives the POST request: It defines a route /handle-response to handle incoming requests.
  2. Extracts the Digits: It retrieves the user's input from the request data.
  3. Handles the choice: It uses if statements to determine the user's choice and responds accordingly.

Conclusion

By combining the power of Twilio's TwiML and your own backend logic, you can build interactive voice applications that seamlessly capture user input from a menu and respond accordingly. Remember to:

  • Design clear menus: Make it easy for users to understand the options.
  • Provide feedback: Let users know their choice has been received.
  • Handle errors gracefully: Provide clear messages for invalid inputs.

As your applications become more complex, you can explore advanced features like:

  • Multiple levels of menus: Create nested menus for more complex interactions.
  • Dynamic content: Use variables to generate menus based on user data.
  • Speech recognition: Allow users to speak their choices instead of using the keypad.

With Twilio, the possibilities are endless for building engaging and intuitive voice experiences!