Reply to Incoming SMS Messages Using Ruby


    This guide shows how to receive SMS text messages on a Plivo phone number — something you might want to do to acknowledge a request from a customer, handle opt-ins and opt-outs, or implement an autoresponder application.

    You can reply to SMS messages either by using our PHLO visual workflow builder or our APIs. Follow the instructions in one of the tabs below.

    To reply incoming SMS, 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 SMS 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.


    In this section, we will guide you to create a PHLO to Reply Incoming SMS.


    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 Reply Incoming SMS:

    Create a PHLO to Reply Incoming 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 HTTP Request node onto the canvas.
    • Connect the Start node with the Send Message node, using the Incoming Message trigger state.
    • Configure the Send Message node to send the details as shown in the above gif.
    • 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 for 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 receive a message to your Plivo phone number to reply an incoming SMS and see how reply incoming SMS is handled 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 reply incoming SMS, you can refer to the instructions in the following section to begin your implementation.


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


      1. Plivo account: Sign up for a Plivo account if you don’t have one already.
    1. Plivo phone number:: To receive SMS, you must have a Plivo phone number that supports SMS. You can purchase numbers from the Numbers page of the Plivo console or by using the Numbers API.

    Buy a New Plivo Number

    Set Up a Rails Server to Reply Incoming SMS

    In this section, we’ll walk you through how to set up a Rails server in under five minutes and start to Reply Incoming SMS.

    Install Rails

    Use the below command to install Rails

    $ gem install rails

    Create a Rails Project

    As we have Rails and its dependencies installed, we can use them to create a new Rails project. As the initial step, using rails we can auto-generate code in the ruby on rails folder structure. Use the below command to start your Rails project.

    $ rails new plivotest

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

    Install Plivo

    As we have the rails application created, now, let’s add Plivo-Ruby by modifying the Gemfile. Open the Gemfile in any IDE/text-editor and add the following line:

    gem 'plivo', '~> 4.16.0'

    You can use the below command to install the Plivo-Ruby gem into the bundle:

    $ bundle install

    Create a Rails Controller to Reply Incoming SMS

    Change the directory to our newly created project directory, i.e, plivotest directory and run the below command to create a rails controller to Reply Incoming SMS.

    $ rails generate controller Plivo sms

    This will generate a controller named plivo_controller in the app/controllers/ directory and a respective view will be generated in app/views/plivo directory. We can delete the View as we will not need it.

    $ rm app/views/plivo/sms.html.erb

    Now, open the app/controllers/plivo_controller.rb file and add include the following code in the PlivoController class:

    def reply
    	from_number = params[:From]
    	to_number = params[:To]
    	text = params[:Text]
    	response =
    	params = {
    		src: to_number,
    		dst: from_number,
    	message_body = "Thank you, we have received your request"
    	response.addMessage(message_body, params)
    	xml =
    	puts xml.to_xml
    	render xml: xml.to_xml

    Add a Route

    Now, to add a route for the inbound function in the PlivoController class, open the config/routes.rb file and change the line:

    get 'plivo/sms' 


    get 'plivo/reply'

    Now the plivo_controller is ready for your to reply incoming SMS, you can use the below command to handle to reply incoming sms using Rails and Plivo Ruby SDK.

    $ rails server

    Your local development server will be started and you can test the rails app for inbound calls via the URL

    Exposing your local server to the internet

    To reply incoming SMS 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.

    Note: Before starting Ngrok service, you need to add ngrok in the config.hosts list in the config/environments/development.rb file and include the below line. You will start facing Blocked host error if you fail to add this.
    # Whitelist Ngrok domain
    config.hosts << /[a-z0-9]+\.ngrok\.io/

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

    ./ngrok http 3000

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

    ngrok CLI

    You can check the app in action on

    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 Reply incoming SMS. Enter your server URL (e.g. 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 Application 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 Reply SMS (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    Assign Phone Number to Reply 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 reply the message to the specified destination.