Today we'll use Ruby and the Sinatra framework to demonstrate how to send SMS alerts to a list of system administrators when something goes wrong with your server. We'll cover all the key details in depth so you can easily incorporate this important feature into your own application.
Let's get started!
Click the button below to begin the tutorial.
To send messages we'll need to create a Twilio REST client which requires reading a
TWILIO_AUTH_TOKEN from environment variables.
The values for your account SID and Auth Token will come from the Twilio console:
Click the eyeball icon to expose your Auth Token in a form you can copy and paste.
Our Twilio REST Client is now ready. Let's take a closer look at that administrator list, shall we?
Here we create a yaml list of people who should be notified if a server error occurs.
The only essential piece of data we need is a
phone_number for each administrator.
Next, let's look at how we're going to piggyback on application exceptions.
We use Sinatra Errors Handlers and send out the notifications when an exception occurs.
Next up: sending a message to each administrator.
Notifier module, we read the administrators from our YAML file and send alert messages to each one of them with the method
Next up, we will see how to send a text message.
US and Canadian phone numbers can also send an image with the message. Other countries will have an automatically shortened URL added to the message body.
It's as simple as that!
We've just implemented an automated server notification system that can push out alerts if anything goes wrong.
Next, let's look at what other features the Twilio Ruby SDK makes simple to integrate.
We've got a lot of other excellent Ruby code we'd love to share with you... but we'll keep our links to just two:
Increase the security of your login system by verifying a user's mobile phone in addition to their password.
SMS and MMS messages are a personal way to engage with users, offering a much higher open rate than e-mail and more impact than a tweet.
Thanks for checking out this tutorial! Tweet @twilio with what you thought, what you're thinking, and most importantly... what you're going to build.