Set up Your Java Dev Environment for Voice Calls

    In this guide, you will learn how to set up a development environment in under 5 minutes to trigger API requests and to start serving valid Plivo XML to control your call flow. Also, Using a tunnelling software like ngrok, we will see how you can expose your local dev server with a public address to test out your integration with Plivo.

    Prerequisites

    Install Java and the Plivo Java SDK

    You must install Java(Java 1.8 or higher) and Plivo’s Java SDK to make your first call. Here’s how.

    Install Java

    Operating SystemInstructions
    OS X & LinuxYou would already have Java installed, you can check this by running the command java -version in the terminal. If you do not have it installed, you can install it from here.
    WindowsTo install Java on Windows you can follow the instructions from here.

    Install Plivo Java Package using IntelliJ Idea

    • Create a new project in IntelliJ Idea

    Create New Project

    • Choose a dependency management and Java SE SDK for the new project

    Choose Dependency management

    • Install the Plivo Java package by adding the dependency in pom.xml
    <dependency>
        <groupId>com.plivo</groupId>
        <artifactId>plivo-java</artifactId>
        <version>4.7.1</version>
    </dependency>
    

    Install Plivo package

    Trigger an API Request

    To trigger any Plivo API, you can create a file in the project directory and execute the code. Please check below example to make an outbound call:

    • Now, create a Java class in the project called MakeCall and paste the below code.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    import java.io.IOException;
    import java.util.Collections;
    import com.plivo.api.Plivo;
    import com.plivo.api.exceptions.PlivoRestException;
    import com.plivo.api.models.call.Call;
    import com.plivo.api.models.call.CallCreateResponse;
    
    class MakeCall {
        public static void main(String [] args) throws IOException, PlivoRestException {
            Plivo.init("auth_id","auth_token");
            CallCreateResponse response = Call.creator("+14151234567",
                    Collections.singletonList("+14157654321"),
                    "http://s3.amazonaws.com/static.plivo.com/answer.xml")
                    .answerMethod("GET")
                    .create();
            System.out.println(response);
        }
    }
    
    • Replace the placeholders of mandatory parameters to make outbound call using Plivo API.
    Note:
    • Replace the placeholders auth_id & auth_token with your credentials from Plivo Console.
    • We recommend that you store your credentials in the auth_id & auth_token environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables
    • You can use System.getenv() to store and retrieve environment variables while initializing the client.
    • Replace the placeholder +14151234567 with the Phone number which you have purchased and +14157654321 with the phone number you will be making calls to.
    • Both +14151234567 and +14157654321 should be in E.164 format
    • Save the file and run it.

    Make Call

    Note: You can follow the same approach to trigger any other API requests. Please refer to the detailed API reference to check all the API requests available on the Voice API platform.

    Serve an XML Document and Manage Callbacks

    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. We will see how to create and set up a new Plivo Application through the Plivo console in the following section.

    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 above, 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.

    Set up a Spark Webapp to Serve XML & Manage Callbacks

    In this section, we’ll walk you through how to set up a Spark server in under five minutes to serve XML documents & manage callbacks.

    • Add Spark web app and SLF4J dependecy:
    <dependency>
        <groupId>com.sparkjava</groupId>
        <artifactId>spark-core</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
    </dependency>
    

    Create a Spark app

    Create a Java class named ReceiveCall and paste the following code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    import static spark.Spark.*;
    import com.plivo.api.xml.Speak;
    import com.plivo.api.xml.Response;
    
    public class ReceiveCall {
        public static void main(String[] args) {
            get("/receive_call/", (request, response) -> {
                response.type("application/xml");
                return new Response()
                        .children(new Speak("Hello, you just received your first call")).toXmlString();
            });
        }
    }
    

    Receive Call

    Run the project and you should see your basic server app in action on http://localhost:4567/receive_call/

    Ngrok Setup

    To sever XML documents and to handle callbacks, your local server should be able to connect with 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 which can talk to Plivo server.

    ngrok block diagram

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

    Use Ngrok to Expose Your Local Server to the Internet

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

    ./ngrok http 4567
    

    This will start the ngrok server on your local server. Refer to the below screenshot.

    Sample ngrok CLI

    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. And you should see your basic server app in action on http://<nrgok_URL>/receive_call/

    Sample ngrok CLI

    Note: You can follow the same approach to serve any other XML documents to manage various call flows. Please refer to the detailed XML reference to check all the XML elements available on the Voice API platform.