aguynamedloren
aguynamedloren

Reputation: 2273

Receiving a text message in Rails app (Twilio SMS)

Does anybody have any example code on how this would work? Seems like it should be pretty straightforward, but the Twilio documentation is sparse for SMS/Rails.

I have a simple "post" model with a "body" column and "from" column. I just want to display the SMS messages in a list. The closest thing I got to work was something like this:

#posts_controller.rb
class PostsController < ApplicationController
 def new
   @post = Post.new(:body=>"?",:from=>"?")
   @post.save
 end
end

#twilio sms url: ...myappurl/posts/new

This creates a new post, but the 'from' and 'body' values are "?", obviously. How do I pass the Twilio SMS 'From' and 'Body' values into the rails controller?

Any ideas or a nudge in the right direction? Thanks!

Upvotes: 7

Views: 2168

Answers (1)

aguynamedloren
aguynamedloren

Reputation: 2273

Just solved it! It was as simple as I thought it was.

In my posts_controller.rb file:

def twilio_create
  @post = Post.new(:body => params[:Body], :from => params[:From])
  @post.save
end

This effectively pulls the Body and From params from Twilio. The same can be applied for other params (SmsMessageSid, AccountSid, etc).

You can see the full list of parameters sent with Twilio's request here.

Upvotes: 7

Related Questions