Get Started with Ruby on Rails Framework
Sign up for a Plivo Account
When you sign up with Plivo, you will start with a free trial account to experiment with and learn about our services. This free trial account comes with free credits. If you wish to continue with our service, you can add more credits and purchase a number by clicking here. Add a number and credits to your account to start testing the full range of our voice and SMS service features.
Sign up here to get your free Plivo account today.
Follow these steps to successfully sign up for a free trial account:
- Sign up with your work email address
- Check your inbox for an activation email from Plivo. Click on the link in the email to activate your account.
- Enter your mobile number to complete the phone verification step.
Sign up with your work email address
If you have any issues creating a Plivo account, please reach out to our Support Team for assistance.
Install Rails and the Plivo Ruby SDK
You must install rails, create a rails project and add Plivo’s Ruby SDK to make your first call. Here’s how.
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.
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.13.0'
You can use the below command to install the Plivo-Ruby gem into the bundle:
$ bundle install
Make your first Outbound Call
This section will guide you through how to use Plivo APIs to make voice calls from your application. First, let’s make sure you meet these prerequisites before we dive into the code.
Plivo Phone Number(Optional): You can purchase numbers from the Numbers section of your Plivo Console and use the same as the caller ID for the outbound call. This number will also help you receive incoming calls as you must have a voice-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.
Answer Url: When a call is answered by the destination_number, you can control the call flow with the help of the answer_url set in the API request. Plivo will invoke the answer_url specified as soon as the call is answered and expect a valid XML response with instructions to handle the call. For example, you can use https://s3.amazonaws.com/static.plivo.com/answer.xml as answer_url to test your first outgoing call. The XML response of this is:
<Response> <Speak>Congratulations! You've made your first outbound call!</Speak> </Response>
As you can tell, the XML above holds instructions to say ‘Congratulations! You’ve made your first outbound call!’ to the callee. You can find the entire list of Plivo XML verbs and their details in the XML Reference section of the website here.
Create a Rails Controller
Change the directory to our newly created project directory, i.e,
plivotest directory and run the below command to create a rails controller for outbound call.
$ rails generate controller Plivo voice
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/voice.html.erb
Now, You have to open the app/controllers/plivo_controller.rb file and add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 include Plivo include Plivo::Exceptions class PlivoController < ApplicationController def outbound api = RestClient.new("auth_id", "auth_token") response = api.calls.create( 'the_callerID', ['the_destination_number'], 'http://s3.amazonaws.com/static.plivo.com/answer.xml' ) puts response render json: response.to_s end end
- 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
- Replace the placeholder the_callerID with the Phone number which you have purchased and the_destination_number with the phone number you will be making calls to.
- Both the_callerID and the_destination_number should be in E.164 format, for example +15671234567.
Add a Route
Now, to add a route for the outbound function in the PlivoController class, open the config/routes.rb file and change the line:
Test & Validate
plivo_controller is ready for your first outbound call, you can use the below command to initiate your first outbound using Rails and Plivo Ruby SDK.
$ rails server
Your local development server will be started and you can test the rails app for outbound calls via the URL http://127.0.0.1:3000/plivo/outbound/.
Receive Your First Inbound Call
In addition to having at least one voice-enabled Plivo phone number that is needed to receive incoming calls (as was mentioned earlier), you need the following:
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 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.
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.
Set up a Rails Server for incoming calls & callbacks
In this section, we’ll walk you through how to set up a Rails server in under five minutes and start handling incoming calls & callbacks.
Now, open the app/controllers/plivo_controller.rb file and add include the following code in the PlivoController class after the outbound function:
1 2 3 4 5 6 7 8 def inbound response = Response.new speak_body = 'Hello, you just received your first call' response.addSpeak(speak_body) xml = Plivo::PlivoXML.new(response) render xml: xml.to_xml end
Add a Route
Now, you need to add a route for the inbound function in PlivoController class, open the config/routes.rb file and add the below line after the outbound route:
Test & Validate
plivo_controller is ready for your first inbound call, you can use the below command to receive your first inbound 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 http://127.0.0.1:3000/plivo/inbound/.
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.
You can download and install ngrok from here. Follow the detailed configuration instructions to get started.
# 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 ngrok.io/* which you can use to access your local server using the public network.
You can check the app in action on https://6ea358b0f703.ngrok.io/receivecall/ and check the XML response as shown below:
Create an Application
- Create an Application by visiting the Application Page and click on
New Application. You can also use Plivo’s Application API.
- Give your application a name. Let’s call it
Receive_call. Enter your server URL (e.g.
http://example.com/receive_call/) in the
Answer URLfield and set the method as
POST. See our Application API docs to learn how to modify your application through our APIs.
- Click on
Createto save your application.
Assign a Plivo number to your app
- Navigate to the Numbers page and select the phone number you want to use for this app.
Receive Call(name of the app) from the Plivo App dropdown list.
- Click on
Test and validate
Make a call to your Plivo number using a regular mobile phone. Plivo will send a request to your
Answer URL requesting for a valid XML response and process the call. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.
Forward an Incoming call
To forward an incoming call, you need to use Dial XML.
- You should set up the dial XML document in your server
- URL of the dial XML in your server has to be set up as the answer_url of the app assigned to the Plivo number.
- During an incoming call to your Plivo number, Plivo will send a request to the answer_url expecting a valid XML to process the incoming call.
- As the response would be a dial XML, the call will be forwarded to the number defined in the XML response.
Now, open the app/controllers/plivo_controller.rb file and add include the following code in the PlivoController class after the inbound function:
1 2 3 4 5 6 7 8 9 10 def forward response = Response.new dial = response.addDial() dest_number = "14151234567" dial.addNumber(dest_number) xml = PlivoXML.new(response) puts xml.to_xml render xml: xml.to_xml end
Add a Route
Now, you need to add a route for the inbound function in PlivoController class, open the config/routes.rb file and add the below line after the inbound route:
Test & Validate
plivo_controller is ready to forward the incoming calls to your Plivo numberl, you can use the below command to start the Rails server to forward incoming calls and to handle callbacks.
$ rails server
Your local development server will be started and you can test the rails app for outbound calls via the URL http://127.0.0.1:3000/plivo/forward/.
Make a call to your Plivo number using a regular mobile phone. Plivo will send a request to your Answer URL requesting for a valid XML response and process the call. Once Plivo receives the dial XML response, the call will be forwarded to the number defined in the XML. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.