System Initiated IVR Call to trap Voice Response - twilio

I need to make an IVR app in php where the syetm initiates the outbound call and traps a voice response from user.
How may i go about doing this?
This is my code (does not trap response from user)
voice.xml contents
<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/complex_gather.xml -->
<Response>
</Response>
completed.php contents
file_put_contents('voice.txt',$_SERVER['QUERY_STRING']);
Main Code
<?php
require_once 'vendor/autoload.php';
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = "xxx";
$token = "xxx";
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+1310XXXXXXX", // to
"+15676777774", // from
[
"method" => "GET",
"statusCallbackMethod" => "POST",
"url" => "http://xxxx.com/voice.xml"
]
);
$response = new VoiceResponse();
$gather = $response->gather(['action' => '/completed.php','method' => 'GET', 'input'=>'speech','timeout'=>3,''=>'true','speech_model'=>'phone']);
$gather->say('Enter something, or not');
echo $response;

Twilio developer evangelist here.
When you create a call through the Twilio API, as you are doing, you pass a URL parameter.
When the call connects, Twilio will make a webhook (HTTP) request to that URL and will follow the instructions that are returned in the form of TwiML.
Currently your /voice.xml endpoint returns an empty <Response/> which will cause the call to hang up as there is no TwiML to execute.
Instead of returning an empty <Response/> from /voice.xml you should return the TwiML you generate at the end of your script.
So, perhaps voice.xml should be voice.php and look a bit like this:
<?php
require_once 'vendor/autoload.php';
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;
header('Content-Type: application/xml');
$response = new VoiceResponse();
$gather = $response->gather(['action' => '/completed.php','method' => 'GET', 'input'=>'speech','timeout'=>3,''=>'true','speech_model'=>'phone']);
$gather->say('Enter something, or not');
echo $response;
You should also return TwiML from your completed.php endpoint too.
<?php
file_put_contents('voice.txt',$_SERVER['QUERY_STRING']);
header('Content-Type: application/xml');
?>
<Response/>

Related

How to switch an ongoing Twilio phone call to a <Pay> connector

I would like to use the < Pay> connector optionally while in an ongoing phone call. I cannot find out how to do trigger a new resource during an ongoing phone call.
You can modify an "in progress" call by passing new TwiML (XML) to execute which could contain your "<Pay>".
You must provide
the ID of the call you want to modify (the "CallSid" "CAe1644a7eed5088b159577c5802d8be38")
and an URL where Twilio will find the instructions (the "Url" "http://demo.twilio.com/docs/voice.xml")
I don't know what language you're using but in PHP with Twilio's library the code would look someting like this:
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
$sid = "ACc0966dd96e4d55d26ae72df4d6dc3494";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$call = $twilio->calls("CAe1644a7eed5088b159577c5802d8be38")
->update(array(
"method" => "POST",
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
print($call->to);
You can read more about this here
(https://www.twilio.com/docs/voice/tutorials/how-to-modify-calls-in-progress).

Record an outbound call in twillio

I am trying to record an outbound call using twillio php code :
Once i execute the code in browser, call will go to destination number [+919999999999].
Issue :
but once it recieved, some default voice will play and call will be disconnected....
Requirement :
But i want both pepoles [ source & destination ] should speak and want to record that conversation....
<?php
require_once '/var/www/html/ecom1/vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "account_sid";
$token = "auth_token";
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+919999999999",
"+918888888888",
array(
"record" => True,
"url" => "http://twimlets.com/forward?PhoneNumber=%2B918888888888&"
)
);
print($call->sid);
?>
I am using Trail account....
Append the parameter "Record=true" when making a POST request to tell Twilio to record an outgoing call via the REST API.
By default, the recording will be single-channel (mono). For dual-channel recordings (two legs of the call each in separate stereo channels), append the parameter "RecordingChannels=dual".
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$account_sid = '<account_sid value>';
$auth_token = '<auth_token value>';
$client = new Client($account_sid, $auth_token);
$calls = $client->accounts("<account id>")
->calls->create("<Valid To number>", "<Valid From number>", array(
'Method' => "POST",
'Record' => "true",
'RecordingChannels' => "dual"
));
Please Note :
When attempt to initiate an outbound phone call, ensure the URL you specified to handle the call should be valid URL.
If you specified an Application Sid for your outbound phone call, the application must have a valid VoiceUrl or the call will fail.
Hope this information helps you !

Twillo,making call with REST API,call gets disconnect after receiving

In outgoing call,when make call using REST API then call gets disconnected after reading the twiml on the my_url that we pass when creating call.
I want to know what TWIML or what i do on that my_url to keep the call go as normal call(means talk to the person who I have called).
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);
$call = $client->calls->create(
"+14155551212", "+14158675309",
array("url" => "my_url")
);
echo $call->sid;
The twiml is
<Response>
<Say>Hello </Say>
</Response>
I want to connect the same caller.Like if agent A dail a user number then I want to connect the agent A with user.and I don't want to dail another number to connect with user

Twilio REST API How to get Sid

I am still new to Twilio. I am writing a PHP script which will connect to Twilio and parse some information about calls. I am using the code from this page:
https://www.twilio.com/docs/api/rest/call
Here is my code:
require_once '../twilio-library/Services/Twilio.php';
// Twilio REST API version
$version = '2010-04-01';
// Set our AccountSid and AuthToken
$sid = 'abc132xxxxx';
$token = 'xxxxeeffv';
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls as $call) {
echo "->".$call->Sid."<- <br/>";
}
The browser just outputs many blanks. No Sid values. What am I doing wrong?
Twilio Developer Evangelist here.
Try doing the following to get call information.
<?php
require_once '../twilio-library/Services/Twilio.php';
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'abc132xxxxx';
$token = 'xxxxeeffv';
$client = new Services_Twilio($sid, $token);
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls as $call) {
echo $call->sid;
}
Notice that if you're using the latest version of the library, you don't need to specify a version on your request. Double check that the path ../twilio-library/Services/Twilio.php is correct for you though.
If you're only testing, you could move the file Twilio.php to the same directory where your code is and just import Twilio.php on it.
If you then decide to filter the logs by date, here's how you would do it.
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls->getIterator(0, 50, array(
"Status" => "completed",
"StartTime>" => "2015-03-01",
"StartTime<" => "2015-05-10"
)) as $call
) {
echo $call->sid;
}
Let me know how it goes.

Forwarding live Calls to a new Twiml from the browser

I am following the tutorial on https://www.twilio.com/docs/api/rest/change-call-state#post I am coding in php the portion that allows you to forward a current inbound call to a new Twiml URL. How do I get the inbound call Sid? Currently, the call Sid that I am retrieving forwards my browser to the new Twiml URL and hangs up the inbound caller. I think that I may have the wrong call Sid since I want to forward the current inbound caller to the new Twiml URL. Not The Browser. Can someone please give me some advice on retrieving the inbound call Sid to use in this php script? Thanks
Twilio.Device.incoming(function (conn) {
callSid = conn.parameters.CallSid;
$("#log").text("Incoming connection from " + conn.parameters.From);
// accept the incoming connection and start two-way audio
conn.accept();
});
This is how I am getting the Call Sid. If I input this Call Sid into
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('TwilioAPI/twilio-php-master/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = '';
$token = '';
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get("THE CALL SID I GOT FROM THE JS GOES HERE");
$call->update(array(
"Url" => "http://twimlets.com/message?Message%5B0%5D=I%20finally%20did%20it&",
"Method" => "POST"
));
echo $call->to;
?>
This code forwards the browser which receives the call to the new Twiml URL. Not the inbound caller.

Resources