user2515526 user2515526 - 1 year ago 99
Ruby Question

Twilio: Receive SMS and Do Something With Body Content

I'm having a tough time understanding Twilio. I've read the docs, and plan to read them again, but I was hoping for some pointers. I am using Ruby on Rails for my application.

What I want to do is to be able to receive a text message from a user with some body text. Then, I want to be able to save that text in my model some how. How do I go about doing this?

Thank you!

Answer Source

The Twilio number can be associated with a callback URL which it will send a POST request to when it receives an SMS. This callback URL should be customized to point to a controller in your app that you will use for processing SMS. From there, you can just read the params hash for details of the SMS message received. Of note: params['From'] and params['Body']. Store the text from those params into any model you like!

Twilio Callback URL



resource :sms, only: :create


class SmsController < ApplicationController
  skip_before_filter :force_ssl # You may need this if your app uses https normally

  def create
    # Do something with params['From'] -- contains the phone number the SMS came from
    # Do something with params['Body'] -- contains the text sent in the SMS

    # <Reponse/> is the minimum to indicate a "no response" from Twilio
    render xml: "<Response/>"