Reputation:
new to ruby/rails here so please forgive my ignorance. Working on adding SMS capabilities to an existing app and I’ve been successful while just setting up a ruby document and sending a sms message but when I go to incorporate it in my rails app, I’m getting a little lost.
I’ve followed this document (https://www.twilio.com/blog/2012/02/adding-twilio-sms-messaging-to-your-rails-app.html) and created a SendTextController with the following code but included the account_sid and account_token in my application.yml file using ENV and figaro. In my actual file, I have my twilio phone number and the number I'd like to send it to (just blocked it out here).
Once I set this up, I’m lost at how to call this method from a view in my app?
class TwilioController < ApplicationController
def index
end
def send_text_message
number_to_send_to = "+1XXXXXXXXXX"
twilio_sid = ENV["TWILIO_SID"]
twilio_token = ENV["TWILIO_TOKEN"]
twilio_phone_number = "+1XXXXXXXXXX"
@twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token
@twilio_client.account.sms.messages.create(
:from => "+1#{twilio_phone_number}",
:to => number_to_send_to,
:body => "Test Message from testing"
)
end
end
Upvotes: 1
Views: 2011
Reputation: 1209
Your code to send the text message looks pretty much solid - if you have that code defined in your controller, and you'd like to call it when rendering, say, the index view of your TwilioController, you should be able to call self.send_text_message()
.
But if you're just getting started (and presumably using Rails 4?) there's a more up-to-date tutorial that can take you through the entire integration process.
Upvotes: 2