Reputation: 35
It looks like all the guides are outdated on this matter, and twilio's website doesnt have a clear answer for this.
Im trying to get a list of all calls, and for each call record check for a recording record, if it has a recording record then get the uri for it.
Although I dont think this is the correct way of doing what im trying to do the script is very very slow, and doesnt work as expected , here is where im at right now :
// Set our AccountSid and AuthToken
$sid = 'MY_SID';
$token = 'MY_TOKEN';
// Your Account Sid and Auth Token from twilio.com/user/account
$client = new Client($sid, $token );
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls->read() as $call
) {
echo $call->sid.", ".getRecording($call->sid)."<br/>";
}
function getRecording($callsid){
// Set our AccountSid and AuthToken
$sid = 'MY_SID';
$token = 'MY_TOKEN';
$client = new Client($sid, $token);
// Loop over the list of recordings and echo a property for each one
foreach ($client->account->recordings->read( array( "CallSid" => $callsid )) as $recording ) {
return " ->".$callsid." <a href='http://api.twilio.com".$recording->uri."'>Audio</a> ";
}
}
The output is that all the recording URI are the same for each .
CAb5323eed7ed4f82b3990830777c02684, ->CAb5323eed7ed4f82b3990830777c02684 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA57df3525265949c4dfcaa9073b02880a, ->CA57df3525265949c4dfcaa9073b02880a <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA31f0ac07483d72a56d424b55672a61ab, ->CA31f0ac07483d72a56d424b55672a61ab <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAac6e6f0d45cd15069300202ce6cbc27e, ->CAac6e6f0d45cd15069300202ce6cbc27e <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAe51db5d605b94c7141d43611bc8dbbd1, ->CAe51db5d605b94c7141d43611bc8dbbd1 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAbe46fe9ab0202fc15184915b0af94d1a, ->CAbe46fe9ab0202fc15184915b0af94d1a <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA15c3eaccc8b1cfca648105744c1c1c8c, ->CA15c3eaccc8b1cfca648105744c1c1c8c <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAcb9a5d1f7e3f3b4f3b1eff08f4e51094, ->CAcb9a5d1f7e3f3b4f3b1eff08f4e51094 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAfc6a986c4e58e35778d4242303f37e32, ->CAfc6a986c4e58e35778d4242303f37e32 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA58aa5dc00c72567b91b43db52577080a, ->CA58aa5dc00c72567b91b43db52577080a <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA65dbdee33266a706f17616ecf03e78eb, ->CA65dbdee33266a706f17616ecf03e78eb <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
Im looking for a better solution, because this doesnt work and this is also running very very slowly.
Upvotes: 0
Views: 2054
Reputation: 35
This is actually a very quick and a great way. Solution provided by a Twilio employee. Thanks
$client = new Client($sid, $token);
// Create an array of recordings
$recording_array = array();
// Loop over the list of recordings and echo a property for each one
foreach ($client->recordings->read() as $recording) {
$recording_array[$recording->callSid][$count] = $recording->sid;
$count++;
}
foreach ($client->account->calls->read() as $call) {
// Check if there is a call sid exist
if(array_key_exists($call->sid, $recording_array)){
foreach($recording_array["$call->sid"] as $key=>$val){
echo $call->sid.", Recording is ".$val."\r\n";
}
} else {
echo $call->sid."\r\n";
}
}
Upvotes: 1
Reputation: 73075
Twilio developer evangelist here.
You're right looping over all calls and then looping over all recordings of the call via a REST API is going to be very slow. I recommend that you do not take this approach in order to display the recordings.
Instead, there are two things that you can do.
Firstly, write a script similar to what you already have, but instead of writing out HTML, save the calls and their recordings to a database. That way, you can look through your own database which will be much quicker than making multiple calls to an API.
Secondly, rather than keep running that script to update for new calls, you can use recordingStatusCallback
s on calls. This allows you to set a webhook URL so that when a new call's recording is complete, your application will receive an HTTP request with all the information on the recordings. Then you can save that to your database too and your application will be kept updated with all the latest recordings.
Let me know if that helps.
Upvotes: 1