Wait XML

The Wait element waits silently for a specified number of seconds. If Wait is the first element in a XML document, Plivo will wait the specified number of seconds before picking up the call.

Prerequisites

  1. Sign up for a free Plivo trial account.
  2. Check out our Helper Libraries page and install the right helper based on the programming language you want to use.
  3. Buy a Plivo phone number. A phone number is required to receive calls. You can buy a Plivo phone number in over 20+ countries through the Buy Numbers tab on your Plivo account UI. Check the Voice API coverage page for all the supported countries.
  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. The code snippet below will create three routes on your webserver:

/basic_wait/ this route will say to the caller “I will wait for 10 seconds”, then wait for 10 seconds, then say “I just waited 10 seconds”.
/delayed_wait/ this route will wait 10 seconds, answer the call, then say to the caller “Hello”.
/beep_det/ this route will answer the call, wait for 100 seconds, then say to the caller “Hello”. During the 100 second wait, if a beep is detected like that of an answering machine or voicemail, the Wait XML will immediately end and the caller will hear “Hello”.

Now when we send an HTTP request to example.com/basic_wait/, example.com/delayed_wait/, or example.com/beep_det/ these routes will be invoked.

Note: For PHP, the routes will be example.com/basic_wait.php, example.com/delayed_wait.php, and example.com/beep_det.php.

  1. Copy the relevant code below into a text file called wait and save it.
  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
import plivoxml
from flask import Flask, Response

app = Flask(__name__)

# Example for Basic Wait
@app.route('/basic_wait/', methods=['GET','POST'])
def basic_wait():
    r = plivoxml.Response()
    r.addSpeak("I will wait for 10 seconds")
    params = {
        'length': "10" # Time to wait in seconds
    }
    r.addWait(**params)
    r.addSpeak("I just waited 10 seconds")
    print r.to_xml()
    return Response(str(r), mimetype='text/xml')

# Example for Delayed Call Answer
@app.route('/delayed_wait/', methods=['GET','POST'])
def delayed_wait():
    r = plivoxml.Response()
    params = {
        'length': "10" # Time to wait in seconds
    }
    r.addWait(**params)
    r.addSpeak("Hello")
    print r.to_xml()
    return Response(str(r), mimetype='text/xml')

# Example for Beep Detection
@app.route('/beep_det/', methods=['GET','POST'])
def beep_det():
    r = plivoxml.Response()
    params = {
        'length': "100", # Time to wait in seconds
        'beep': "true" # If a beep is detected, immediately end Wait and process next XML element
    }
    r.addWait(**params)
    r.addSpeak("Hello")
    print r.to_xml()
    return Response(str(r), mimetype='text/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
require 'rubygems'
require 'sinatra'
require 'plivo'
include Plivo

# Example for Basic Wait
get '/basic_wait/' do
    r = Response.new()
    r.addSpeak("I will wait for 10 seconds")
    params = {
        'length' => "10" # Time to wait in seconds
    }
    r.addWait(params)
    r.addSpeak("I just waited 10 seconds")

    puts r.to_xml()
    content_type 'text/xml'
    return r.to_s()
end

# Example for Delayed Call Answer
get '/delayed_wait/' do
    r = Response.new()
    params = {
        'length' => "10" # Time to wait in seconds
    }
    r.addWait(params)
    r.addSpeak("Hello")

    puts r.to_xml()
    content_type 'text/xml'
    return r.to_s()
end

# Example for Beep Detection
get '/beep_det/' do
    r = Response.new()
    params = {
        'length' => "100", # Time to wait in seconds
        'beep' => "true" # If a beep is detected, immediately end Wait and process next XML element
    }
    r.addWait(params)
    r.addSpeak("Hello")

    puts r.to_xml()
    content_type 'text/xml'
    return r.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
var express = require('express');
var app = express();

var plivo = require('plivo');
var p = plivo.RestAPI({
  authId: 'Your_AUTH_ID',
  authToken: 'Your_AUTH_TOKEN'
});

app.set('port', (process.env.PORT || 5000));

// Example for Basic Wait
app.get('/basic_wait/', function(request, response){
    var r = plivo.Response();

    r.addSpeak("I will wait for 10 seconds");
    var params = {
        'length': "10" // Time to wait in seconds
    };
    r.addWait(params);
    r.addSpeak("I just waited 10 seconds");

    console.log(r.toXML());
    response.set({'Content-Type': 'text/xml'});
    response.send(r.toXML());
});

// Example for Delayed Call Answer
app.get('/delayed_wait/', function(request, response){
    var r = plivo.Response();
    var params = {
        'length': "10" // Time to wait in seconds
    };
    r.addWait(params);
    r.addSpeak("Hello");

    console.log(r.toXML());
    response.set({'Content-Type': 'text/xml'});
    response.send(r.toXML());
});

// Example for Beep Detection
app.get('/beep_det/', function(request, response){
    var r = plivo.Response();
    var params = {
        'length': "100", // Time to wait in seconds
        'beep': "true" // If a beep is detected, immediately end Wait and process next XML element
    };
    r.addWait(params);
    r.addSpeak("Hello");

    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
<!-- basic_wait.php -->
<?php
    require 'vendor/autoload.php';
    use Plivo\Response;
    $r = new Response();
    $body = "I will wait for 10 seconds";
    $r->addSpeak($body);
    $params = array(
        'length' => '10' # Time to wait in seconds
    );
    $r->addWait($params);
    $body1 = "I just waited 10 seconds";
    $r->addSpeak($body1);

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

<!-- delayed_wait.php -->
<?php
    require 'vendor/autoload.php';
    use Plivo\Response;
    $r = new Response();
    $params = array(
        'length' => '10' # Time to wait in seconds
    );
    $r->addWait($params);
    $body = "Hello";
    $r->addSpeak($body);

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

<!-- beep_det.php -->

<?php
    require 'vendor/autoload.php';
    use Plivo\Response;
    $r = new Response();
    $params = array(
        'length' => '10', # Time to wait in seconds
        'beep' =>'true' # If a beep is detected, immediately end Wait and process next XML element
    );
    $r->addWait($params);
    $body = "Hello";
    $r->addSpeak($body);

    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
 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
// basicWait.java
package plivoexample;

import java.io.IOException;

import com.plivo.helper.exception.PlivoException;
import com.plivo.helper.xml.elements.PlivoResponse;
import com.plivo.helper.xml.elements.Speak;
import com.plivo.helper.xml.elements.Wait;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class basicWait extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PlivoResponse response = new PlivoResponse();
        Speak speak = new Speak("I will wait for 10 seconds");
        Wait wait = new Wait();
        wait.setLength(10); // Time to wait in seconds
        Speak spk = new Speak("I just waited 10 seconds");

        try {
            response.append(speak);
            response.append(wait);
            response.append(spk);
            System.out.println(response.toXML());
            resp.addHeader("Content-Type", "text/xml");
            resp.getWriter().print(response.toXML());;
        } catch (PlivoException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        String port = System.getenv("PORT");
        if(port == null)
            port ="8000";
        Server server = new Server(Integer.valueOf(port));
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        context.addServlet(new ServletHolder(new basicWait()),"/basic_wait/");
        context.addServlet(new ServletHolder(new delayedWait()),"/delayed_wait/");
        context.addServlet(new ServletHolder(new beepDetection()),"/beep_det/");
        server.start();
        server.join();
    }
}

// delayedWait.java
package plivoexample;

import java.io.IOException;
import com.plivo.helper.exception.PlivoException;
import com.plivo.helper.xml.elements.PlivoResponse;
import com.plivo.helper.xml.elements.Speak;
import com.plivo.helper.xml.elements.Wait;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class delayedWait extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PlivoResponse response = new PlivoResponse();
        Wait wait = new Wait();
        wait.setLength(10); // Time to wait in seconds
        Speak spk = new Speak("Hello");

        try {
            response.append(wait);
            response.append(spk);
            System.out.println(response.toXML());
            resp.addHeader("Content-Type", "text/xml");
            resp.getWriter().print(response.toXML());;
        } catch (PlivoException e) {
            e.printStackTrace();
        }
    }
}

// beepDetection.java
package plivoexample;

import java.io.IOException;
import com.plivo.helper.exception.PlivoException;
import com.plivo.helper.xml.elements.PlivoResponse;
import com.plivo.helper.xml.elements.Speak;
import com.plivo.helper.xml.elements.Wait;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class beepDetection extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PlivoResponse response = new PlivoResponse();
        Wait wait = new Wait();
        wait.setLength(100); // Time to wait in seconds
        wait.setBeep(true); // If a beep is detected, immediately end Wait and process next XML element
        Speak spk = new Speak("Hello");

        try {
            response.append(wait);
            response.append(spk);
            System.out.println(response.toXML());
            resp.addHeader("Content-Type", "text/xml");
            resp.getWriter().print(response.toXML());;
        } catch (PlivoException e) {
            e.printStackTrace();
        }
    }
}
 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using RestSharp;
using Plivo.XML;
using Nancy;

namespace wait_xml
{
    public class Program : NancyModule
    {
        public Program()
        {
            Get["/basic_wait/"] = x =>
            {
                Plivo.XML.Response resp = new Plivo.XML.Response();
                resp.AddSpeak("I will wait for 10 seconds", new Dictionary<string, string>() { });
                resp.AddWait(new Dictionary<string, string>()
                {
                    {"length", "10"} // Time to wait in seconds
                });
                resp.AddSpeak("I just waited 10 seconds", new Dictionary<string, string>() { });

                Debug.WriteLine(resp.ToString());
                var output = resp.ToString();
                var res = (Nancy.Response)output;
                res.ContentType = "text/xml";
                return res;
            };

            Get["/delayed_wait/"] = x =>
            {
                Plivo.XML.Response resp = new Plivo.XML.Response();
                resp.AddWait(new Dictionary<string, string>()
                {
                    {"length", "10"} // Time to wait in seconds
                });
                resp.AddSpeak("Hello", new Dictionary<string, string>() { });

                Debug.WriteLine(resp.ToString());
                var output = resp.ToString();
                var res = (Nancy.Response)output;
                res.ContentType = "text/xml";
                return res;
            };

            Get["/beep_det/"] = x =>
            {
                Plivo.XML.Response resp = new Plivo.XML.Response();
                resp.AddWait(new Dictionary<string, string>()
                {
                    {"length", "100"}, // Time to wait in seconds
                    {"beep", "true"} // If a beep is detected, immediately end Wait and process next XML element
                });
                resp.AddSpeak("Hello", new Dictionary<string, string>() { });

                Debug.WriteLine(resp.ToString());
                var output = resp.ToString();
                var res = (Nancy.Response)output;
                res.ContentType = "text/xml";
                return res;
            };
        }
    }
}

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. Lets call it Wait XML. Enter your server URL (e.g., http://www.example.com/wait) in the Answer URL field and set the method as POST. See our Application API docs to learn how to modify your application through our APIs.
  3. Click on Create to save your application.

Create Wait XML Application

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 Wait XML (name of the app) from the Plivo App dropdown list.
  3. Click on ‘Update’ to save.

Create Wait XML Application

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

Phone Number Search

Test it out

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

Sample XML

Basic Wait
<Response>
    <Speak>I will wait for 10 seconds</Speak>
    <Wait length="10" />
    <Speak>I just waited 10 seconds</Speak>
</Response>

Delayed  Wait
<Response>
    <Wait length="10" />
    <Speak>Hello</Speak>
</Response>

Beep Detection
<Response>
    <Wait length="10" beep="true" />
    <Speak>Hello</Speak>
</Response>

Next Step

Learn about the Speak API.

  1. Make an Outbound Call
  2. Play a Text-to-speech Message
  3. Connect Call to a Second Person
  4. Greet Caller by Name
  5. Play MP3/WAV Audio to Caller
  6. Hangup Using API
  7. Receive Incoming Call
  8. Forward Incoming Call
  9. Record Using API
  10. Screen Incoming Call
  11. Reject incoming call
  12. Get Details of all Calls
  13. Get Details of a single Call
  14. Get Details of Live Calls
  15. Build an IVR Phone Menu
  16. Conference Call
  17. Call Forward
  18. SIP Endpoint (Direct-Dial)
  19. Inbound Trunk
  20. Outbound Trunk