user3305040
user3305040

Reputation: 55

Replying to SMS via Twilio API with Ruby on Rails 4.0

I'm having a tough time understanding Twilio. I've read the docs many many times and still couldn't get my app run. I'm writing a web app in Ruby on Rails (4.0) to get an SMS text from students and print them out on the website.

In the Gemfile I put

gem 'twilio-ruby'

Then I have a controller like this

class TwilioController < ApplicationController

def index

end

def sent_sms
  account_sid = "I put my ID here"
  auth_token = "I put my auth token here"
  twilio_phone_number = "xxxxxxxxx"

  message_body = params["Body"]
  from_number = params["From"]

  @client = Twilio::REST::Client.new(account_sid, auth_token)
  @client.account.sms.messages.create(
    from: "+1#{twilio_phone_number}",
    to: "+1#{from_number}",
    body: "Hey there! I got a text from you"
  )
end

Now I don't know how to print out those messages gotten from students to the webpage. I spent 2 days on this and still can't figure it out. Also, I don't know what to put in the URL in Twilio account in this screenshot below. I deploy my app to Heroku though. Any advice is appreciated.

https://www.dropbox.com/s/zxbyw6j5te8ipgp/Screen%20Shot%202014-04-12%20at%203.43.18%20PM.png

Upvotes: 1

Views: 810

Answers (1)

Devin Rader
Devin Rader

Reputation: 10366

Twilio evangelist here.

Let me start clarify whats your trying to do. From the code above it looks like you want to receive a text message from your students and then send them back a simple response. You also want to print out the text messages that you've received? Hopefully I've got that right. If I do, lets start with receiving messages and sending a reply.

When a student sends a text message to your Twilio phone number, Twilio will receive that message and make an HTTP request to whatever URL you have set in the Messaging URL. Based on the Rails code in your post, it looks like that URL is going to be something like:

http://[yourdomain].com/Twilio/send_sms

Now, if you want to send a SMS message the student who sent you a message, you just need to generate and return some TwiML containing the <Message> verb from the send_sms function. That would look something like this:

twiml = Twilio::TwiML::Response.new do |r|
    r.Message "Hey there! I got a text from you."
end
twiml.text

If you loaded the send_sms route in a browser you should see that your Rails app outputs some TwiML that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>Hey there! I got a text from you.</Message>
</Response>

This code came from the "Replying to an SMS" quickstart which has a ton of great info in it. Another great resource for getting started with receiving text messages using Rails is this fantastic blog post.

OK, so now you've had a bunch of students send some text messages to you and you want to create a page in your Rails app to display them. For this you're going to need to use the REST API. This sample from the docs shows how to use the Ruby helper library to get a list of messages from Twilio. You can pass the list you get back from Twilio to your Rails view where you can loop over it outputting whatever HTML elements you want to use to display the SMS attributes.

Hop that helps.

Upvotes: 1

Related Questions