Re-send a Twilio SMS Message

1.2k views Asked by At

I store sent messages in a log table with their unique SIDs. With a periodic console task I iterate over the records having status = undelivered and request the status of it and if it's still undelivered, I'd like to re-send that very message. I don't want a new one as the message contains a verification code and we store only hash of it. Is it possible to re-send the old message having its SID?

1

There are 1 answers

1
Sam Agnew On BEST ANSWER

Twilio Developer Evangelist here,

There is not a way to automatically resend an undelivered message using the API. You can work around this by grabbing the messages by SID and sending the undelivered ones again to the same number with the same body. When you use the REST API to get a message by SID, you have access to all of the data from that message, including the exact message body.

A quick example using the Twilio PHP Library would look like this:

<?php
$client = new Services_Twilio($AccountSid, $AuthToken);

$messageSIDs = array("Insert", "Array of", "Message SIDs", "here");

foreach ($messageSIDs as $sid) {

    $msg = $client->account->messages->get($sid);

    if ($msg->status == "undelivered") {
        echo "Resending undelivered message for SID: $sid\n";
        $sms = $client->account->messages->sendMessage(

            // The number we are sending from.
            $msg->from,

            // The number we are sending to.
            $msg->to,

            // The sms body.
            $msg->body
        );
    }
}

You can see all of the data that the REST API gives you about messages here