Get started with Voicemail using Java

    Overview

    You can use Voicemail to capture the caller’s details and queries if the respective team is currently unavailable. Plivo allows you to quickly create and configure a Voicemail for your business. An incoming call will trigger the PHLO/XML and will capture the voicemail. Recording will be available on your Plivo Console and HTTP callbacks.

    Start your Voicemail implementation with Plivo 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 implement Voicemail use-case, 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

    Voicemail - Call flow

    Implementation

    In this section, we will guide you to create a PHLO to implement Voicemail use-case.

    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

    1. On the top navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
    2. Click CREATE NEW PHLO to build a new PHLO.
    3. From the CREATE A NEW PHLO window, select Build on a blank canvas, and then click CREATE PHLO. 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 Incoming SMS trigger state.
    4. From the list of components, on the left hand side, drag and drop the Record Audio component onto the canvas. This will add a Record Audio node onto the canvas.
    5. Connect the Start node with the Record Audio node, using the Incoming Call trigger state.
    6. Click the Start node to open the Configurations tab, and then configure the from and to custom variables.
      Note: To keep the PHLO dynamic, make sure you do not enter any value for the from and to variables.
    7. Click Validate to save the configurations for the node.
    8. Next, click the Record Audio node to open the Configuration tab.
    9. In the Speech text field, enter the text you’d want to play to the callers.
    10. Rename the node to Voicemail Message.
      Note: You can rename the nodes as per your requirements. We are using specific names to help you relate to the different nodes used in this use case.
    11. Click Validate to save the configurations for the node.
    12. Next, create a node for the Send SMS Component, and rename it to Send_Recording_URL.
    13. Configure the Send SMS node to select the From and To numbers from the Start node.
    14. On the Send_Recording_URL Configurations tab, enter the message in the Message field.
      Note:
      • The SMS message can be static or dynamic. In this example, we will use a combination of a static message along with some dynamic message.
      • Enter two curly brackets to view all available variables.
    15. Click Validate to save the configurations for the node.
    16. After you complete the configurations, click Save.

    You can trigger your PHLO using the PHLO URL to test it out.

    Your complete PHLO will look like this:

    Voicemail

    Assign the PHLO to a Plivo Number for Incoming Calls

    Once you have created and configured your PHLO, assign your PHLO to a Plivo number.

    To assign a PHLO to a number:

    1. Login to your Plivo Console
    2. On the Product Navigation bar, click Phone Numbers.
    3. On the Numbers page, under Your Numbers, click the phone number you wish to use for the PHLO.
    4. In the Number Configuration window, select PHLO from the Application Type list.
    5. From the PHLO Name list, select the PHLO you wish to use with the phone number, and then click Update Number.

    Assign PHLO to a Plivo Number

    If you have not purchased a phone number yet, you can buy Phone number(s) by referring to the instructions available here.

    Test and Validate

    You can now make a call to your Plivo phone number to receive an incoming call and see how the Voicemail implementation is working using 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.

    to Implement Voicemail use-case in the traditional XML way, you can refer to the instructions in the below section to begin your implementation.

    Outline

    Phone System IVR- Call Flow

    Implementation

    In this section, we will guide you in setting up an app to receive a call on your Plivo number and redirect the call to a answering machine(Voicemail box) using Plivo XML. In this example, when an incoming call is received, Plivo will redirect the caller(s) to the Voicemail Box using the <Record> XML.

    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 a Java Spark Webapp for Incoming Calls & Callbacks section.

    Set Up a Java Spark Webapp for Incoming Calls & Callbacks

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

    Install Java

    Operating SystemInstructions
    OS X & LinuxTo see if you already have Java installed, run 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 follow the instructions listed 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, Spark web app and SLF4J by adding the dependencies in pom.xml
    <dependency>
        <groupId>com.plivo</groupId>
        <artifactId>plivo-java</artifactId>
        <version>4.7.1</version>
    </dependency>
    <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 Spark Webapp to Implement Voicemail

    Now, create a Java class called Voicemail and paste 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
    
    import static spark.Spark.*;
    
    import com.plivo.api.xml.GetInput;
    import com.plivo.api.xml.Play;
    import com.plivo.api.xml.Response;
    import com.plivo.api.xml.Speak;
    
    public class Voicemail {
        public static void main(String[] args) {
            post("/voicemail/", (request, response) -> {
                response.type("application/xml");
                Response response = new Response()
                    .children(
                            new Speak("Leave message. Press star key when done"),
                            new Record("http://foo.com/get_recording/")
                                    .finishOnKey("*")
                                    .maxLength(20),
                            new Speak("Recording not received.")
                    );
                resp.children(new Speak(NoinputMessage));
                return resp.toXmlString();
            });
        }
    }
    

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

    Exposing your local server to the internet

    To receive Incoming Calls 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.

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

    ./ngrok http <port_on_which_your_local_server_is_running>
    

    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.

    Sample ngrok CLI

    In this case, the ngrok url will be something like https://37347786acf6.ngrok.io/voicemail/ and you can check the XML document using any browser.

    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 Voicemail. Enter your server URL (e.g., http://www.example.com/voicemail) in the Answer URL field and set the method as GET. 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 Voicemail

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

    Assign Application Voicemail

    Test and validate

    You can now make a call to your Plivo phone number to receive an incoming call and see how the Voicemail implementation is working using Plivo XML.