Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save technicalpickles/404392 to your computer and use it in GitHub Desktop.
Save technicalpickles/404392 to your computer and use it in GitHub Desktop.
require 'twiliolib'
require 'rexml/document'
class MessagesController < ApplicationController
TWILIO_API_VERSION = '2008-08-01'
TWILIO_ACCOUNT_SID = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
TWILIO_ACCOUNT_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
def create
@message = Message.new(params[:message])
# TODO push Twilio logic into Message, or some other helper
account = Twilio::RestAccount.new(TWILIO_ACCOUNT_SID, TWILIO_ACCOUNT_TOKEN)
message_data = {
'From' => @message.From,
'To' => @message.To,
'Body' => @message.Body
}
twilio_response = account.request("/#{API_VERSION}/Accounts/#{TWILIO_SID}/SMS/Messages", 'POST', message_data)
twilio_response.error! unless resp.kind_of? Net::HTTPSuccess
h = REXML::Document.new(resp.body)
sid = REXML::XPath.first(h, "//Sid")
@message.SmsMessageSid = sid.text # TODO use snake case for model attributes
respond_to do |format|
if @message.save
flash[:notice] = 'Message was successfully created.'
format.html { redirect_to(@message) }
format.xml { render :xml => @message, :status => :created, :location => @message }
else
format.html { render :action => "new" }
format.xml { render :xml => @message.errors, :status => :unprocessable_entity }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment