Twilio: Best method for retrieving media uri for sent sms

344 views Asked by At

When my server sends an SMS with the MediaUrl parameter specified, I want to utilize the link location(s) that Twilio assigns to the image(s) to construct img tags with src properties based on media->uri.

Up until today, the following code was working...

$client = new Services_Twilio($twilio_sid, $twilio_token);
$params = array (
    "To" => $to,
    "From" => $from,
    "Body" => $body,
    "MediaUrl" => $media,
    "StatusCallback" => $twilio_callbackURL
);
$message = $client->account->messages->create($params);

$sid = $message->sid;
$status = $message->status;
$attachments = "";
foreach ($message->media as $media) {
    $attachment = $twilio_mediaURL . $media->uri;
    $attachments .= "<br><br><a href='" . $attachment . "' target='_blank'><img src='" . $attachment . "' target='_blank'></a>";
}

I'm not sure whether the above shown technique is no longer acceptable, or whether it only worked by fluke. For instance, perhaps the status is always returned in a timely fashion and doesn't wait for any delays in transferring the images from my server to Twilio, and so, under heavy loads, no URL's are assigned until after the status has already been delivered.

  1. Should the above shown technique work? If not...
  2. Are there any parameters provided in the callback status that would provide this information? If not...
  3. What's the best way to retrieve this information after the callback?
  4. If the URL can only be reliably retrieved after the callback, is there a way in the original creation of the message to pass a parameter that would be returned so the server would know that a MediaUrl been specified for that message? (Obviously, the server could use the SID to fetch the message parameters from its local database, but that wouldn't be as efficient.)
1

There are 1 answers

0
Alan M. On BEST ANSWER

A quick review and update of the items in the original question...

  • Although the method shown in my question follows the Twilio documentation, the results are unrealiable. In my testing, the Twilio server only provided the required links 60% of the time. Note that all of the images tested were only 5KB, so it apparently does not relate to file size.
  • As shown in the original question, I wondered whether the links would be accessible in the status callback. I don't believe they are.
  • Also as shown in the original question, I wondered whether there was anything in the status callback that would indicate that attachments were sent. I realized that I could adjust my own callback url accordingly, and then review those parameters when the callback was executed.

For completeness, here's the entire process from sending, to receiving an immediate update, to receiving a callback update, to requesting more information from Twilio's server:

// Append to the callback URL if attachments.
if (isset($_REQUEST["MediaUrl"]) ) {
    $callback .= "&Attachments=true";
}

// Prep info to send.
$params = array (
    "To" => $to,
    "From" => $from,
    "Body" => $body,
    "StatusCallback" => $callback
);

// If the upload has an attachment array, include it.
if (isset($_REQUEST["MediaUrl"]) ) {
    $params["MediaUrl"] = $_REQUEST["MediaUrl"];
}

// Send to Twilio.
$client = new Services_Twilio($twilio_sid, $twilio_token);
$message = $client->account->messages->create($params);

// Note the initial/partial SID and status.
$sid = $message->sid;
$status = $message->status;

// If there was an attachment, fetch the Twilio links.
// This is the portion that fails 40% of the time in my use.
if (isset($_REQUEST["MediaUrl"]) ) {
    foreach ($message->media as $media) {
        $attachment = $twilio_mediaURL . $media->uri;
        $body .= "<br><a href='" . $attachment . "' target='_blank'><img src='" . $attachment . "' target='_blank'></a>";
    }
}

When the message status changes, Twilio's server contacts my server based on the Callback URL...

// Immediately respond to Twilio
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response></Response>";

// Read the arguments.
// Cleanse the data since we're not yet sure the data really is coming from Twilio.
$sid = preg_replace("/\W|_/", "", $_REQUEST["MessageSid"]);
$to = preg_replace("/[^0-9+]/", "", $_REQUEST["To"]);
$status = preg_replace("/\W|_/", "", $_REQUEST["MessageStatus"]);

if (isset($_REQUEST["Attachments"]) ) {
    require_once $twilio_source;

    $client = new Services_Twilio($twilio_sid, $twilio_token);
    $body = $client->account->messages->get($sid)->body;
    foreach ($client->account->messages->get($sid)->media as $media) {
        $attachment = $twilio_mediaURL . $media->uri;
        $body .= "<br><a href='" . $attachment . "' target='_blank'><img src='" . $attachment . "' target='_blank'></a>";
    }

    updateMessage($sid, $to, $status, $body);
}
else {
    updateMessage($sid, $to, $status, null);
}

For anyone trying this, I'm not saying it's the best way; but, in absence of receiving a response from Twilio, or finding the information in their documentation, this is the best I could come up with.