Skip to main content

Documentation Index

Fetch the complete documentation index at: https://plivo.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

Overview

Plivo lets you automate voice surveys for use cases such as collecting feedback from customers and conducting polling on political issues. You can set up multiple levels of questions and walk users through different paths depending on the keys they press in response to your questions, and save the responses for analysis. You can implement voice surveys either by using our PHLO visual workflow builder or our APIs and XML documents. Follow the instructions in one of the tabs below.
Here’s how to use Plivo APIs and XML to implement voice surveys.

Create a voice survey application in Ruby

Change to the project directory and run this command to create a Rails controller.
$ rails generate controller Plivo voice
This generates a controller named plivo_controller in the app/controllers/ directory and a respective view in app/views/plivo. We can delete the view as we will not need it.
$ rm app/views/plivo/voice.html.erb
Edit app/controllers/plivo_controller.rb and add this code in the PlivoController class.
include Plivo
include Plivo::XML
include Plivo::Exceptions

class PlivoController < ApplicationController
  # Message that Plivo reads when the call recipient answers
  $question1 = "Hi, this is a call from Plivo. How would you rate your overall satisfaction with our services? Press 1 if you're satisfied or 2 to suggest improvements"
  $question2 = "How would you rate your satisfaction with our customer service? Press 1 if you're satisfied or 2 to suggest improvements"
  # Message that Plivo reads when the recipient provides negative feedback
  $negative_feedback = "We're sorry about your bad experience. One of our representatives will get in touch with you"
  # Message that Plivo reads when the caller does nothing
  $noinput_message = "Sorry, I didn't catch that. Please hang up and try again"
  # This is the message that Plivo reads when the caller inputs a wrong number.
  $wronginput_message = "Sorry, that's not a valid entry"
  def survey
    r = Response.new()

    getinput_action_url = "https://<yourdomain>.com/ivr/firstbranch/"
    params = {
        action: getinput_action_url, 
        method: 'POST', 
        digitEndTimeout: '5',
        inputType:'dtmf',
        redirect:'true'
    }
    getinput = r.addGetInput(params)
    getinput.addSpeak($question1)
    r.addSpeak($noinput_message)
    
    xml = PlivoXML.new(r)
    render xml: xml.to_xml
  end
  def firstbranch
    digit = params[:Digits]
    r = Response.new()
    
    if (digit == "1")
        getinput_action_url = "https://<yourdomain>.com/ivr/secondbranch/"
        params = {
            action: getinput_action_url, 
            method: 'POST', 
            digitEndTimeout: '5',
            inputType:'dtmf',
            redirect:'true'
        }
        getinput = r.addGetInput(params)
        getinput.addSpeak($question2)
        r.addSpeak($noinput_message)
    
    elsif (digit == "2")
        r.addSpeak($negative_feedback)
    else
        r.addSpeak($wronginput_message)
    end
    
    xml = PlivoXML.new(r)
    render xml: xml.to_xml
  end
  def secondbranch
    digit = params[:Digits]

    r = Response.new()
    
    if (digit == "1")
        body = "Thank you for participating in the survey"
        params = {
            'language'=> "en-GB"
        }
    
        r.addSpeak(body,params)
    elsif (digit == "2")
        r.addSpeak($negative_feedback)
    else
        r.addSpeak($wronginput_message)
    end
    
    xml = PlivoXML.new(r)
    render xml: xml.to_xml
  end
end
Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).
Note: We recommend that you store your credentials in the auth_id and auth_token environment variables, to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables. You can use ENV to store environment variables and fetch them when initializing the client.

Add a route

Add a route for the inbound function in the PlivoController class. Edit config/routes.rb and add these lines after the inbound route.
get 'plivo/survey'
get 'plivo/firstbranch'
get 'plivo/secondbranch'
Start the Rails server.
$ rails server
You should see your basic server application in action at http://localhost:3000/plivo/survey/.Set up ngrok to expose your local server to the internet.

Test

Make a call to a Plivo phone number and see how the survey application works.