Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 951
aguynamedloren Avatar asked Jan 12 '11 04:01

aguynamedloren


People also ask

How can I receive SMS in Twilio?

Log in to the Twilio Console's Phone Numbers page. Click on the phone number you'd like to have connected to your Function. If you want the Function to respond to incoming SMS, find the A Message Comes In option under Messaging.

Does Twilio charge for incoming SMS?

Twilio will only charge your Twilio project for sending messages through our API. We will not charge the recipient of your message for receiving a Twilio message.

How do I reply to text from Twilio?

When you send an SMS message to your Twilio phone number, Twilio will send a webhook, an HTTP request with all the details about the message, to a URL you associate with that number. You can reply to the message by responding to the webhook with TwiML (Twilio Markup Language).


1 Answers

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.

like image 197
aguynamedloren Avatar answered Sep 29 '22 21:09

aguynamedloren