Latest Legacy

Phone tree

In this example, after a caller enters digits on the keypad, Plivo sends them as a request to the action URL. We also include a nested Speak element, so input can be gathered at any time during the Speak element.

If the caller enters a digit during the speaking of the text, the Speak element will stop speaking and wait for digits, the finishOnKey, or a timeout.

If the GetDigits element times out without input, the Speak element will complete and the GetDigits element will exit without submitting. Plivo will then process the next element in the document, which in this case is a Speak element that informs the caller that no input was received.

For a nested phone tree, you should return a GetDigits element from the action URL. In the top example, if the caller enters a four-digit PIN, Plivo will POST the Digits to the action URL, and your application can return the second XML example to play another message and accept input.

Response

<Response>
    <GetDigits action="https://<yourdomain>.com/gather_pin/" method="POST">
        <Speak>Enter your 4-digit pin number, followed by the hash key</Speak>
    </GetDigits>
    <Speak>Input not received</Speak>
</Response>

Return this XML to gather department:

<Response>
    <GetDigits action="https://<yourdomain>.com/gather_department/" method="POST">
        <Speak>Enter 1 for support and 2 for sales</Speak>
    </GetDigits>
</Response>

Example Request

1
2
3
4
5
6
7
8
9
10
from plivo import plivoxml

response = plivoxml.ResponseElement()
response.add(
    plivoxml.GetDigitsElement(
        action='https://<yourdomain>.com/gather_pin/', method='POST').add(
            plivoxml.SpeakElement(
                'Enter PIN.')))
response.add(plivoxml.SpeakElement('Input not received.'))
print(response.to_string())
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
require 'rubygems'
require 'plivo'

include Plivo::XML
include Plivo::Exceptions

begin
  response = Response.new

  params = {
      action: 'https://<yourdomain>.com/gather_pin/',
      method: 'POST'
  }

  get_digits = response.addGetDigits(params)
  input_received_speak = 'Enter PIN.'
  get_digits.addSpeak(input_received_speak)

  input_not_received_speak = 'Input not received.'
  response.addSpeak(input_not_received_speak)

  xml = PlivoXML.new(response)
  puts xml.to_xml
rescue PlivoXMLError => e
  puts 'Exception: ' + e.message
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
var plivo = require('plivo');

var response = plivo.Response();

var params = {
    'action': "https://<yourdomain>.com/gather_pin/",
    'method': "POST"
};
var get_digits = response.addGetDigits(params);

var input_received_speak = "Enter PIN.";
get_digits.addSpeak(input_received_speak);

var input_not_received_speak = "Input not received.";
response.addSpeak(input_not_received_speak);

console.log(response.toXML());

/*
Sample Output
<Response>
    <GetDigits action="https://<yourdomain>.com/gather_pin/" method="POST">
        <Speak>Enter PIN.</Speak>
    </GetDigits>
    <Speak>Input not received.</Speak>
</Response>
*/
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
<?php
    require '../vendor/autoload.php';
    use Plivo\XML\Response;

    $response = new Response();

    $params = array(
        'action' => "https://<yourdomain>.com/gather_pin/",
        'method' => "POST"
    );

    $get_digits = $response->addGetDigits($params);
    $input_received_speak = "Enter PIN.";
    $get_digits->addSpeak($input_received_speak);

    $input_not_received_speak = "Input not received.";
    $response->addSpeak($input_not_received_speak);

    Header('Content-type: text/xml');
    echo($response->toXML());

    /*
    Sample Output

    <Response>
        <GetDigits action="https://<yourdomain>.com/gather_pin/" method="POST">
            <Speak>Enter PIN.</Speak>
        </GetDigits>
        <Speak>Input not received.</Speak>
    </Response>
    */
?>
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
// Example for getdigits - phone tree
package com.plivo.api.xml.samples.getdigits;

import com.plivo.api.exceptions.PlivoXmlException;
import com.plivo.api.xml.GetDigits;
import com.plivo.api.xml.Response;
import com.plivo.api.xml.Speak;


class PhoneTree {
    public static void main(String[] args) throws PlivoXmlException {
        Response response = new Response()
                .children(


                        new GetDigits()
                                .action("https://<yourdomain>.com/gather_pin/")
                                .method("POST")
                                .children(
                                        new Speak("Enter PIN.")
                                ),


                        new Speak("Input not received.")
                );
        System.out.println(response.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
using System;
using System.Collections.Generic;
using Plivo.XML;

namespace Plivo
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			Plivo.XML.Response resp = new Plivo.XML.Response();
			Plivo.XML.GetDigits get_digits = new
				Plivo.XML.GetDigits("",
				   new Dictionary<string, string>()
			{
				{"action", "https://<yourdomain>.com/gather_pin/"},
				{ "method", "POST" }
			});
			resp.Add(get_digits);

			get_digits.AddSpeak("Enter PIN.",
				new Dictionary<string, string>() { });
			resp.AddSpeak("Input not received.",
				new Dictionary<string, string>() { });
			var output = resp.ToString();
			Console.WriteLine(output);

		}
	}
}



//<Response>
//  <GetDigits action = "https://<yourdomain>.com/gather_pin/" 
//    method="POST">
//    <Speak>Enter PIN.</Speak>
//  </GetDigits>
//  <Speak>Input not received.</Speak>
//</Response>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example for getdigits - phone tree
package main

import "github.com/plivo/plivo-go/v7/xml"

func main() {
	response := xml.ResponseElement{
		Contents: []interface{}{

			new(xml.GetDigitsElement).
				SetAction("https://<yourdomain>.com/gather_pin/").
				SetMethod("POST").
				SetContents([]interface{}{

					new(xml.SpeakElement).
						AddSpeak("\tEnter PIN."),
				}),

			new(xml.SpeakElement).
				AddSpeak("Input not received."),
		},
	}
	print(response.String())
}