Phone IVR

    An IVR (Interactive Voice Response) system is also known as phone menu system. It is an automated phone system that interacts with a caller via pre-recorded or dynamic text-to-speech messages and inputs, via the keypad.

    IVR systems are popular as they can handle large call volumes and help reduce the costs associated with customer service. With our app, you can build an entire phone menu system on the Plivo platform (in any computer language), without relying on expensive physical infrastructure.

    The instructions below will teach you how to build your own IVR system that can:

    • Read a pre recorded text in different languages if ‘1’ is pressed.
    • Play a song if ‘2’ is pressed.
    • Say “Sorry, I didn’t catch that. Please hang up and try again later.” if nothing is pressed.

    The app will operate in the following sequence:

    1. The user dials the Plivo IVR phone number.
    2. The user is prompted with the message “Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages. Press 2 to listen to a song.”
    3. During the call, if the user presses ‘1’, then Plivo reads another message “Press 1 for English. Press 2 for French. Press 3 for Russian” . If the user presses ‘2’, then Plivo plays a song.
    4. If anything else is pressed or if the user does nothing, then Plivo says “Sorry, I didn’t catch that. Please hang up and try again later.” and hangs up.

    Prerequisites

    1. Sign up for a free Plivo trial account.
    2. Check out our server SDKs page and install the right helper based on the programming language you want to use.
    3. Buy a Plivo phone number (optional).
    4. Use a web hosting service to host your web application. There are many inexpensive cloud hosting providers that you can use for just a few dollars a month. Follow the instructions of your hosting provider to host your web application.

    Set up a Web Server

    Let’s assume your web server is located at example.com. Below is a snippet to set up a route on your webserver. Let’s call it, response/ivr. Now when we send an HTTP request to example.com/response/ivr this route will be invoked. You will now have to configure this URL in your Plivo application.

    Note: For PHP, the route will be `example.com/ivr.php`.
    1. Copy the relevant code below into a text file and save it. Let’s call it, ivr.

    2. Next, you will now have to configure this URL in your Plivo application.

    Code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    
    # -*- coding: utf-8 -*-
    from flask import Flask, Response, request, url_for
    from plivo import plivoxml
    
    # This file will be played when a caller presses 2.
    plivo_song = "https://s3.amazonaws.com/plivocloud/music.mp3"
    # This is the message that Plivo reads when the caller dials in
    ivr_message1 = "Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages.  \
                    Press 2 to listen to a song."
    ivr_message2 = "Press 1 for English. Press 2 for French. Press 3 for Russian"
    # This is the message that Plivo reads when the caller does nothing at all
    noinput_message = "Sorry, I didn't catch that. Please hangup and try again \
                        later."
    # This is the message that Plivo reads when the caller inputs a wrong number.
    wronginput_message = "Sorry, it's wrong input."
    
    app = Flask(__name__)
    
    @app.route('/ivr/', methods=['GET','POST'])
    def ivr():
        response = plivoxml.ResponseElement()
        getinput_action_url = "http://www.foo.com/firstbranch/"
        response.add(plivoxml.GetInputElement().
            set_action('https://www.foo.com/result').
            set_method('POST').
            set_input_type('dtmf').
            set_digit_end_timeout(5).
            set_redirect(True).add(
                plivoxml.SpeakElement(ivr_message1)))
        response.add(plivoxml.SpeakElement(noinput_message))
        return Response(response.to_string(), mimetype='application/xml')
    
    @app.route('/ivr/firstbranch/', methods=['GET','POST'])
    def firstbranch():
        response = plivoxml.ResponseElement()
        digit = request.values.get('Digits')
        if digit == "1":
            # Read out a text.
            getinput_action_url = "http://www.foo.com/secondbranch/"
            response.add(plivoxml.GetInputElement().
                set_action('https://www.foo.com/result').
                set_method('POST').
                set_input_type('dtmf').
                set_digit_end_timeout(5).
                set_redirect(True).add(
                    plivoxml.SpeakElement(ivr_message2)))
            response.add(plivoxml.SpeakElement(noinput_message))
    
        elif digit == "2":
            # Listen to a song
            response.add_play(plivo_song)
    
        else:
            response.add_speak(wronginput_message)
        return Response(response.to_string(), mimetype='application/xml')
    
    @app.route('/ivr/secondbranch/', methods=['GET','POST'])
    def secondbranch():
        response = plivoxml.ResponseElement()
        digit = request.values.get('Digits')
    
        if digit == "1":
            text = u"This message is being read out in English"
            params = {
                'language': "en-GB",
            }
            response.add_speak(text,**params)
    
        elif digit == "2":
            text = u"Ce message est lu en français"
            params = {
                'language': "fr-FR",
            }
            response.add_speak(text,**params)
    
        elif digit == "3":
            text = u"Это сообщение было прочитано в России"
            params = {
                'language': "ru-RU",
            }
            response.add_speak(text,**params)
    
        else:
            response.add_speak(wronginput_message)
        return Response(response.to_string(), mimetype='application/xml')
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', debug=True)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    
    # encoding: utf-8
    require 'rubygems'
    require 'sinatra'
    require 'plivo'
    
    include Plivo
    include Plivo::XML
    
    # This file will be played when a caller presses 2.
    $plivo_song = "https://s3.amazonaws.com/plivocloud/music.mp3"
    # This is the message that Plivo reads when the caller dials in
    $ivr_message1 = "Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages. Press 2 to listen to a song."
    
    $ivr_message2 = "Press 1 for English. Press 2 for French. Press 3 for Russian"
    # This is the message that Plivo reads when the caller does nothing at all
    $noinput_message = "Sorry, I did not catch that. Please hangup and try again later."
    
    # This is the message that Plivo reads when the caller inputs a wrong number.
    $wronginput_message = "Sorry, it's wrong input."
    
    post '/ivr/' do
        r = Response.new()
    
        getinput_action_url = "https://example.com/ivr/firstbranch/"
        params = {
            action: getinput_action_url, 
            method: 'POST', 
            digitEndTimeout: '5',
            inputType:'dtmf',
            redirect:'true'
        }
        getinput = r.addGetInput(params)
        getinput.addSpeak($ivr_message1)
        r.addSpeak($noinput_message)
    
        xml = PlivoXML.new(r)
        content_type "application/xml"
        return xml.to_s()
    
    end
    
    post '/ivr/firstbranch/' do
        digit = params[:Digits]
        r = Response.new()
        
        if (digit == "1")
            getinput_action_url = "https://example.com/ivr/secondbranch/"
            params = {
                action: getinput_action_url, 
                method: 'POST', 
                digitEndTimeout: '5',
                inputType:'dtmf',
                redirect:'true'
            }
            getinput = r.addGetInput(params)
            getinput.addSpeak($ivr_message2)
            r.addSpeak($noinput_message)
    
        elsif (digit == "2")
            r.addPlay($plivo_song)
        else
            r.addSpeak($wronginput_message)
        end
    
        xml = PlivoXML.new(r)
        content_type "application/xml"
        return xml.to_s()
    end 
    
    post '/ivr/secondbranch/' do
        digit = params[:Digits]
    
        r = Response.new()
    
        if (digit == "1")
            body = "This message is being read out in English"
            params = {
                'language'=> "en-GB"
            }
    
            r.addSpeak(body,params)
        elsif (digit == "2")
            body = "Ce message est lu en français"
            params = {
                'language'=> "fr-FR"
            }
    
            r.addSpeak(body,params)
        elsif (digit == "3")
            body = "Это сообщение было прочитано в России"
            params = {
                'language'=> "ru-RU"
            }
    
            r.addSpeak(body,params)
        else
            r.addSpeak($wronginput_message)
        end
    
        xml = PlivoXML.new(r)
        content_type "application/xml"
        return xml.to_s()
    
    end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    
    var plivo = require('plivo');
    var express = require('express');
    var bodyParser = require('body-parser');
    var app = express();
    
    app.use(bodyParser.urlencoded({extended: true}));
    app.set('port', (process.env.PORT || 5000));
    
    // This file will be played when a caller presses 2.
    var PlivoSong = "https://s3.amazonaws.com/plivocloud/music.mp3";
    // This is the message that Plivo reads when the caller dials in
    var IvrMessage1 = "Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages. Press 2 to listen to a song.";
    var IvrMessage2 = "Press 1 for English. Press 2 for French. Press 3 for Russian";
    // This is the message that Plivo reads when the caller does nothing at all
    var NoinputMessage = "Sorry, I didn't catch that. Please hangup and try again later.";
    // This is the message that Plivo reads when the caller inputs a wrong number.
    var WronginputMessage = "Sorry, you've entered an invalid input.";
    
    app.post('/ivr/', function(request, response) {
      var r = plivo.Response();
      var getinput_action_url, params, getDigits;
      getinput_action_url = request.protocol + '://' + request.headers.host + '/ivr/firstbranch/';
      params = {
            'action': getinput_action_url,
            'method': 'POST',
            'inputType': 'dtmf',
            'digitEndTimeout': '5',
            'redirect': 'true',
      };
      get_input = r.addGetInput(params);
      get_input.addSpeak(IvrMessage1);
      r.addSpeak(NoinputMessage);
    
      console.log(r.toXML());
      response.set({'Content-Type': 'text/xml'});
      response.send(r.toXML());
    });
    
    app.post('/ivr/firstbranch/', function(request, response) {
      var r = plivo.Response();
      var getinput_action_url, params, getDigits;
      var digit = request.body.Digits;
      console.log(digit);
      if (digit === '1') {
        getinput_action_url = request.protocol + '://' + request.headers.host + '/ivr/secondbranch/';
        params = {
            'action': getinput_action_url,
            'method': 'POST',
            'inputType': 'dtmf',
            'digitEndTimeout': '5',
            'redirect': 'true',
        };
        get_input = r.addGetInput(params);
        get_input.addSpeak(IvrMessage2);
        r.addSpeak(NoinputMessage);
      } else if (digit === '2') {
        r.addPlay(PlivoSong);
      } else {
        r.addSpeak(WRONG_INPUT_MESSAGE);
      }
    
      console.log(r.toXML());
      response.set({'Content-Type': 'text/xml'});
      response.send(r.toXML());
    });
    
    app.all('/ivr/secondbranch/', function(request, response) {
      var r = plivo.Response();
      var text, params;
      var digit = request.body.Digits || request.query.Digits;
      if (digit === "1") {
        text = "This message is being read out in English";
        params = {'language': 'en-US'};
        r.addSpeak(text, params);
      } else if (digit === "2") {
        text = "Ce message est lu en français";
        params = {'language': 'fr-FR'};
        r.addSpeak(text, params);
      } else if (digit === "3") {
        text = "Это сообщение было прочитано в России";
        params = {'language': 'ru-RU'};
        r.addSpeak(text, params);
      } else {
        r.addSpeak(WronginputMessage);
      }
    
      console.log(r.toXML());
      response.set({'Content-Type': 'text/xml'});
      response.send(r.toXML());
    });
    
    app.listen(app.get('port'), function() {
        console.log('Node app is running on port', app.get('port'));
    });
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    
    <!--ivr.php-->
    
    <?php
        require 'vendor/autoload.php';
        use Plivo\XML\Response;
        # This is the message that Plivo reads when the caller dials in
        $IvrMessage = "Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages. Press 2 to listen to a song.";
        # This is the message that Plivo reads when the caller does nothing at all
        $NoinputMessage = "Sorry, I didn't catch that. Please hangup and try again later.";
        # This is the message that Plivo reads when the caller inputs a wrong number.
        $WronginputMessage = "Sorry, wrong input.";
    
        $r = new Response();
        
        $getinput_action_url = "https://example.com/firstbranch.php";
        $get_input = $r->addGetInput([
                    'action' => $getinput_action_url,
                    'method' => "POST",
                    'digitEndTimeout' => "5",
                    'inputType' => "dtmf",
                    'redirect' => "true",
                ]);
        $get_input->addSpeak($IvrMessage);
        $r->addSpeak($NoinputMessage);
        Header('Content-type: text/xml');
        echo($r->toXML());
    ?>
    
    <!--firstbranch.php-->
    
    <?php
        require 'vendor/autoload.php';
        use Plivo\XML\Response;
        # This file will be played when a caller presses 2.
        $PlivoSong = "https://s3.amazonaws.com/plivocloud/music.mp3";
        $IvrMessage = "Press 1 for English. Press 2 for French. Press 3 for Russian";
        # This is the message that Plivo reads when the caller does nothing at all
        $NoinputMessage = "Sorry, I didn't catch that. Please hangup and try again later.";
        # This is the message that Plivo reads when the caller inputs a wrong number.
        $WronginputMessage = "Sorry, wrong input.";
    
        $r = new Response();
    
        $digit = $_REQUEST['Digits'];
        if ($digit == '1'){
            $getinput_action_url = "https://example.com/secondbranch.php";
            $get_input = $r->addGetInput([
                    'action' => $getinput_action_url,
                    'method' => "POST",
                    'digitEndTimeout' => "5",
                    'inputType' => "dtmf",
                    'redirect' => "true",
                ]);
            $get_input->addSpeak($IvrMessage);
            $r->addSpeak($NoinputMessage);
        }
        else if ($digit == '2'){
            $r->addPlay($PlivoSong);
        }
        else {
            $r->addSpeak($WronginputMessage);
        }
        Header('Content-type: text/xml');
        echo($r->toXML());
    ?>
    
    <!--secondbranch.php-->
    
    <?php
        require 'vendor/autoload.php';
        use Plivo\XML\Response;
    
        # This is the message that Plivo reads when the caller inputs a wrong number.
        $WronginputMessage = "Sorry, wrong input.";
    
        $r = new Response();
        $digit = $_REQUEST['Digits'];
        if ($digit == '1'){
            $body = "This message is being read out in English";
            $params = array(
                'language' => "en-GB"
              );
            $r->addSpeak($body,$params);
        }
        else if ($digit == '2'){
            $body = "Ce message est lu en français";
            $params = array(
                'language' => "fr-FR"
              );
            $r->addSpeak($body,$params);
        }
        else if ($digit == '3'){
            $body = "Это сообщение было прочитано в России";
            $params = array(
                'language' => "ru-RU"
              );
            $r->addSpeak($body,$params);
        }
        else {
            $r->addSpeak($WronginputMessage);
        }
        Header('Content-type: text/xml');
        echo($r->toXML());
    ?>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    
    import static spark.Spark.*;
    
    import com.plivo.api.xml.GetInput;
    import com.plivo.api.xml.Play;
    import com.plivo.api.xml.Response;
    import com.plivo.api.xml.Speak;
    
    public class ivr {
        public static void main(String[] args) {
            // This file will be played when a caller presses 2.
            String PlivoSong = "https://s3.amazonaws.com/plivocloud/music.mp3";
            // This is the message that Plivo reads when the caller dials in
            String IvrMessage1 = "Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages. Press 2 to listen to a song.";
            String IvrMessage2 = "Press 1 for English. Press 2 for French. Press 3 for Russian";
            // This is the message that Plivo reads when the caller does nothing at all
            String NoinputMessage = "Sorry, I didn't catch that. Please hangup and try again later.";
            // This is the message that Plivo reads when the caller inputs a wrong number.
            String WronginputMessage = "Sorry, it's wrong input.";
            post("/ivr/", (request, response) -> {
                response.type("application/xml");
                Response resp = new Response();
                resp.children(
                    new GetInput()
                            .action("https://77efc2639d0c.ngrok.io/ivr/firstbranch/")
                            .method("POST")
                            .inputType("dtmf")
                            .digitEndTimeout(5)
                            .redirect(true)
                            .children(
                                    new Speak(IvrMessage1)
                            )
                );
                resp.children(new Speak(NoinputMessage));
                return resp.toXmlString();
            });
            post("/ivr/firstbranch/", (request, response) -> {
                response.type("application/xml");
                String digit = request.queryParams("Digits");
                Response resp = new Response();
                if (digit.equals("1")){
                    resp.children(
                            new GetInput()
                                    .action("https://77efc2639d0c.ngrok.io/ivr/secondbranch/")
                                    .method("POST")
                                    .inputType("dtmf")
                                    .digitEndTimeout(5)
                                    .redirect(true)
                                    .children(
                                            new Speak(IvrMessage2)
                                    )
                    );
                    resp.children(new Speak(NoinputMessage));
                }
                else if (digit.equals("2")){
                    resp.children(
                            new Play(PlivoSong)
                    );
                }
                else {
                    resp.children(
                            new Speak(WronginputMessage)
                    );
                }
                return resp.toXmlString();
            });
            post("/ivr/secondbranch/", (request, response) -> {
                response.type("application/xml");
                String digit = request.queryParams("Digits");
                Response resp = new Response();
                if (digit.equals("1")){
                    resp.children(
                            new Speak("This message is being read out in English", "MAN","en-GB",1)
                    );
                }
                else if (digit.equals("2")){
                    resp.children(
                            new Speak("Ce message est lu en français", "MAN", "fr-FR", 1)
                    );
                }
                else if (digit.equals("3")){
                    resp.children(
                            new Speak("Это сообщение было прочитано в России", "MAN", "ru-RU", 1)
                    );
                }
                else {
                    resp.children(
                            new Speak(WronginputMessage)
                    );
                }
                return resp.toXmlString();
            });
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    
    package main
    
    import (
    	"github.com/go-martini/martini"
    	"github.com/plivo/plivo-go/xml"
    	"net/http"
    )
    
    func main() {
    	m := martini.Classic()
    	const
    	(
    	// This file will be played when a caller presses 2.
    	PlivoSong = "https://s3.amazonaws.com/plivocloud/music.mp3"
    	// This is the message that Plivo reads when the caller dials in
    	IvrMessage1 = "Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages. Press 2 to listen to a song."
    	IvrMessage2 = "Press 1 for English. Press 2 for French. Press 3 for Russian"
    	// This is the message that Plivo reads when the caller does nothing at all
    	NoInputMessage = "Sorry, I didn't catch that. Please hangup and try again later."
    	// This is the message that Plivo reads when the caller inputs a wrong number.
    	WrongInputMessage = "Sorry, it's wrong input."
    	)
    
    	m.Post("/ivr/", func(w http.ResponseWriter, r *http.Request) string {
    		w.Header().Set("Content-Type", "application/xml")
    		response := xml.ResponseElement{
    			Contents: []interface{}{
    				new(xml.GetInputElement).
    				SetAction("https://www.foo.com/ivr/firstbranch/").
    				SetMethod("POST").
    				SetDigitEndTimeout(5).
    				SetInputType("dtmf").
    				SetRedirect(true).
    				SetContents([]interface{}{new(xml.SpeakElement).
    					SetContents(IvrMessage1),
    					}),
    				new(xml.SpeakElement).
    					SetContents(NoInputMessage),
    			},
    		}
    		return response.String()
    	})
    
    	m.Post("/ivr/firstbranch/", func(w http.ResponseWriter, r *http.Request) string {
    	w.Header().Set("Content-Type", "application/xml")
    	digit := r.FormValue("Digits")
    	if digit == "1" {
    		return xml.ResponseElement{
    			Contents: []interface{}{
    				new(xml.GetInputElement).
    				SetAction("https://www.foo.com/ivr/firstbranch/").
    				SetMethod("POST").
    				SetDigitEndTimeout(5).
    				SetInputType("dtmf").
    				SetRedirect(true).
    				SetContents([]interface{}{new(xml.SpeakElement).
    					SetContents(IvrMessage2),
    					}),
    				new(xml.SpeakElement).
    					SetContents(NoInputMessage),
    			},
    		}.String()
    	} else if digit == "2" {
    		return xml.ResponseElement{
    			Contents: []interface{}{
    				new(xml.PlayElement).
    					SetContents(PlivoSong),
    			},
    		}.String()
    	} else {
    		return xml.ResponseElement{
    			Contents: []interface{}{
    				new(xml.SpeakElement).
    					SetContents(WrongInputMessage),
    				},
    			}.String()
    		}
    	})
    
    	m.Post("/ivr/secondbranch/", func(w http.ResponseWriter, r *http.Request) string {
    		w.Header().Set("Content-Type", "application/xml")
    		digit := r.FormValue("Digits")
    		if digit == "1" {
    			return xml.ResponseElement{
    				Contents: []interface{}{
    					new(xml.SpeakElement).
    						SetLanguage("en-GB").
    						SetContents("This message is being read out in English"),
    					},
    				}.String()
    		} else if digit == "2" {
    			return xml.ResponseElement{
    				Contents: []interface{}{
    					new(xml.SpeakElement).
    						SetLanguage("fr-FR").
    						SetContents("Ce message est lu en français"),
    					},
    			}.String()
    		} else if digit == "3" {
    		return xml.ResponseElement{
    			Contents: []interface{}{
    				new(xml.SpeakElement).
    					SetLanguage("ru-RU").
    					SetContents("Это сообщение было прочитано в России"),
    				},
    			}.String()
    		} else {
    			return xml.ResponseElement{
    				Contents: []interface{}{
    					new(xml.SpeakElement).
    						SetContents(WrongInputMessage),
    					},
    			}.String()
    		}
    		})
    	m.Run()
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    
    using System;
    using Plivo.XML;
    using Microsoft.AspNetCore.Mvc;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    namespace Ivrphonetree.Controllers
    {
        public class IvrController : Controller
        {
            //  This file will be played when a caller presses 2.
            String PlivoSong = "https://s3.amazonaws.com/plivocloud/music.mp3";
            // This is the message that Plivo reads when the caller dials in
            String IvrMessage1 = "Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages. Press 2 to listen to a song.";
            String IvrMessage2 = "Press 1 for English. Press 2 for French. Press 3 for Russian";
            // This is the message that Plivo reads when the caller does nothing at all
            String NoinputMessage = "Sorry, I didn't catch that. Please hangup and try again later.";
            // This is the message that Plivo reads when the caller inputs a wrong number.
            String WronginputMessage = "Sorry, it's a wrong input.";
    
            // GET: /<controller>/
            public IActionResult Index()
            {
                var resp = new Response();
                Plivo.XML.GetInput get_input = new
                    Plivo.XML.GetInput("",
                        new Dictionary<string, string>()
                        {
                            {"action", "https://www.foo.com/ivr/firstbranch/"},
                            {"method", "POST"},
                            {"digitEndTimeout", "5"},
                            {"inputType", "dtmf"},
                            {"redirect", "true"},
                        });
                resp.Add(get_input);
                get_input.AddSpeak(IvrMessage1,
                    new Dictionary<string, string>() { });
                resp.AddSpeak(NoinputMessage,
                    new Dictionary<string, string>() { });
    
                var output = resp.ToString();
                return this.Content(output, "text/xml");
            }
            // First branch of IVR phone tree
            public IActionResult FirstBranch()
            {
                String digit = Request.Query["Digits"];
                Debug.WriteLine("Digit pressed : {0}", digit);
    
                var resp = new Response();
    
                if (digit == "1")
                {
                    String getinput_action_url = "https://www.foo.com/ivr/secondbranch/";
    
                    // Add GetInput XML Tag
                    Plivo.XML.GetInput get_input = new
                    Plivo.XML.GetInput("",
                        new Dictionary<string, string>()
                        {
                            {"action", getinput_action_url},
                            {"method", "POST"},
                            {"digitEndTimeout", "5"},
                            {"finishOnKey", "#"},
                            {"inputType", "dtmf"},
                            {"redirect", "true"},
                        });
                    resp.Add(get_input);
                    get_input.AddSpeak(IvrMessage2,
                        new Dictionary<string, string>() { });
                    resp.AddSpeak(NoinputMessage,
                        new Dictionary<string, string>() { });
                }
                else if (digit == "2")
                {
                    // Add Play XML Tag
                    resp.AddPlay(PlivoSong, new Dictionary<string, string>() { });
                }
                else
                {
                    // Add Speak XML Tag
                    resp.AddSpeak(WronginputMessage,
                        new Dictionary<string, string>() { });
                }
    
                Debug.WriteLine(resp.ToString());
    
                var output = resp.ToString();
                return this.Content(output, "text/xml");
            }
            // Second branch of IVR phone tree
            public IActionResult SecondBranch()
            {
                var resp = new Response();
                String digit = Request.Query["Digits"];
                Debug.WriteLine("Digit pressed : {0}", digit);
    
                // Add Speak XMLTag
                if (digit == "1")
                {
                    resp.AddSpeak("This message is being read out in English",
                       new Dictionary<string, string>()
                       {
                        { "language","en-GB"}
                    });
                }
                else if (digit == "2")
                {
                    resp.AddSpeak("Ce message est lu en français",
                       new Dictionary<string, string>()
                       {
                        { "language","fr-FR"}
                    });
                }
                else if (digit == "3")
                {
                    resp.AddSpeak("Это сообщение было прочитано в России",
                       new Dictionary<string, string>()
                       {
                        { "language","ru-RU"}
                    });
                }
                else
                {
                    resp.AddSpeak(WronginputMessage,
                        new Dictionary<string, string>() { });
                }
    
                Debug.WriteLine(resp.ToString());
    
                var output = resp.ToString();
                return this.Content(output, "text/xml");
            }
        }
    }
    

    Create an Application

    1. Create an Application by visiting the Application Page and click on New Application or by using Plivo’s Application API.
    2. Give your application a name. Let’s call it Phone IVR. Enter your server URL (e.g., http://www.example.com/response/ivr) in the Answer URL field and set the method as GET. See our Application API docs to learn how to modify your application through our APIs.
    3. Click on Create to save your application.

    Plivo Create Application Phone IVR

    Assign a Plivo number to your app

    1. Navigate to the Numbers page and select the phone number you want to use for this app.
    2. Select Phone IVR (name of the app) from the Plivo App dropdown list.
    3. Click on ‘Update’ to save.

    Assign Application Phone IVR

    If you don’t have a number, go to the Buy Number page to purchase a Plivo phone number.

    Voice Phone Number Search

    Test and validate

    When you make a call to your Plivo number, the call will be answered by Plivo and Phone menu will be played.

    Sample Output

    <Response>
        <GetInput action="https://example.com/ivr/firstbranch/" method="POST" digitEndTimeout="5" inputType="dtmf" redirect="true">
            <Speak>
                Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages. Press 2 to listen to a song.
            </Speak>
        </GetInput>
        <Speak>
            Sorry, I didn't catch that. Please hangup and try again later.
        </Speak>
    </Response>

    If 1 is pressed, another menu is read out. Following is the generated Speak XML.

    <Response>
        <GetInput action="https://example.com/ivr/secondbranch/" method="POST" digitEndTimeout="5" inputType="dtmf" redirect="true">
            <Speak>Press 1 for English. Press 2 for French. Press 3 for Russian</Speak>
        </GetInput>
        <Speak>Sorry, I didn't catch that. Please hangup and try again later.</Speak>
    </Response>

    If 1 is pressed, the English text is read out. Following is the generated Speak XML.

    <Response>
       <Speak language="en-US">This message is being read out in English</Speak>
    </Response>

    If 2 is pressed, the French text is read out. Following is the generated Speak XML.

    <Response>
        <Speak language="fr-FR">Ce message est lu en français</Speak>
    </Response>

    If 3 is pressed, the Russian text is read out. Following is the generated Speak XML.

    <Response>
        <Speak language="ru-RU">Это сообщение было прочитано в России</Speak>
    </Response>

    If 2 is pressed, a music is played. Following is the generated Play XML.

    <Response>
       <Play>https://s3.amazonaws.com/plivocloud/music.mp3</Play>
    </Response>