Twilio How To Receive SMS Messages with Ruby on Rails Using the Twilio Gem

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Assuming you have a Twilio account and API credentials, add the following to your Gemfile:

gem 'twilio-ruby'

Alternatively you can gem install twilio-ruby.

To have Twilio send an incoming SMS to a particular route in your application, you need to configure the Messaging URL for your phone number. Once that's done, you need to set up a route in config/routes.rb:

TwilioSample::Application.routes.draw do
  resources :messages
end

This creates a set RESTful routes (GET /messages to list messages, POST /messages to create a message, etc.) that will send requests to the MessagesController. With these routes defined, you would set the Messaging URL in the Twilio dashboard to https://your.site/messages as an HTTP POST Webhook.

Your controller handles the message:

class MessagesController < ApplicationController

  def create
    # First, check to see if this message really came from Twilio
    uri, signature = [request.protocol, request.host_with_port, request.path].join, request.env['HTTP_X_TWILIO_SIGNATURE']
    validator = Twilio::Util::RequestValidator.new(ENV['TWILIO_AUTH_TOKEN'])
    
    if validator.validate(uri, request.request_parameters, signature)
      # The params hash has the data from the Twilio payload
      Rails.logger.info "I just got this message from #{params[:From]}: #{params[:Body]}"

      # Send back a response that Twilio can handle
      render xml: { Sms: "Thanks, I got your message" }.to_xml(root: 'Response')

    else
      # This request didn't come from Twilio, so return a "Forbidden" response
      head(403)
    end

  end

end




Got any Twilio Question?