Skip to content

Instantly share code, notes, and snippets.

@manlycode
Created January 18, 2012 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manlycode/1635511 to your computer and use it in GitHub Desktop.
Save manlycode/1635511 to your computer and use it in GitHub Desktop.
class SendTextController < ApplicationController
def index
end
def send_text_message
number_to_send_to = params[:number_to_send_to]
twilio_sid = "abc123"
twilio_token = "foobar"
twilio_phone_number = "6165555555"
@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 => "This is an message. It gets sent to #{number_to_send_to}"
)
end
end
@markburns
Copy link

This could be much nicer if it was for example, an SmsController, with a create action, then the library could be something like

class SmsController < ApplicationController

  def create
    number_to_send_to = params[:number_to_send_to]

    Twilio::SMS.create(
      :from => "+1#{twilio_phone_number}",
      :to => number_to_send_to,
      :body => "This is an message. It gets sent to #{number_to_send_to}"
    )
  end
end

with config in a class level initializer like the following so that you don't need to set up every time

Twilio.config do |c|
  c.twilio_sid = "abc123"
  c.twilio_token = "foobar"
  c.twilio_phone_number = "6165555555"
end

@alexcchan
Copy link

Just ran across this old comment. The Ruby client is not (currently) thread-safe so it's probably better to keep it request scoped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment