Save twilio recordings to database using recordingStatusCallback - php

I need to save the call recording details including the audio file to database while making a call.
This is my twiML file.
<Response>
<Dial record="record-from-ringing-dual" recordingStatusCallback="https://example.com/record_data.php" action="https://example.com/test.php" callerId="xxxxxxxxxx">
xxxxxxxxxx
</Dial>
</Response>
What should I do in record_data.php file to save the recorded details to the database?
Can anyone help me on this? Thanks in advance.

Twilio developer evangelist here.
When you receive the recordingStatusCallback webhook, Twilio will send the following parameters in the body of the request.
AccountSid The unique identifier of the Account responsible for this recording.
CallSid A unique identifier for the call associated with the recording. This will always refer to the parent leg of a two leg call.
RecordingSid The unique identifier for the recording.
RecordingUrl The URL of the recorded audio.
RecordingStatus The status of the recording. Possible values are: completed.
RecordingDuration The length of the recording, in seconds.
RecordingChannels The number of channels in the final recording file as an integer. Possible values are 1, 2.
RecordingSource The type of call that created this recording. For recordings initiated when record is set on <Dial>, DialVerb is returned.
The RecordingUrl is where you can download the recording from to store on your own server.
Let me know if that helps.

Related

Is this scenario possible with Twilio?

The users in the system records audio for one of the products that they have with us, and that audio is saved in our server and is used later in the platform for some functionalities.
Currently the audio is recorded in this way:
User calls to the phone number *1234*1# to record the audio for product 1.
User calls to the phone number *1234*2# to record the audio for product 2.
My client asked me to make it this way:
User in the dashboard and there is a button called "Record Audio".
User is prompted with a popup asking for his phone number, so the platform can call him.
User answers the call and hears something like "Hello blah blah blah... Please click [in the web page dashboard] the product you want to record audio for"
User clicks the product he wants to record audio to, so in the call, the machine says "Record the audio for product [product clicked] after the beep"...
I think I have described the key of the idea. Is that possible?
I'm using PHP.
Twilio developer evangelist here.
You absolutely can do this using Twilio. I'll give you a couple of options too, as what you describe isn't so straightforward. So we'll build up to it.
The easiest version of what you're asking to do would be to reverse the flow you describe slightly. You could have the user click the button for the product they wish to record audio for, then you ask for their phone number. Once you have that, you can use a similar system that was built in this click to call PHP tutorial to call the user back to receive their recording.
The key would be that you could include a parameter in the URL you use when you create your call that records the product ID.
$call = $client->account->calls->create(
$_ENV['TWILIO_NUMBER'], // A Twilio number in your account
$number, // The user's phone number
"http://example.com/calls?productId=" . $productId // the product the user selected
);
Then, when the call connects, Twilio will make an HTTP request to that URL, passing the product ID with it and you can handle the recording the same way you would previously.
It is, however, possible to do what you want entirely. It takes a little more work though.
Once you have received the user's phone number you can make the call to them as described with the click to call example above. You'll want to save the CallSid that the API returns as you will need to use it later.
$call = $client->account->calls->create(
$_ENV['TWILIO_NUMBER'], // A Twilio number in your account
$number, // The visitor's phone number
$url
);
$callSid = $call->sid;
The URL you supply in this call should return TwiML that speak to the user to tell them to select the item on the page. Something like:
<Response>
<Say loop="0">Please select the product on the page you wish to record audio for.</Say>
</Response>
Then, when your user presses a button, you will need to trigger another call to the REST API to redirect the call from the repeating message to TwiML that handles the recording. This is where you need the call Sid from the call you created. Your request will look a bit like this:
$call = $client->account->calls->get($callSid);
$call->update(array(
"Url" => "http://example.com/calls?productId=" . $productId
));
Check out the documentation on modifying live calls for more detail on this.
Let me know if this helps at all.
This can be done with Twilio.
When the user click on the record audio and submit his/her phone number you can make an outbound call to the user number using twilio rest api.
Once the caller accepts the call play a looping message asking him/her to click on the dashboard to record . On the dashboard click make another rest call to twilio to update the previous call to route to your audio recording flow.

Outgoing Dial Into Conference From Customer's Number

I am trying to have all my incoming calls automatically go to conference. This part can be achieved using:
<Response>
<Dial>
<Conference>Room <?php echo($_REQUEST['From']);?></Conference>
</Dial>
</Response>
This way, each caller goes into their own separate conference room. The part I can't figure out how to do is the outbound call from that point. I need to make a SIP call to my PBX using the customer's caller id so an agent can answer it. When they answer, they should be sent to the same conference room.
I found this for making outbound calls into a conference. However, I want the outbound call to the agent to use the caller id of the customer. This way, the agent can see the customer's phone number.
The main reason I want to use a conference and not a direct dial is because I want to make it possible for supervisors to call into an active call without interrupting the call between the agent and customer.
Twilio evangelist here.
I believe that when making SIP calls, the From parameter will take any alphanumeric string so you can just provide the customers caller ID as the From value:
$call = $client->account->calls->create($_REQUEST['From'], "sip:agent#example.com", "http://www.example.com/conference.xml", array());
echo $call->sid;
Hope that helps.

Is it possible for me to exchange information with Twilio APIs?

I'm trying to identify the user who is making Twilio requests from my system. Is there a way I can encrypt the information of this user, send it to Twilio along with the allowed parameters & receive the same along with Twilio's response?
EDIT: I may not have been very clear. I'm trying to make calls using Twilio's API from a web-app (PHP). I need to send the user (who is trying to establish the call)'s details & our API key to Twilio so that I can identify the user when I receive the response from Twilio.
You can include custom URL parameters in Twilio's webhook callback URL. Although the Twilio system itself will not make use of this information, it will be passed back to your php application when the call is answered. You can use this information to correlate the call to a user or other application specific concept. Here's an example:
$call = $client->account->calls->create(
'9991231234', // From a valid Twilio number
'8881231234', // Call this number
// Call this URL when a call connects and include the custom user_id parameter
'http://www.yoursite.com/handle_call.php?user_id=41423’
);
You can find more information about how the Twilio REST API and webhooks work together in the Twilio Skills training.
I doubt if it is twilio's responsibility to store additional information that you need to be otherwise handling yourself on your system. Why would twilio take operational overload from your end? Twilio probably has a confirmation token or request id for the twilio request that your system makes (returned via XML/ Json by its API server). You can take that ID and store it against the user_id inside your database (SQL / Mongo / Redis). Use that table to extrapolate information you need such as.
The number of requests each user made initiated.
The kind of requests made.
What time each request went out.
Result / Response from each request.
Hope this helps.

Twilio IVR Database Integration Two Inputs?

I have played around with database integration on Twilio, but how would I be able to ask for two inputs such as tracking number and password, and then for a status to be read if both are correct?
My main aim here is to protect our clients who need to get updates on their cases so that others cannot just randomly press a load of numbers and inadvertently get someone's update messages.
Do you have any coding example?
I've been adapting their PHP examples which are given in their How-to guides.
Thank you very much in advance for your help,
Dan
Twilio evangelist here.
So the pattern you can use in this scenario is basically something like this:
An inbound call causes Twilio to request the URL you have specified for your Voice Request URL. That URL returns TwiML.
https://www.twilio.com/docs/api/twiml
Maybe the <Say> verb telling Twilio to prompt the user for the first input (lets say tracking number) and the <Gather> verb to listen for the caller to enter tones. As part of the Gather verb you need to include the action parameter which tells Twilio what URL to request once the Gather completes.
<Response>
<Gather action="/gather-tracking-number.php">
<Say>Please enter your tracking number</Say>
<Gather>
</Response>
Once the user enters their tracking number and the Gather completes, Twilio will request the URL specified in the Gather verbs action parameter. As part of the request Twilio will pass you the digits the caller entered. Your app needs to persist those somehow. Database, session, whatever.
This URL also needs to return TwiML with the next prompt in it.
<Response>
<Gather action="/gather-passcode.php">
<Say>Please enter your customer pass code</Say>
</Gather>
</Response>
Also, as a cheap way of maintaining state across the request/responses, you can use the querystring. For example to pass the tracking number to the next page in your workflow you can throw it in the URL like this:
<Response>
<Gather action="/gather-passcode.php?trackingnumber=" . _REQUEST["Digits"]>
<Say>Please enter your customer pass code</Say>
</Gather>
</Response>
Hope that helps.

Unable to get Twilio "call-recording" url

I am developing a Twilio based application for a online campaign management system, and for each campaign there will be a twilio number and a target number. Whenever somebody will call to a twilio number (assigned for a particular campaign), it will be forwarded to the particular target number. There are also two Check-box field (one for Call-Whisper and another for Call-Recording) for each campaign.
I got a reference for Call-Whisper and Call-Recording from :
Call Screening and Recording
But unable to understand how to get the call-recording url from this tutorial.
Kindly suggest.
Twilio evangelist here.
You will need to add an action attribute to the <Dial> tag. This attribute takes a URL as a parameter, and once the call completes Twilio will make a request to that URL. That request will contain a paramater called RecordingUrl, which will have the URL of the recorded file. So configure the <Dial action=""> to some URL on your server, and then you'll get the request so you can download/store the recording.
You can see more about this in the TwiML documentation for the Dial verb.
Hope this helps!

Categories