Set up Your Ruby 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.
Install Node.js and the Plivo Node.js SDK
You must set up and install Ruby and Plivo’s Ruby SDK to make your first call. Here’s how.
|OS X & Linux||You would already have Ruby installed, you can check this by running the command ruby --version in the terminal. If you don't have it installed, you can install it using homebrew.|
|Windows||To install Ruby on Windows you can download it from here and install.|
Install Plivo Ruby Package
Create a project directory, run the following command:
$ mkdir myrubyapp
Change the directory to our project directory in the command line:
$ cd myrubyapp
Add this line to your application’s Gemfile:
gem 'plivo', '>= 4.3.0'
And then execute:
Or install it yourself as:
$ gem install plivo
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 file called
Makecall.rband paste the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 require 'rubygems' require 'plivo' include Plivo include Plivo::Exceptions api = RestClient.new("<auth_id>","<auth_token>") begin response = api.calls.create( '+14151234567', ['+14157654321'], 'http://s3.amazonaws.com/static.plivo.com/answer.xml' ) puts response rescue PlivoRESTError => e puts 'Exception: ' + e.message end
- Replace the placeholders of mandatory parameters to make outbound call using Plivo API.
- 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 ENV to store environment variables and fetch them 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 use the below command to run it.
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 Sinatra Server to Serve XML & Manage Callbacks
In this section, we’ll walk you through how to set up a Express server in under five minutes to serve XML documents & manage callbacks.
Use the following code snippet to start a local server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 require "sinatra" require 'plivo' include Plivo::XML get "/receive_call/" do response = Response.new speak_body = 'Hello, you just received your first call' response.addSpeak(speak_body) xml = PlivoXML.new(response) content_type "application/xml" return xml.to_s() end
Save this code in any file (name the file something like
receive_call.rb). To run this file on the server, go to the folder where this file resides and use the following command:
You should see your basic server app in action on http://localhost:4567/receive_call/
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.
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.
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/