SMS and MMS Notifications with Ruby and Rails

January 10, 2017
Written by
Reviewed by
Paul Kamp
Twilion
Hector Ortega
Contributor
Opinions expressed by Twilio contributors are their own
Kat King
Twilion
Alexis Crespo
Contributor
Opinions expressed by Twilio contributors are their own

sms-mms-ruby-rails

Ruby, Rails, and an automatic ping for all of your server administrators when something goes wrong?  We hope you're as excited to build that application as we were when we wrote this tutorial.

We'll use Ruby on Rails and demonstrate how to send SMS (or MMS) alerts to a list of system administrators if something goes wrong on your server.  Follow along as we demonstrate the key pieces of code to make it all work.

Clone our repo from Github and use the Readme to get it running in your local dev environment, then you're ready to hop in.

Let's get started!

A List of Your Server Admins... And Whomever Else as Well

In yaml, create a list of administrators who should be notified if a server error occurs.

The only essential piece of data we need is a phone_number for each administrator.

This is a migrated tutorial. Clone the code from https://github.com/TwilioDevEd/server-notifications-rails/

-
  name: Doraj
  phone_number: "+15553185056"
-
  name: Nivek
  phone_number: "+15558675309"

On deck: configuring the Twilio REST Client!

Configuring the Twilio REST Client

To send a message, we'll need to initialize the Twilio REST client which requires reading our TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN from environment variables.  That link shows you how to set environment variables inside *NIX, Mac OSX and Windows - but it may vary based on your choice of shell.  If you are on another platform, you'll need to consult the platform documentation.

You can grab those two from inside the Twilio Console:

Account Credentials

 

class NotificationsController < ApplicationController
  rescue_from StandardError do |exception|
    trigger_sms_alerts(exception)
  end

  def trigger_sms_alerts(exception)
    alert_message = <<MSG
[This is a test] ALERT!
It appears the server is having issues.
Exception: #{exception}.
Go to: http://newrelic.com for more details."
MSG
    image_url = 'http://howtodocs.s3.amazonaws.com/new-relic-monitor.png'

    admins = YAML.load_file('config/administrators.yml')
    admins.each do |admin|
      begin
        phone_number = admin['phone_number']
        send_message(phone_number, alert_message, image_url)

        flash[:success] = "Exception: #{exception}. Administrators will be notified."
      rescue
        flash[:alert] = "Something went wrong."
      end
    end

    redirect_to '/'
  end

  def index
  end

  def server_error
    raise 'A test exception'
  end

  private

  def send_message(phone_number, alert_message, image_url)
    twilio_number = ENV['TWILIO_NUMBER']
    client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']

    client.messages.create(
      from: twilio_number,
      to:   phone_number,
      body: alert_message,
      # US phone numbers can make use of an image as well
      # media_url: image_url
    )
  end
end

Next, let's take a look at how the application is set up to handle exceptions.

Handling Application Exceptions

We take advantage of the rescue_from method to rescue any exception raised in controller actions. With those exceptions under control, we can send out a notification when something goes wrong - and get our administrators on the case, quickly.

class NotificationsController < ApplicationController
  rescue_from StandardError do |exception|
    trigger_sms_alerts(exception)
  end

  def trigger_sms_alerts(exception)
    alert_message = <<MSG
[This is a test] ALERT!
It appears the server is having issues.
Exception: #{exception}.
Go to: http://newrelic.com for more details."
MSG
    image_url = 'http://howtodocs.s3.amazonaws.com/new-relic-monitor.png'

    admins = YAML.load_file('config/administrators.yml')
    admins.each do |admin|
      begin
        phone_number = admin['phone_number']
        send_message(phone_number, alert_message, image_url)

        flash[:success] = "Exception: #{exception}. Administrators will be notified."
      rescue
        flash[:alert] = "Something went wrong."
      end
    end

    redirect_to '/'
  end

  def index
  end

  def server_error
    raise 'A test exception'
  end

  private

  def send_message(phone_number, alert_message, image_url)
    twilio_number = ENV['TWILIO_NUMBER']
    client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']

    client.messages.create(
      from: twilio_number,
      to:   phone_number,
      body: alert_message,
      # US phone numbers can make use of an image as well
      # media_url: image_url
    )
  end
end

Next up, let's get creative and see how to create a custom exception alert message.

Creating a Custom Alert Message

Here we create an alert message to send out via text message.

You might also decide to include a picture with your alert message. Perhaps a screenshot of the application when the crash happened?  A funny gif?  Kid pictures?

class NotificationsController < ApplicationController
  rescue_from StandardError do |exception|
    trigger_sms_alerts(exception)
  end

  def trigger_sms_alerts(exception)
    alert_message = <<MSG
[This is a test] ALERT!
It appears the server is having issues.
Exception: #{exception}.
Go to: http://newrelic.com for more details."
MSG
    image_url = 'http://howtodocs.s3.amazonaws.com/new-relic-monitor.png'

    admins = YAML.load_file('config/administrators.yml')
    admins.each do |admin|
      begin
        phone_number = admin['phone_number']
        send_message(phone_number, alert_message, image_url)

        flash[:success] = "Exception: #{exception}. Administrators will be notified."
      rescue
        flash[:alert] = "Something went wrong."
      end
    end

    redirect_to '/'
  end

  def index
  end

  def server_error
    raise 'A test exception'
  end

  private

  def send_message(phone_number, alert_message, image_url)
    twilio_number = ENV['TWILIO_NUMBER']
    client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']

    client.messages.create(
      from: twilio_number,
      to:   phone_number,
      body: alert_message,
      # US phone numbers can make use of an image as well
      # media_url: image_url
    )
  end
end

Let's continue, and look at loading the administrators from the yaml file.

Reading the Administrators from the YAML File

Next, we read the admins from our YAML file and send alert messages to each of them with the private send_message method.

class NotificationsController < ApplicationController
  rescue_from StandardError do |exception|
    trigger_sms_alerts(exception)
  end

  def trigger_sms_alerts(exception)
    alert_message = <<MSG
[This is a test] ALERT!
It appears the server is having issues.
Exception: #{exception}.
Go to: http://newrelic.com for more details."
MSG
    image_url = 'http://howtodocs.s3.amazonaws.com/new-relic-monitor.png'

    admins = YAML.load_file('config/administrators.yml')
    admins.each do |admin|
      begin
        phone_number = admin['phone_number']
        send_message(phone_number, alert_message, image_url)

        flash[:success] = "Exception: #{exception}. Administrators will be notified."
      rescue
        flash[:alert] = "Something went wrong."
      end
    end

    redirect_to '/'
  end

  def index
  end

  def server_error
    raise 'A test exception'
  end

  private

  def send_message(phone_number, alert_message, image_url)
    twilio_number = ENV['TWILIO_NUMBER']
    client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']

    client.messages.create(
      from: twilio_number,
      to:   phone_number,
      body: alert_message,
      # US phone numbers can make use of an image as well
      # media_url: image_url
    )
  end
end

Now you've seen the wiring behind the messages, let's deep dive into how we'll send them.

Sending a Text Message

There are the three parameters needed to send an SMS using the Twilio REST API: from, to, and body.

After the message is sent, we print out the phone number we're texting.

US and Canadian phone numbers can also send an image with the message as an MMS (uncomment media_url).  The rest of the world will have image links with automatically shortened links.

class NotificationsController < ApplicationController
  rescue_from StandardError do |exception|
    trigger_sms_alerts(exception)
  end

  def trigger_sms_alerts(exception)
    alert_message = <<MSG
[This is a test] ALERT!
It appears the server is having issues.
Exception: #{exception}.
Go to: http://newrelic.com for more details."
MSG
    image_url = 'http://howtodocs.s3.amazonaws.com/new-relic-monitor.png'

    admins = YAML.load_file('config/administrators.yml')
    admins.each do |admin|
      begin
        phone_number = admin['phone_number']
        send_message(phone_number, alert_message, image_url)

        flash[:success] = "Exception: #{exception}. Administrators will be notified."
      rescue
        flash[:alert] = "Something went wrong."
      end
    end

    redirect_to '/'
  end

  def index
  end

  def server_error
    raise 'A test exception'
  end

  private

  def send_message(phone_number, alert_message, image_url)
    twilio_number = ENV['TWILIO_NUMBER']
    client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']

    client.messages.create(
      from: twilio_number,
      to:   phone_number,
      body: alert_message,
      # US phone numbers can make use of an image as well
      # media_url: image_url
    )
  end
end

And it's as simple as that!  You've now got a Ruby on Rails server application which uses the server to notify your server administrators when something goes wrong with the... server.

What else can you do with Twilio?  Let's look into it on the next pane...

Where to Next?

If you're a Ruby developer working with Twilio, you might want to check out these other tutorials.

Two-Factor Authentication

Increase the security of your login system by verifying a user using his or her mobile phone in addition to a password.

Appointment Reminders

Customers missing things scheduled months in advance?  Build an appointment reminder application with Twilio and reduce the number of no-shows.

Did this help?

Thanks for checking out this tutorial! Tweet @twilio to let us know what you think, and keep us posted on what you're going to build next!