Screen incoming calls using PHP

    Overview

    Screening incoming calls are necessary when you do not want to receive calls from specific phone numbers or a country. Plivo makes it easy for you to create an application and set up a simple call screening app to block a specific number or a list of numbers or a country itself. You can follow the instructions in this guide to quickly create an application to create a blocklist to screen incoming calls to your number(s).

    Start your implementation with Plivo to screen incoming calls using PHLO or the traditional XML way. PHLO allows you to create and deploy the call flows using its intuitive canvas in few clicks. Refer to the instructions from the respective tabs below to start your integration with PHLO or XML as you wish.

    To screen incoming calls to your Plivo number, you can create and deploy a PHLO with a few clicks on the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, you only pay for calls you make/receive, and building with PHLO is free.

    Note: When you create a PHLO application to handle the incoming calls to your Plivo number(s), you don't have to write a single line of code because you will define the instructions to manage the call flow of the incoming calls on the PHLO itself.

    Outline

    Screen Incoming Calls- Call Flow

    Implementation

    In this section, we will guide you to create a PHLO to reject all incoming calls to your Plivo number(s).

    Prerequisites

    1. Create a Plivo Account(if you don’t have one already): You can sign up with your work email address and complete the phone verification step using your mobile number.
    2. Buy a Plivo number: You must have a voice-enabled Plivo phone number to receive incoming calls. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.
    3. PHLO Application: When you receive a call on a Plivo voice-enabled number, you can control the call flow by associating a PHLO application to that Plivo phone number. Plivo will fetch the PHLO associated with the number and expect valid instructions via PHLO to handle the call.

    Create the PHLO - Block phone number(s)

    You can block incoming calls from a specific incoming phone number by referring to the below GIF and instructions.

    Create the PHLO to Block calls from a specific number

    • On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any. If this is your first PHLO, then the PHLO page will be empty.
    • Click Create New PHLO to build a new PHLO.
    • On the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the Start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the API Request trigger state.
    • From the list of components, on the left hand side, drag and drop the Branch component onto the canvas. This will add a Branch node onto the canvas.
    • Connect the Start node with the “Branch” node, using the Incoming Call trigger state.
    • Select the information i icon from the Branch component
    • The branch component allows you to compare values. In this case, we will compare from number for blocking specific phone number(s).
      • Variable to compare: The value you would receive on the call.
        • For Caller ID, enter {{Start.call.from}}
      • Operation: It has many operations. For comparing you should select “Is equal to”
      • Value to compare: The value you want to compare with.
        • For Caller ID, enter the Caller ID you want to block. Ex. 1-415-777-8888
    • Click Validate to save the settings. The branch component will have two output nodes:
      • No Match: When the values do not match
      • Condition 1: When the condition matches.
      Note: The condition fields are editable. You can change the name if you have more values to compare. This would make the PHLO easier to track.
    • Connect No Match to the Call forwarding component.
    • You can choose to leave the Condition 1 node empty or attach the Hangup component. The calls will be blocked in either case.

    Create the PHLO - Block a Country

    You can block incoming calls from a specific incoming phone number by referring to the below GIF and instructions.

    Create the PHLO to Block calls from a specific number

    • On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any. If this is your first PHLO, then the PHLO page will be empty.
    • Click Create New PHLO to build a new PHLO.
    • On the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the Start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the API Request trigger state.
    • From the list of components, on the left hand side, drag and drop the Branch component onto the canvas. This will add a Branch node onto the canvas.
    • Connect the Start node with the “Branch” node, using the Incoming Call trigger state.
    • Select the information i icon from the Branch component
    • The branch component allows you to compare values. In this case, we will compare the 2-digit iso code to block calls from a specific country.
      • Variable to compare: The value you would receive on the call.
        • For 2-digit iso code, enter {{Start.call.from_iso2}}
      • Operation: It has many operations. For comparing you should select “Is equal to”
      • Value to compare: The value you want to compare with.
        • For 2-digit iso code, enter the iso code for the country you want to block. Ex. US for United States of America. Find 2-digit iso code here.
    • Click Validate to save the settings. The branch component will have two output nodes:
      • No Match: When the values do not match
      • Condition 1: When the condition matches.
      Note: The condition fields are editable. You can change the name if you have more values to compare. This would make the PHLO easier to track.
    • Connect No Match to the Call forwarding component.
    • You can choose to leave the Condition 1 node empty or attach the Hangup component. The calls will be blocked in either case.

    Configure the PHLO to Your Plivo Number

    Once you have created and configured your PHLO, You can configure the PHLO to one of your Plivo numbers by following the instructions below:

    Configure the PHLO to your Plivo Number

    • On the side navigation bar, click Phone Numbers. The Phone Numbers page will appear and display your existing Phone numbers(if any). If this is your first PHLO, then the PHLO page will be empty.
    • On the Phone Numbers page, you can either select one of your existing Plivo numbers and configure the PHLO or buy a new number if you do not have one already. Refer to the Buy a Phone Number Guide for more information.
    • After selecting your Plivo number, on the Your Numbers page, under the number configuration section, you can select the Application Type as PHLO and select your PHLO from the PHLO list and click on Update Number once done.

    Test and Validate

    You can now make a call to your Plivo phone number from one of the blocked phone number(s) or country and see that the calls are rejected using PHLO. If you make a call from any other number, the call will be forwarded as specified in the PHLO.

    For more information about creating a PHLO app, see the PHLO User Guide.
    For information on components and their variables, see the PHLO Components Library.

    If you still wish to learn to screen incoming calls in the traditional API/XML way, you can refer to the instructions in the following section to begin your implementation.

    Outline

    Outbound- Call Flow

    Implementation

    In this section, we will guide you in setting up an app to screen incoming calls on your Plivo number. In this example, when an incoming call is received, we will check if that number has been blacklisted. And if it has, then we will hangup immediately using the <Hangup> XML. If the phone number hasn’t been blacklisted, then we will return a Speak XML that will say “Hello, how are you today!”.

    Prequisites

    1. Create a Plivo Account (if you don’t have one already): You can Sign up with your work email address and complete the phone verification step using your mobile number.
    2. Buy a Plivo Number: You must have a voice-enabled Plivo phone number if you are willing to receive incoming calls. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.
    3. Answer URL & Callback URLs: When a call is received on a Plivo voice-enabled number, you can control the call flow by declaring an answer URL for your Plivo application associated with that Plivo phone number. Plivo will invoke the answer URL specified and expect a valid XML response to handle the call.

      Notice how the concept of Answer URLs applies to both outbound API calls as well as incoming calls to your Plivo numbers. In the outbound API call example in the Make-Outbound-Calls use-case guide, we specified the answer URL along with the make call API request, whereas in the case of incoming calls to Plivo numbers, the answer URL is specified in the Plivo application associated with the phone number.

      In addition to requests to the answer URL, Plivo initiates HTTP requests to your application server through the course of a call based on the specific XML elements in your answer XML. Such requests can be broadly classified into two categories:

      Action URL requests: XML instructions to carry forward the call are expected in response to these requests. These requests are typically invoked at the end of an XML element’s execution. For example: when an IVR input is received from the caller during a GetInput XML execution.

      Callback URL requests: No XML instructions are expected in response to these requests. Such requests serve as webhooks to notify your application server of important events through the course of an XML element’s execution. For example: when a conference participant is muted or unmuted.

    4. Set Up Your Web Server: To be able to host Answer and Callback URLs and to be able to provide valid XMLs and accept notifications on these URLs respectively, you need to host a webserver at your end. To set up your Web Server in your preferred programming language, please refer to the instructions available in the Set Up Laravel Server for Incoming Calls & Callbacks section.

    Set Up Laravel server for Incoming Calls & Callbacks.

    In this section, we’ll walk you through how to set up a Laravel server in under five minutes and start handling incoming calls & callbacks.

    Install PHP

    Operating SystemInstructions
    OS XYou can install PHP using the official installer. You can also install it from here.
    LinuxTo install PHP on Linux you can find the instructions here.
    WindowsTo install PHP on Windows you can use the official installer.

    Install Composer

    Composer is a dependency manager for PHP that is used in all modern PHP frameworks, such as Symfony and Laravel. We highly recommend using Composer as the package manager for your web project.

    1. Download the latest version of Composer.
    2. Run the following command in Terminal in order to run the composer:

       $ php ~/Downloads/composer.phar --version
      

      Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line

    3. Run the following command to make it executable:

       $ cp ~/Downloads/composer.phar /usr/local/bin/composer
       $ sudo chmod +x /usr/local/bin/composer
       $ Make sure you move the file to bin directory.
      
    4. To check if the path has /usr/local/bin, use

       $ echo $PATH
      

      If the path is different, use the following command to update the $PATH:

       $ export PATH = $PATH:/usr/local/bin
       $ source ~/.bash_profile
      

      Note: If your PATH doesn’t include /usr/local/bin directory, we recommend adding it so that you can access it globally.

    5. You can also check the version of Composer by running the following command:

       $ composer --version.       
      

    1. Run the following command:

       $ curl -sS https://getcomposer.org/installer | php
      
    2. Run the following command to make the composer.phar file as executable:

       $ chmod +x composer.phar
      

      Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line

    3. Run the following command to make Composer globally available for all system users:

       $ mv composer.phar /usr/local/bin/composer
      

    1. Download and run the Windows Installer for Composer.

      Note: Make sure to allow Windows Installer for Composer to make changes to your php.ini file.

    2. If you have any terminal windows open, close all instances and open a fresh terminal instance.
    3. Run the Composer command.

       $ composer -V
      

    Install Laravel & Create a Laravel Project

    • Use the below command to install Laravel:

      $ composer require laravel/installer
      

    As we have Laravel and its dependencies installed, we can use them to create a new Laravel project. As the initial step, using Laravel we can auto-generate code in the Laravel folder structure.

    • Change the directory to our project directory in the command line:

      $ cd mylaravelapp
      
    • Use the below command to start your Laravel project:

      $ composer create-project laravel/laravel quickstart --prefer-dist
      
    • To install the stable release, run the following command in the project directory:

      $ composer require plivo/plivo-php
      

    This will create a quickstart directory with the necessary folders & files for development.

    Install Plivo

    • To install a specific release, run the following command in the project directory:

      $ composer require plivo/plivo-php:4.15.0
      
    • Alternatively, you can download this source and run

      $ composer install
      

    This generates the autoload files, which you can include using the following line in your PHP source code to start using the SDK.

    <?php
    require 'vendor/autoload.php'
    

    Create a Laravel Controller to Screen Incoming Calls

    Change the directory to our newly created project directory, i.e, quickstart directory and run the below command to create a Laravel controller to screen incoming calls.

    $ php artisan make:controller VoiceController
    

    This will generate a controller named VoiceController in the app/http/controllers/ directory. Now, You have to open the app/http/controllers/voiceController.php file and add the following 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
    
    <?php
    
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\RestClient;
    use Plivo\XML\Response;
    use Illuminate\Http\Request;
    
    class VoiceController extends Controller
    {
        // Speak XML to handle your first incoming call
        public function screenCall()
        {
          $from_number = $_REQUEST['From'];
          $blacklist = array('14156667777', '14156667778', '14156667779');
          $r = new Response();
          if (in_array($from_number, $blacklist)) {
             $params = array('reason' => 'rejected');
             $r->addHangup($params);
          } else {
             $body = "Hello, how are you today!";
             $r->addSpeak($body);
          }
          Header('Content-type: text/xml');
          echo $r->toXML();
        }
    }
    

    Add a Route

    Now, you need to add a route for the screenCall function in VoiceController class, open the routes/web.php file and add the below line at the end of the file:

    Route::match(['get', 'post'], '/screencall', 'VoiceController@screenCall');
    

    Now the VoiceController is ready to reject inbound calls, you can use the below command to handle your inbound calls using Laravel and Plivo PHP SDK.

    $ php artisan serve
    

    Your local development server will be started and you can test the laravel app for inbound calls via the URL http://127.0.0.1:8000/screencall/.

    Exposing your local server to the internet

    To receive incoming calls and to handle callbacks, your local server should be able to connect with the Plivo API service. Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks that can talk to the Plivo server.

    ngrok block diagram

    You can download and install ngrok from here. Follow the detailed configuration instructions to get started.

    Run ngrok on the port which currently hosts your application. For example, if your port number is 8000, run the following command:

    $ ./ngrok http 8000
    

    This will give you a UI with links that look like ngrok.io/* which you can use to access your local server using the public network.

    ngrok

    You can check the app in action on https://ff3e00858d94.ngrok.io/screencall.

    Create an Application

    1. Create an Application by visiting the Application Page and clicking on New Application. You can also use Plivo’s Application API.
    2. Give your application a name. Let’s call it Screen Call. Enter your server URL (e.g. http://example.com/screencall/) 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 Screencall 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 Screen Call (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    Assign Screen call Application

    Test and validate

    Call the Plivo phone number assigned to your app. If your phone number is on the blacklist, then your call will be rejected. If your phone number is not blacklisted, then the call will go through and you will be greated with “Hello, how are you today!”.