Skip to content

Instantly share code, notes, and snippets.

@rememberlenny
Forked from lambtron/twiliocontroller.rb
Last active August 29, 2015 14:14
Show Gist options
  • Save rememberlenny/fe42792ae2fe808b8a45 to your computer and use it in GitHub Desktop.
Save rememberlenny/fe42792ae2fe808b8a45 to your computer and use it in GitHub Desktop.
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