Passing Variables to Outbound Call TwiML

2019-09-05 02:38发布

I am creating a page that takes in information stored as JSON. For example:

"PatientContactHeader":{  
  "PatientID":14,
  "PhoneNumber":"+1558881414",
  "ContactType":"Phone Call",
  "DateTimeOfCall":"2015-06-25: 11:00:00AM",
  "TimeZone":"EST"
 } "PatientContactDetails":[  
  {  
     "MessageID":123,
     "RecordingURL":"http://examplerecording.com",
     "MessageTitle":"Greeting"
  }
 ]
}

The initial page will take in this JSON and use it to create an outbound call. The outbound, as per Twilio's API, makes a request to a certain TwiML Url.

 function initiateCall($fromNumber, $toNumber, $url) {

try {
    // Initiate a new outbound call
    $call = $client->account->calls->create(
        $fromNumber, // The number of the phone initiating the call
        $toNumber, // The number of the phone receiving call
        $url, // The URL Twilio will request when the call is answered
        array('IfMachine' =>'Continue')
    );
    echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

}

What I want is to be able to access some of the JSON information within the TwiML instructions. More specifically, if the person being called is supposed to receive multiple messages, I want to be able to loop through the JSON data and access each message for playback. My problem is that I know of no way to pass the information from the initial page that makes the call request to the page that contains the TwiML. The logical way to solve this problem would appear to be session variables but I have read (and found) that those do not work when making outbound calls. Is there any solution to this problem?

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-05 03:24

If you already have a database with this information stored then the quickest way to handle this would be passing a piece of data that you could use to query on the database (perhaps PatientID or MessageID} in the query string of the TwiML url like this:

  $call = $client->account->calls->create(
        $fromNumber, // The number of the phone initiating the call
        $toNumber, // The number of the phone receiving call
        $url . "?PatientID=" . $patientID, // The URL Twilio will request when the call is answered
        array('IfMachine' =>'Continue')
    );

Then within your file that's serving your TwiML you could access that data like this:

$patientID = $_GET['PatientID'];
// query your database with $patientID and get the info you need

Hope that helps!

查看更多
登录 后发表回答