Get Started with Call Forwarding using Ruby
Call forwading can be very useful in any business work flow. For example, incoming calls can be dynamically routed to available agents, extensions, or departments that cater to the caller’s needs. In this tutorial, you will learn how to forward a received call to another phone number.
Start your implementation with Plivo to forward incoming calls 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 Call Forwarding 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.
In this section, we will guide you to create a PHLO to implement Call Forwarding use-case.
- 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.
- 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.
- 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
- On the top navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any. If this is your first PHLO, then the PHLO page will be empty.
- Click CREATE NEW PHLO to build a new PHLO.
- On 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, Endpoint Call, and API Request. For this PHLO, we will use the Incoming Call trigger state.
- From the list of components, on the left hand side, drag and drop the Call Forward component onto the canvas. This will add a Call Forward node onto the canvas.
Connect the Start node with the Call Forward node, using the Incoming Call trigger state.
- Configure the Call Forward node to select the From number using a variable. Enter all the numbers you wish to call in the To field, separated with commas.
- Once you have configured a node, click Validate to save the configurations.
After you complete the configurations, click Save. Your PHLO is now ready. Link the PHLO to a Plivo Phone Number and test it out. For more information, see Adding the PHLO to a Phone Number .
Your complete PHLO will look like this:
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:
- Login to your Plivo Console
- On the Product Navigation bar, click Phone Numbers.
- On the Numbers page, under Your Numbers, click the phone number you wish to use for the PHLO.
- In the Number Configuration window, select PHLO from the Application Type list.
- From the PHLO Name list, select the PHLO you wish to use with the phone number, and then click Update 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 inbound call is forwarded using PHLO.
To implement Call Forwarding use-case in the traditional XML way, you can refer to the instructions in the below section to begin your implementation.
In this section, we will guide you in setting up an app to receive a call on your Plivo number and forward it using Plivo XML. In this example, when an incoming call is received, Plivo will forward the call using the <Dial> XML.
- 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.
- 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.
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.
- 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 Rails Server for Incoming Calls & Callbacks section.
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.
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
Create a Rails Controller to Forward Incoming Calls
Change the directory to our newly created project directory, i.e,
plivotest directory and run the below command to create a rails controller for inbound 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, open the app/controllers/plivo_controller.rb file and add include the following code in the PlivoController class:
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:
plivo_controller is ready for your first inbound call, you can use the below command to handle your first inbound call 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/forward/.
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/plivo/forward/.
Create a Plivo Application
- Create an Application by visiting the Application Page and click on
New Applicationor by using Plivo’s Application API.
- Give your application a name. Let’s call it
Forward Call. Enter your server URL (e.g., http://myvoiceapp.com/plivo/forward/) 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.
Forward 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 forward the call. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.
Common Use Cases for Call Forwarding
Call forwarding plays a pivotal role in routing the customer calls based on agents’ availability, to series of agents, business/non-business hours based routing, etc.
- Agent Availability: Call forwarding based on agent availability is very helpful for businesses to increase availability to customers by routing the calls to an available agent as soon as the incoming call lands on their hotline.
- Business Hours based Forward: Time-based forward involves routing the calls to your office number during business hours and to your mobile phone or to an answering machine(voicemail box) during non-business hours.
- Time-zone based Forward: Time-zone based forward will help you forward your customer calls to agents from different time-zones to ensure round the clock availability.