Twilio unable to record incoming call conversation (not related to leave a message) - twilio

In the incoming call back url
I have the next code:
Twilio Call id
if (isset($_REQUEST['CallSid'])){
$tw_call_id = $_REQUEST['CallSid'];
}
else {
$tw_call_id = '';<br />
}<br />
$sid = "Axxxx";
$token = "Txxxx";
$twilio = new Client($sid, $token);
$recording = $twilio->calls($tw_call_id)
->recordings
->create([
"recordingStatusCallback" => "https://mycode.php",
"recordingStatusCallbackEvent" => ["completed"],
"recordingChannels" => "dual"
]
);
After this I have a say() and a dial() , so I want to record the say and the dial , no only the dial call
I got the error
PHP Fatal error: Uncaught Twilio\Exceptions\RestException: [HTTP 400] Unable to create record: Requested resource is not eligible for recording in /var/www/html/vendor/twilio/sdk/src/Twilio/Version.php
What I am looking for is to record the complete conversation from incoming call

Related

Not recieving sms using twilio notify service, no debug error in console?

I am trying to send bulk SMS using Twilio notify API. I had looked at the documentation, and other StackOverflow resources but did not find the issue yet. What I am doing is:
$sid = "AC1e590cbb8eee064c3c71axxxxxxxxxxx";
$token = "94c2dc3e2e407c4ebd28cxxxxxxxxxxx";
$twilio = new Client($sid, $token);
$serviceSid = "IS32913ae9b083b809b1c06xxxxxxxxxxx";
$recipients = array();
foreach($phone_nos as $phone_no) {
array_push($recipients, $phone_no['phone_no']);
}
//recipients array print value is
//Array
//(
//[0] => +923105653361
//[1] => +923491457062
//)
$binding = array();
foreach ($recipients as $recipient) {
$binding[] = '{"binding_type":"sms", "address":"'.$recipient.'"}';
}
//binding array is print value is
//Array
//(
//[0] => {"binding_type":"sms", "address":"+923105653361"}
//[1] => {"binding_type":"sms", "address":"+923491457062"}
//)
$service = $twilio->notify->v1->services->create();
$notification = $twilio->notify->services($serviceSid)
->notifications->create([
"toBinding" => $binding,
"body" => 'Test message 5 notify'
]);
echo $notification->body;
echo '<pre>';print_r($notification->sid);exit;
The notify console is showing the messages are sent with no error.
Twilio developer evangelist here.
It looks like you haven’t connected a messaging service and a phone number to your Notify service. Follow the instructions in the documentation here to set that up, then try sending the messages again.

Twilio attach statusCallbackEvent and url on an incoming call

I am trying to forward a call to user phone if his twilio phone is called.
$usr_ph = 'xxxxxxxxxx'; // get user personal phone from database
$twiml = new Twiml();
$twiml->dial($usr_ph);
return $twiml;
The above code works fine , I need to get the call minutes. So I added status callback url to dial . But its not sending me the any status event
Below is the code I have
$data = array ('statusCallbackEvent' => array('completed'),
statusCallback' => "https://mywebsite.com/get_Call_outcome",
'statusCallbackMethod' => 'POST'
);
$twiml = new Twiml();
$twiml->dial($usr_ph, json_encode($data));
return $twiml;
I did tried :
$twiml = new Twiml();
$twiml->dial($usr_ph, [
'statusCallbackEvent' => array('completed'),
'statusCallback' => "https://mywebsite.com/get_Call_outcome",
'statusCallbackMethod' => 'POST'
]);
return $twiml;
But then I get the error message as
SimpleXMLElement::addAttribute() expects parameter 2 to be string, array given
Not sure , but I think I need to add statusCallbackEvent on the original call which was made on the twilio phone.
Edit:
Now I tried updating the incoming call by adding statusCallbackEvent, but its still not sending any Event.
$twi = new Client($twilio->sid, $twilio->token);
$call = $twi->calls($request->input('CallSid'))
->update(array ('statusCallbackEvent' => array('completed'),
'statusCallback' =>"www.mywebsite.com/get_Call_outcome",
'statusCallbackMethod' => 'POST'
));
The statusCallback is part of the Number noun, not Dial verb. You can find more details below. See if that addresses the issue.
Twilio Voice:
https://www.twilio.com/docs/voice/twiml/number
Try this Set Status Codes on Number Noune
$dial->number('+14158675310', [
'statusCallbackEvent' => 'initiated ringing answered completed',
'statusCallback' => 'https://myapp.com/calls/events',
'statusCallbackMethod' => 'POST'
]);

How to handle my sms statuscallback in twilio using php laravel?

I saw an example in twilio: https://www.twilio.com/docs/sms/tutorials/how-to-confirm-delivery-php
<?php
$sid = $_REQUEST['MessageSid'];
$status = $_REQUEST['MessageStatus'];
openlog("myMessageLog", LOG_PID | LOG_PERROR, LOG_USER);
syslog(LOG_INFO, "SID: $sid, Status: $status");
closelog();
I don't know what the code above exactly do, but what I want is to save the data to my local database.
The code in my post method(my statuscallback):
public function smsStatusCallback(Request $request){
$sms = SmsChannel::create([
'number' => $request['MessageSid'],
'body' => $request['MessageStatus'],
]);
}
I've found a solution already. I saw the possible solutions in twilio debugger: "Double check that your TwiML URL does not ...". So I tried making it as a twiml
public function smsStatusCallback(Request $request){
$response = new Twiml();
$sms = SmsChannel::create([
'sid' => $request['MessageSid'],
'status' => $request['MessageStatus'],
]);
return response($response)
->header('Content-Type', 'text/xml');
}
I've added my route to api.php since the URL should be accessible by twilio.
Route::post('sms-status-callback','CommunicationController#smsStatusCallback');

Redirecting a Call to a new Twiml URL

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. I am finding that in order for this to work, I have to specify a To and From parameter in the update array. I need the call forwarded to the URL specified not the number specified in the To parameter. However, the Twilio API throws an error that says the To parameter is required but the docs indicate that it is not. Is there something I am doing wrong here?
<?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 = 'XXXXXXX';
$token = 'XXXXXXX';
$callSid = $_POST['CallSid'];
$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($callSid);
$call->update(array(
"Url" => "http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",
"Method" => "POST"
));?>
forwardToBob.xml contains a Response with a Say Verb.
This php code (not the twiml) throws the error
Uncaught exception 'Services_Twilio_RestException' with message 'No 'To' number is specified' in /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php:297
If I add
"To" => "Some Ten Digit Phone Number",
"From" => "Some Ten Digit Phone Number",
to the array, the error is not thrown. The call is then directed to the "To" phone number. If the phone number specified in the "To" parameter answers, then the call is connected AND the twiml at forwardToBob.xml executes all at the same time.
EDIT # 3----------------------------------------------------------------------
Here is my entire code....
This is the Twiml that is executed everytime the Twilio verified number is called. I got this code from the Twilio quickstart site.
<?php
header('Content-type: text/xml');
$callerId = "+19012311158";
// put your default Twilio Client name here, for when a phone number isn't given
$number = "Bob";
// get the phone number from the page request parameters, if given
if (isset($_REQUEST['PhoneNumber'])) {
$number = htmlspecialchars($_REQUEST['PhoneNumber']);
}
// wrap the phone number or client name in the appropriate TwiML verb
// by checking if the number given has only digits and format symbols
if (preg_match("/^[\d\+\-\(\) ]+$/", $number)) {
$numberOrClient = "<Number>" . $number . "</Number>";
} else {
$numberOrClient = "<Client>" . $number . "</Client>";
}
?>
<Response>
<Dial callerId="<?php echo $callerId ?>">
<?php echo $numberOrClient ?>
</Dial>
</Response>
This is the client browser which I mostly copied from the Twilio quickstart site.
<?php
include 'TwilioAPI/twilio-php-master/Services/Twilio/Capability.php';
// put your Twilio API credentials here
$accountSid = 'XXXXXXXX';
$authToken = 'XXXXXXXX';
// put your Twilio Application Sid here
$appSid = 'XXXXXXXXXXXXX';
// put your default Twilio Client name here
$clientName = 'Bob';
// get the Twilio Client name from the page request parameters, if given
if (isset($_REQUEST['client'])) {
$clientName = $_REQUEST['client'];
}
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing($appSid);
$capability->allowClientIncoming($clientName);
$token = $capability->generateToken();
?>
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script type="text/javascript"
src="//static.twilio.com/libs/twiliojs/1.2/twilio.min.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<link href="http://static0.twilio.com/bundles/quickstart/client.css"
type="text/css" rel="stylesheet" />
<script type="text/javascript">
var callSid = "";
Twilio.Device.setup("<?php echo $token; ?>");
Twilio.Device.ready(function (device) {
$("#log").text("Client '<?php echo $clientName ?>' is ready");
});
Twilio.Device.error(function (error) {
$("#log").text("Error: " + error.message);
});
Twilio.Device.connect(function (conn) {
callSid = conn.parameters.CallSid;
$("#log").text("Successfully established call");
});
Twilio.Device.disconnect(function (conn) {
$("#log").text("Call ended");
});
Twilio.Device.incoming(function (conn) {
$("#log").text("Incoming connection from " + conn.parameters.From);
// accept the incoming connection and start two-way audio
conn.accept();
});
Twilio.Device.presence(function (pres) {
if (pres.available) {
// create an item for the client that became available
$("<li>", {id: pres.from, text: pres.from}).click(function () {
$("#number").val(pres.from);
call();
}).prependTo("#people");
}
else {
$("#" + pres.from).remove();
}
});
function call() {
// get the phone number or client to connect the call to
params = {"PhoneNumber": $("#number").val()};
Twilio.Device.connect(params);
}
function forward() {
var xmlhttp = new XMLHttpRequest();
params = "?CallSid=" + callSid + "&ForwardTo=" + document.getElementById("number").value;
xmlhttp.open("POST","forward.php" + params,false);
xmlhttp.send();
document.getElementById("log").innerHTML=xmlhttp.responseText;
}
function hangup() {
Twilio.Device.disconnectAll();
}
</script>
</head>
<body>
<button class="call" onclick="call();">
Call
</button>
<button class="hangup" onclick="hangup();">
Hangup
</button>
<input type="text" id="number" name="number"
placeholder="Enter a phone number or client to call"/>
<button class="call" onclick="forward();">
Forward
</button>
<div id="log">Loading pigeons...</div>
<ul id="people"/>
</body>
</html>
This is the forwarding code that is called via an HTTP POST request from my forward() function.
<?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 = 'XXXXXX';
$token = 'XXXXXX';
$callSid = $_POST['CallSid'];
$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($callSid);
$call->update(array(
"Url" => "http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",
"Method" => "POST"
));
The first code snippet shows the Twiml that is executed when 901-231-1158 is called. It is then directed to the Client "Bob". Once the connection is successful, I press the forward button which I added in. This forward button calls the forward function which then makes an HTTP POST request to the PHP script which is the last snippet. Upon executing, if I do not have the "To" and "From" parameters specified in the array for the update function, I receive an error. Any suggestions on what I should try to fix this would help very much!
PLEASE NOTE: The button that I created in HTML is called Forward and it calls the function forward() which I created. I am getting the call Sid and saving it in an instance variable. I retrieve this value in the Twilio.Device.Connect function.
The full stack trace.
Stack trace:
#0 /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php(180): Base_Services_Twilio->_processResponse(Array)
#1 /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio/InstanceResource.php(31): Base_Services_Twilio->createData('/2010-04-01/Acc...', Array)
#2 /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/forward.php(22): Services_Twilio_InstanceResource->update(Array)
#3 {main}
thrown in /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php on line 297
[12-Jun-2015 00:19:39 UTC] PHP Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'No 'To' number is specified' in /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php:297
Twilio developer evangelist here.
Thanks for all the details, I managed to put together most of what you were trying to do in order to find the errors. I never received the error message you had though, so bare that in mind.
I found I had to change the following parts of your code to get this to work.
In your XHR request, stop trying to make the request synchronously (I was using Firefox and it was deprecated), just use:
xmlhttp.open("POST","forward.php" + params);
I was also not getting the call sid from the XHR request in my PHP, so I changed $_POST to $_REQUEST and that started to work.
Finally, the call sid on the client side is not the same as the originating call sid. That is the parent call and you can get hold of it like this using the twilio-php helper library:
$callSid = $_REQUEST['CallSid'];
$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($callSid);
$parentCall = $client->account->calls->get($call->parent_call_sid);
$parentCall->update(array(
"Url" => "http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",
"Method" => "POST"
));
You then need to update the parent call to forward it on to your original URL and hang up on the client side.
I hope this helps!

How to capture when a caller joins a Twilio conference?

Our app brings together several participants into a conference. The Host, uses twilio client, while participants use either phone lines or twilio clients.
The host need to know when each participant joins the conference.
is there a way via the RESTful API to get in real-time who joined the conference?
Here's how to get the participants' numbers:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXX";
$token = "YYYYY";
$client = new Services_Twilio($sid, $token);
$response = new Services_Twilio_Twiml();
if( isset($_REQUEST['ConferenceSid']) ){
$participants = $client->account->conferences->get( $_REQUEST['ConferenceSid'] )->participants;
$cnt = count( $participants );
$response->Say( "There are ".$cnt." callers in this conference" );
foreach ($participants as $participant) {
$call = $client->account->calls->get( $participant->callsid );
$response->Say( $call->from );
}
}
$response->Redirect("conferencemod.xml");
print $response;
?>
Taken from: https://www.twilio.com/blog/2014/09/roll-call-roger-stringer-shows-you-how-to-take-a-headcount-during-a-twilio-conference-call.html
You can probably modify it to return $call->StartTime additionally. That would let the moderator know who called and when their call started. I hope that helps.

Resources