Receive Incoming Calls

    When you receive a call on your Plivo number, you can answer it using Plivo XML. In this example, when an incoming call is received, Plivo’s text-to-speech engine will play a message using the <Speak> XML.

    Prerequisites

    1. Sign up for a free Plivo trial account.
    2. Check out our server-side SDKs page and install the right helper based on the programming language you want to use.
    3. 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 myvoiceapp.com. Below is a snippet to set up a route on your webserver. Let’s call it speak. Now when we send an HTTP request to myvoiceapp.com/speak this route will be invoked. You will now have to configure this URL in your Plivo application.

    Note: For PHP, the route will be myvoiceapp.com/speak.php.
    1. Copy the relevant code below into a text file and save it. Let’s call it speak.
      Note: Make sure to use the appropriate file extention for your code (e.g., speak.py for Python).
    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
    
    from flask import Flask, request, make_response
    import plivo, plivoxml
    
    app = Flask(__name__)
    
    @app.route('/speak/', methods=['GET','POST'])
    def speak_xml():
        response = (plivoxml.ResponseElement()
                .add(plivoxml.SpeakElement('Hello, you just received your first call')))
        print(response.to_string())
        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
    
    require 'rubygems'
    require 'sinatra'
    require 'plivo'
    include Plivo
    
    # Set te caller ID using Dial XML
    
    get '/receive_call' do
        response = Response.new
    
        speak_body = 'Hello, you just received your first call'
        response.addSpeak(speak_body)
        xml = PlivoXML.new(response)   
    
        puts xml.to_xml() # Prints the XML
        content_type "application/xml"
        return xml.to_s() # Returns the XML
    end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    var plivo = require('plivo');
    var express = require('express');
    var app = express();
    
    app.set('port', (process.env.PORT || 5000));
    
    app.all('/receive_calls/', function(request, response) {
      // Generate a Speak XML with the details of the text to play on the call.
      var r = plivo.Response();
      r.addSpeak('Hello, you just received your first call');
      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
    
    <?php
        require 'vendor/autoload.php';
        use Plivo\XML\Response;
    
        // Generate a Speak XML with the details of the text to play on the call.
        $body = 'Hello, you just received your first call';
        $r = new Response();
        // Add speak element
        $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
    
    import static spark.Spark.*;
    import com.plivo.api.xml.Response;
    import com.plivo.api.xml.Speak;
    
    public class receivecall {
        public static void main(String[] args) {
            post("/receive_calls", (request, response) -> {
                response.type("application/xml");
                Response resp = new Response()
                        .children(
                                new Speak("Hello, you just received your first call")
                        );
                // Returns the XML
                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
    
    package main
    
    import (
    	"net/http"
    	"github.com/go-martini/martini"
    	"github.com/plivo/plivo-go/xml"
    )
    
    func main() {
    	m := martini.Classic()
    	m.Get("/", func(w http.ResponseWriter, r *http.Request) string {
    		w.Header().Set("Content-Type", "application/xml")
    
    		response := xml.ResponseElement{
    			Contents: []interface{} {
    				new(xml.SpeakElement).
    				SetContents("Hello, you just received your first call"),
    			},
    		}
    		print(response.String())
    		return response.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
    
    using System;
    using Plivo.XML;
    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    
    namespace Receivecall
    {
        public class ReceivecallController : Controller
        {
            public IActionResult Index()
            {
                Plivo.XML.Response resp = new Plivo.XML.Response();
                resp.AddSpeak("Hello, you just received your first call",
                    new Dictionary<string, string>() {
            {
            "loop",
            "3"
            }
                    });
                var output = resp.ToString();
                Console.WriteLine(output);
    
                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 Receive Call. Enter your server URL (e.g., http://www.myvoiceapp.com/speak) 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.

    Plivo Create Application Play Audio

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

    Create Text to Speech Application

    If you don’t have a Plivo phone 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 the message will be played.

    Sample XML

    <Response>
        <Speak>Hello, you just received your first call</Speak>
    </Response>