Reputation: 2327
The following twiml used to work a few years ago for recording calls:
<?php
header("content-type: text/xml");
?>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="woman" language="en-gb">This call may be recorded for quality assurance.</Say>
<Dial record='true' action='https://URL.TLD/TWIML/record.php' method='post'>+15555555555</Dial>
</Response>
Now it returns an empty recording link. Revisiting the Twilio documentions lead me to this which says the recording may not be ready right away and to use recordingStatusCallback
<Dial record='true' action='https://URL.TLD/TWIML/do-something.php' recordingStatusCallback = 'https://the-url-thats-supposed-to-do-something-with-the-actual-recording.php' method='post'>+15555555555</Dial>
The problem I'm having is that recordingStatusCallback
doesn't say what number the call came from. I tried saving it to a $_SESSION
variable, but Twilio isn't passing the session ID when it requests the callback url.
Twilio does pass a CallSid
, which could be written to a file or database, and then subsequently pulled, matching on the Sid to the caller's phone number, but isn't there some other way to tie the caller's phone number to the actual recording being made?
Upvotes: 0
Views: 687
Reputation: 173
I have no problem with Twilio respecting session cookies. It respects them. You should be able set the session id at first connect, store variables in the session and access them at any point during the posts/requests that are associated with a particular call.
The times where I've had a problem with this are when I left out session initialization in one of my scripts and the session that was created earlier is no longer available to the current running script.
The way I handled this was to create a method that initializes a session, sends a cookie and if it is called again, will simply use the previously set session because Twilio sets PHPSESSION in either get parameters or the post body.
My routine lives in a common_functions.php script:
/**
* @return string (sessionId)
* It's not required that you consume the sessionID.
* However, this function will either set the current sessionID to
* what is on the URL parameter PHPSESSID or it will just start a
* new session and send a cookie.
*/
public function checkOrStartSession(): string
{
$sessionId = session_id();
if ($sessionId === null || $sessionId === '') {
session_start();
}
# sends a cookie to the caller so that subsequent
# calls will inform us what session to use.
# 1 hour to live
setcookie("my_session_cookie", "/", time() + 3600);
$this->logger->debug("Check Session sessionId:" . $sessionId);
return session_id();
}
Then in your called script (any script that might be called by Twilio actually:
<?php
require_once(__DIR__ . "/../common_functions.php");
$functions = new \common_functions();
/**
* capturing the session ID so it can be logged
**/
$sessionId = $functions->checkOrStartSession();
That's it. The only time I have ever run into a problem is when I've forgotten to call checkOrStartSession() at the beginning of my script. There is no session so $_SESSION is empty and trying to get data is futile.
Upvotes: 1
Reputation: 2327
The solution was slow to come to my mind. I could simply pass the caller ID in as a GET parameter in the recordingStatusCallback as follows:
<Dial record='true' action='https://URL.TLD/TWIML/do-something.php' recordingStatusCallback = 'https://the-url-thats-supposed-to-do-something-with-the-actual-recording.php?caller=<?=$_REQUEST['Caller']?>' method='post'>+15555555555</Dial>
(In mycase it's php, but it doesn't matter what language you're generatign the Twiml in.)
I didn't use to have to do this. Twilio use to respect session cookies, and at one point would send a ready recording URL to the action URL.
Upvotes: 2
Reputation: 10771
You will need to use the Calls resource along with the CallSID to obtain the callers number.
Upvotes: 1