Here’s how to use Plivo APIs and XML to implement voice surveys.How it works
Plivo requests an answer URL when the call is answered (step 4) and expects the file at that address to hold a valid XML response from the application with instructions on how to handle the call.Prerequisites
To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. You must have a voice-enabled Plivo phone number to receive incoming calls; you can rent numbers from the Numbers page of the Plivo console, or by using the Numbers API. If this is your first time using Plivo APIs, follow our instructions to set up a PHP development environment and a web server and safely expose that server to the internet.Create a voice survey application in PHP
Change to the project directory and run this command to create a Laravel controller.$ php artisan make:controller SurveyController
This generates a controller named SurveyController in the app/http/controllers/ directory. Edit app/http/controllers/SurveyController.php and add this code.<?php
namespace App\Http\Controllers;
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Plivo\XML\Response;
use Illuminate\Http\Request;
class SurveyController extends Controller
{
// GetInput XML to handle the incoming call
public function ivrMain()
{
// 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";
// Message that Plivo reads when the recipient provides negative feedback
$NegativeFeedback = "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
$NoinputMessage = "Sorry, I didn't catch that. Please hang up and try again";
// Message that Plivo reads when the caller enters an invalid number
$WronginputMessage = "Sorry, that's not a valid entry";
$r = new Response();
$getinput_action_url = "https://<yourdomain>.com/firstbranch.php";
$get_input = $r->addGetInput([
'action' => $getinput_action_url,
'method' => "POST",
'digitEndTimeout' => "5",
'inputType' => "dtmf",
'redirect' => "true",
]);
$get_input->addSpeak($Question1);
$r->addSpeak($NoinputMessage);
Header('Content-type: text/xml');
echo $r->toXML();
}
// Action URL block for DTMF
public function firstBranch(Request $request)
{
$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
$NegativeFeedback = "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
$NoinputMessage = "Sorry, I didn't catch that. Please hang up and try again";
// Message that Plivo reads when the caller enters an invalid number
$WronginputMessage = "Sorry, that's not a valid entry";
$r = new Response();
$digit = $_REQUEST['Digits'];
if ($digit == '1'){
$getinput_action_url = "https://<yourdomain>.com/secondbranch.php";
$get_input = $r->addGetInput([
'action' => $getinput_action_url,
'method' => "POST",
'digitEndTimeout' => "5",
'inputType' => "dtmf",
'redirect' => "true",
]);
$get_input->addSpeak($Question2);
$r->addSpeak($NoinputMessage);
}
else if ($digit == '2'){
$r->addSpeak($NegativeFeedback);
}
else {
$r->addSpeak($WronginputMessage);
}
Header('Content-type: text/xml');
echo $r->toXML();
}
// Action URL block for Sales and Support branch
public function secondBranch(Request $request)
{
// Message that Plivo reads when the recipient provides negative feedback
$NegativeFeedback = "We're sorry about your bad experience, One of our representatives will get in touch with you";
// Message that Plivo reads when the caller enters a wrong number
$WronginputMessage = "Sorry, that's not a valid entry";
$r = new Response();
$digit = $_REQUEST['Digits'];
if ($digit == '1'){
$body = "Thank you for participating in the survey";
$params = array(
'language' => "en-GB"
);
$r->addSpeak($body,$params);
}
else if ($digit == '2'){
$r->addSpeak($NegativeFeedback);
}
else {
$r->addSpeak($WronginputMessage);
}
Header('Content-type: text/xml');
echo $r->toXML();
}
}
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 them from the environment variables. You can use $_ENV or putenv/getenv functions to store environment variables and fetch them when initializing the client.
Add a route
Add a route for the forward function in the SurveyController class. Edit routes/web.php and add these lines.Route::match(['get', 'post'], '/survey', 'SurveyController@ivrMain');
Route::match(['get', 'post'], '/firstbranch', 'SurveyController@firstBranch');
Route::match(['get', 'post'], '/secondbranch', 'SurveyController@secondBranch');
Start the Laravel server.You should see your basic server application in acation at http://localhost:8000/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.