<

Getting Started with IVRS

This tutorial will help you get started with building a simple Interactive Voice Response System (IVRS). There are three major steps in any IVR.

  • The user calls a number owned by the business.
  • The business then plays an automated voice message and waits for an input from the user.
  • Based on the digit entered by the user further processing of the call will take place.

A Simple Order tracking IVR

Let us assume the following scenario for a supermarket which provides home delivery. The company has a number to contact their support team. Whenever a user calls that number, based on the number from which the call is made, the order ID of the recent call is retrieved and the following text is played to the user: “ Thank you for calling supermarket. Press 1 to know the status of your order number . Press 2 to talk to the delivery agent”

If the user presses 1, the status of the order will be spoken back to the user. In case the user presses 2, then the call will be routed to the number of the agent.

Let us see how we can implement this simple IVRS flow using Plivo.

Prerequisites

  1. We need a Plivo phone number that can receive incoming calls. Refer to our guide on phone numbers to get started with Phone numbers.
  2. The phone number should have an Plivo application attached to it. You can read more about Plivo applications here.
  3. You should be able to serve a valid XML file from a public URL. This URL will be the answer_url in the Plivo application mentioned in the previous step. Please refer to our tutorial on setting up dev servers for reference.

The IVR call flow

Building an IVR System consists of eight steps as described in the diagram above. Let us look at each of steps in detail.

Step 1: User calls a Plivo phone number

When the user calls a Plivo phone number, the application attached to that number will get triggered. The URL mentioned in the answer_url

Step 2: Request to Answer URL

Each application has a answer_url configured (Check prerequisites section for more information). This answer_url will be invoked as soon as a incoming call on the number is detected.

Step 3: Fetch the XML required to initiate the IVR

The invoked answer_url is expected to return a valid XML file. For the “Simple Order Tracking IVR” we are building in this tutorial, the answer_url should return the following XML.

1
2
3
4
5
<Response>
    <GetDigits action="http://c3bb61b6.ngrok.io/action?call_uuid=<call_uuid>" method="POST">
        <Speak>Thank you for calling supermarket. Press 1 to know the status of your order number 12345. Press 2 to talk to the delivery agent</Speak>
    </GetDigits>
</Response>

This XML can be returned from the answer_url using the following Flask code. For a more detailed guide please refer to our tutorial on setting up dev servers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
order_info = {"14252150094": ["12345", "Shipped"]}
from_numbers = {}

@app.route('/ivrs', methods=['GET', 'POST'])
def IVR_responseXML():
    '''Creating a dummy data to mimic order ID and order status'''
    number = request.form.get('From')

    call_uuid = request.form.get('CallUUID')
    from_numbers[call_uuid] = number
    order_id = order_info[number][0]
    resp = '<Response><GetDigits action='
    '"http://c3bb61b6.ngrok.io/action?call_uuid={call_uuid}" method="POST">'
    '<Speak>Thank you for calling supermarket. Press 1 to know the status of '
    'your order number {order_id}. Press 2 to talk to the delivery agent'
    '</Speak></GetDigits></Response>'.format(
        order_id=order_id, call_uuid=call_uuid)
    return Response(resp, mimetype='text/xml')

Note that the order_info variable is just created to show some dummy data. It has the order ID and the shipping status associated with a phone number.

Steps 4 and 5: Play text and Gather digits

This XML when returned instructs Plivo to speak the desired text “Thank you for calling supermarket. Press 1 to know the status of your order number 12345. Press 2 to talk to the delivery agent”.

Once the user inputs the digit, the digit entered will be POSTed (since the method attribute in is set to POST) to the URL mentioned in the action parameter of .

Steps 6, 7 and 8: Fetch XML for further call flow based on IVR choice

In case the user presses 1, the XML should retrieve the status of the order and speak it back to the user. The XML in this case should look like the one below.

1
2
3
<Response>
    <Speak>The status of your order is: Shipped</Speak>
</Response>

In case the user presses 2, the XML should dial the number of the agent and connect the user in call to that number. For that to happen the XML should look like the one below.

1
2
3
4
5
<Response>
    <Dial>
        <Number>dest_number</Number>
    </Dial>
</Response>

The following snippet explains how to return these XML files based on the choice input by user.

1
2
3
4
5
6
7
8
9
10
11
12
13
@app.route('/action', methods=['GET', 'POST'])
def action_XML():
    call_uuid = request.args.get('call_uuid')
    number = from_numbers[call_uuid]
    digit = request.form.get('Digits')
    order_status = order_info[number][1]
    if digit == '1':
        resp = '<Response><Speak>The status of your order is: {order_status}</Speak></Response>'.format(
            order_status=order_status)
        return Response(resp, mimetype='text/xml')
    if digit == '2':
        resp2 = '<Response><Dial><Number>”<<Enter destination number here>>”</Number></Dial></Response>'
        return Response(resp2, mimetype='text/xml')

Further resources

This tutorial helps in creating a simple IVR. The complete source code of the example used is here. You can explore all the attributes available in GetDigits in Plivo XML documentation.