Created
January 10, 2013 08:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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