Skip to content

Instantly share code, notes, and snippets.

@lambtron
Created January 10, 2013 08:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lambtron/4500515 to your computer and use it in GitHub Desktop.
Save lambtron/4500515 to your computer and use it in GitHub Desktop.
Ruby server code that allows for CORS and sends an SMS via Twilio.
require 'rubygems'
require 'twilio-ruby'
require 'net/http'
require 'uri'
require 'json'
class TwilioController < ApplicationController
TWILIO_ACCOUNT_SID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
TWILIO_ACCOUNT_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SENDER_NUMBER = '+xxxxxxxxxx'
def send_sms
set_access_control_headers
head :ok
# Get recipient phone number from POST.
number = params["recipient"]
# # Get body from POST.
body = params["body"]
# # Send sms.
twilio_client = Twilio::REST::Client.new TWILIO_ACCOUNT_SID, TWILIO_ACCOUNT_TOKEN
twilio_client.account.sms.messages.create(
:from => "#{SENDER_NUMBER}",
:to => "#{number}",
:body => "#{body}"
)
render :file => 'app/views/twiliocon/index.html'
end
private
def set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Method'] = 'POST, GET, OPTIONS'
headers['Access-Control-Max-Age'] = '1278000'
render :json => { :success => true }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment