Forward Incoming SMS using Java

    Overview

    There may be times when you are not on your desk and may miss any important messages received on your Plivo number. In order for the SMS to be forwarded to your own number, one must set forward Incoming SMS upon that Plivo number. We’ll show you how to forward incoming SMS messages to a Plivo phone number in this guide.

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

    To forward incoming MMS, 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 MMS you send/receive, and building with PHLO is free. You can refer to the below instructions in the Using PHLO tab to begin your implementation.

    Outline

    Forward SMS

    Implementation

    In this section, we will guide you to create a PHLO to Forward Incoming MMS.

    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. Plivo Phone Number(Optional): To receive SMS, you must have a Plivo phone number that supports SMS. Numbers can be purchased from the Numbers section of your Plivo Console and use the same as the source number/from number for the outbound SMS. This number will also help you receive incoming SMS as you must have a SMS-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.

    Create the PHLO

    You can create a PHLO by referring to the below instructions to Receive MMS:

    Create a PHLO to Forward SMS

    • On the side 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.
    • Click Create New PHLO to build a new PHLO.
    • In 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 Incoming Message trigger state.
    • From the list of components on the left-hand side, drag and drop the Send Message component onto the canvas. This will add a Send Message node onto the canvas.
    • Connect the Start node with the Send Message node, using the Incoming Message trigger state.
    • Configure the Send Message node with the phone number to which you want to forward the Message. For example, in this case, “+14157778889”.
    • Once you have configured the node, click Validate to save the configurations.
    • After you complete the configurations, provide a recognizable name for your PHLO and click Save. Your PHLO is now ready.

    Assign the PHLO to a Plivo Number to Forward Incoming SMS

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

    To assign a PHLO to a number

    1. Log in 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 a phone number(s) by referring to the instructions available here.

    Test and Validate

    You can now send a sms to your Plivo phone number and see how the inbound sms is forwarded to the phone number 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.

    To forward incoming SMS, you can refer to the instructions in the following section to begin your implementation.

    Outline

    Forward SMS

    Implementation

    In this section, we will guide you in setting up an app to Forward Incoming SMS received on your Plivo number.

    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 SMS-enabled Plivo phone number if you are willing to receive incoming SMS. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Set Up a Java Spark Webapp to Forward Incoming MMS

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

    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.15.0</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 Forward Incoming MMS

    Now, create a Java class named ForwardSms 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
    
    import com.plivo.api.xml.Message;
    import com.plivo.api.xml.Response;
    import static spark.Spark.*;
    
    class ForwardSms {
        public static void main(String[] args) {
            post("/forward_sms/", (request, response) -> {
                String from_number = request.queryParams("From");
                String to_number = request.queryParams("To");
                String text = request.queryParams("Text");
                System.out.println(from_number + " " + to_number + " " + text);
                response.type("application/xml");
                Response resp = new Response()
                        .children(
                            new Message(to_number,"+1415234567",text)
                                    .callbackMethod("POST")
                                    .callbackUrl("http://foo.com/sms_status/")
                                    .type("sms")
                        );
                return resp.toXmlString();
            });
        }
    }
    

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

    Exposing your local server to the internet

    To Forward Incoming MMS 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/forward_sms/

    Create an Application

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

    Assign Phone Number to Forward SMS App

    Test and validate

    Using a normal mobile phone, send SMS to your Plivo number. Plivo will send a request to your Message URL with the parameters mentioned in the Messages Documentation and  forward the message to the specified destination.

    Common Use Cases for Forward Incoming SMS

    The majority of the use cases include an Forward incoming SMS. For example Forward messages out of office hours, Forward all incoming messages and so on.