Menu

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Track Delivery Status of Messages in Ruby

In this short tutorial, we'll show you how to track the delivery status of messages you send with Programmable SMS in your Ruby web application. Twilio will notify you about the status of your SMS and MMS messages via a webhook, after which you can log this information or choose to send your delivery data back to Twilio.

The code snippets in this guide are written using Ruby language version 2.0.0 or higher, and make use of the following modules:

What is a Webhook?

Webhooks are user-defined HTTP callbacks. They are usually triggered by some event, such as receiving an SMS message or an incoming phone call. When that event occurs, Twilio makes an HTTP request (usually a POST or a GET) to the URL configured for the webhook.

To handle a webhook, you'll need to build a small web application that can accept the HTTP requests.

Webhooks function the same for every Twilio application:

  1. Twilio makes an HTTP request to a URI that you provide.
  2. Your application performs whatever logic you feel necessary - read/write from a database, integrate with another API, or perform some computation.
  3. Your application then replies to Twilio with a TwiML response with the instructions you want Twilio to perform.

Receive Status Events in your Web Application

Statuses for which you can receive notifications include:

  • accepted
  • queued
  • sending
  • sent
  • failed
  • delivered
  • undelivered
  • receiving
  • received
  • read (WhatsApp only)

For a full description of these and other message statuses, see the API reference documentation.

To get Twilio to call your webhook, you need to provide a URL to your application in the status_callback parameter of each message for which you want the status callbacks.

Let's take a look an example of how you can specify this parameter. Typically, you would include a URL that points to your application, but we'll use PostBin so we can debug what Twilio sends your webhook:

        
        
        
        

        To get this code sample to run, do the following:

        1. Fill in your account_sid and auth_token (found in the Twilio Console dashboard)
        2. Replace the from phone number with one of your Twilio numbers
        3. Replace the to number with your mobile number.

        Please note: it's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before deploying to production. Check out how to set environment variables for more information.

        Next, head over to PostBin and create a new bin. Replace the status_callback parameter in the snippet with the URL of your new bin.

        When you run the code, you should receive your text message. You should also see at least one request come into your PostBin similar to this:

        PostBin Example

        This request shows the MessageStatus of sent. Assuming all was successful, you should see that followed up by another request with a status of delivered (it may take a few minutes to show up).

        Once you get the hang of how the status callback works, you're ready to handle the callback in your application.

        Here's an example of how you might do this by logging the status for each message:

              
              
              
              

              Getting Status Events for TwiML Generated Messages

              Replying to messages using TwiML? You can still track user actions via callback webhooks.

              When you reply to an incoming message with the <Message> verb, you can also provide an action attribute to specify the URL for your callback webhook. The callback can be the same as those explored in the previous sections.

              <?xml version="1.0" encoding="UTF-8"?>
              <Response>
                  <Message action="http://postb.in/b/1234abcd">This message will be tracked!</Message>
              </Response>
              

              Provide Message Delivery Feedback to Twilio

              From direct feedback from carrier partners to deliverability data from some larger customers, as well as various other monitoring techniques, Twilio has many sources of data that help optimize overall delivery deliverability. But nothing trumps the live collective data of our customers.

              Twilio's Message Feedback API resource enables you to programmatically report back to Twilio critical deliverability information. Actions that indicate a message was received can then be used by Twilio to identify network issues and improve the deliverability of your messages. By looking at both aggregate data and data specific to your account, we have a better chance at improving the delivery of your messages.

              Want to learn more? Check out this guide on how to send message delivery feedback to Twilio from your application.

              Agustin Camino David Prothero Mica Swyers Kat King David Meng Maylon Pedroso Andrew Baker
              Rate this page:

              Need some help?

              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.