Skip to content

Instantly share code, notes, and snippets.

@stijlist
Created May 30, 2014 08:31
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 stijlist/71805675d7664d017656 to your computer and use it in GitHub Desktop.
Save stijlist/71805675d7664d017656 to your computer and use it in GitHub Desktop.
class SmsController < ApplicationController
skip_before_filter :verify_authenticity_token, only: [:receive_sms]
def new_sms
# validate that user is an admin
redirect_to :root unless current_user && current_user.admin?
end
def send_sms
begin
sms = SMS.new(params[:message])
numbers = User.where(sms_contact: true).pluck(:phone)
results = BatchSMS.new(sms, numbers, TwilioService).send_message!
flash.update(SmsResultPresenter.present_hash(results))
rescue SMS::InvalidEncodingError => e
flash[:error] = e.message
end
redirect_to send_sms_path
end
def receive_sms
msg_body = params["Body"]
from_num = params["From"]
payload = {
text: "From: #{from_num}. Message: #{msg_body}"
}.to_json
uri = URI('https://tmcyf.slack.com/services/hooks/incoming-webhook?token=lFAo4KrEmegGC3IoBnbfYvdP')
@res = Net::HTTP.post_form(uri, 'payload' => payload)
render json: @res
end
def lookup_number
fname = params["fname"]
lname = params["lname"]
numbers = User.where(fname: fname, lname: lname).pluck(:number)
render json: {number: numbers.first} # just return the first match for now
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment