Reject incoming calls

    When you receive a call on your Plivo number, you can reject it using Plivo XML. In this example, when an incoming call is received, we will check if that number has been blacklist. If yes, we will hangup immediatly using the Hangup XML on the call and if no, we will return a 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. 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. Below is a snippet to set up a route on your webserver. Let’s call it, reject_caller. Now when we send an HTTP request to example.com/reject_caller 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/reject_caller.php.
    1. Copy the relevant code below into a text file and save it. Let’s call it, reject_caller.
    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
    
    from flask import Flask, Response
    from plivo import plivoxml
    
    app = Flask(__name__)
    
    @app.route("/reject_call/", methods=['GET','POST'])
    def hangup():
        # Generate a Hangup XML to reject an incoming call.
        response = plivoxml.ResponseElement()
        params = {'reason': 'rejected'}
        response.add(plivoxml.HangupElement(**params))
      
        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
    include Plivo::XML
    
    get '/reject_call' do
        r = Response.new()
        params = {
            'reason' => 'rejected', # Specify the reason for hangup
        }    
        r.addHangup(params)    
        xml = PlivoXML.new(r)
        puts xml.to_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
    18
    19
    
    var express = require('express');
    var plivo = require('plivo');
    var app = express();
    
    app.set('port', (process.env.PORT || 5000));
    
    app.all('/reject_calls/', function(request, response) {
        var response = plivo.Response();
        var params = {
            'reason': 'rejected'
        };
        response.addHangup(params);
        res.writeHead(200, {'Content-Type': 'text/xml'});
        res.end(response.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
    
    <?php
        require 'vendor/autoload.php';
        use Plivo\Response;
        $r = new Response();
        // Generate a Hangup XML
        $params = array(
                'reason' => 'rejected', # Specify the reason for hangup
            );
    
        $r->addHangup($params);
        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
    
    import static spark.Spark.*;
    import com.plivo.api.xml.Response;
    import com.plivo.api.xml.Speak;
    
    public class rejectcall {
        public static void main(String[] args) {
            post("/reject_calls", (request, response) - > {
                response.type("application/xml");
                Response resp = new Response()
                .children(
                    new Hangup()
                    .reason("rejected")
                );
                // 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("/reject_call", func(w http.ResponseWriter, r *http.Request) string {
    		w.Header().Set("Content-Type", "application/xml")
    
    		response := xml.ResponseElement{
    			Contents: []interface{}{
    				new(xml.HangupElement).
    					SetReason("rejected"),
    			},
    		}
    		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
    
    using System;
    using Plivo.XML;
    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    
    namespace Rejectcall
    {
        public class RejectcallController : Controller
        {
            public IActionResult Index()
            {
                Plivo.XML.Response resp = new Plivo.XML.Response();
                // Add Hangup XML Tag
                resp.AddHangup(new Dictionary<string, string>()
                {
                    {"reason","rejected"}, // Specify the reason for hangup
                });
                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 Reject Call. Enter your server URL (e.g., http://example.com/reject_caller 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

    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 Reject 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 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 rejected if the calling number is added to blacklist.

    Sample XML

    Sample output when From number is in blacklist

    <Response>
        <Hangup reason="rejected"/>
    </Response>