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!
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With