Skip to content

Instantly share code, notes, and snippets.

@nkotbpartyomg
Created January 8, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nkotbpartyomg/32e2a485530e2f83046a to your computer and use it in GitHub Desktop.
Save nkotbpartyomg/32e2a485530e2f83046a to your computer and use it in GitHub Desktop.
Replies controller for teacher/parent texting app
class RepliesController < ApplicationController
require "time"
skip_before_filter :verify_authenticity_token
skip_before_filter :authenticate_teacher!
def receive
# 1. set a @parent based on phone number and/or course number if they exist
# 2/ you then move on to the next state on the parent
@reply = Reply.new
@reply.message_id = params["SmsSid"]
@reply.account_sid=params["AccountSid"]
@reply.from=params["From"]
@reply.body=params["Body"]
#@reply.status=params["SmsStatus"]
@reply.api_version=params["ApiVersion"]
if Parent.where(phone_number: @reply.from).count == 0 && Course.where(class_code: @reply.body).present?
@parent=Parent.new
@parent.state = 'course_num_and_phone'
@parent.phone_number=@reply.from
@parent.class_code=@reply.body
@parent.course_id = Course.where(class_code: @reply.body).last.id
@parent.added_course_number_and_phone_number
@parent.save!
get_first_nm(@reply.from)
elsif Parent.where(phone_number: @reply.from).last.state == 'course_num_and_phone' && Course.where(class_code: @reply.body).present?
@parent=Parent.new
@parent.state = 'course_num_and_phone'
@parent.phone_number=@reply.from
@parent.class_code=@reply.body
@parent.course_id = Course.where(class_code: @reply.body).last.id
@parent.added_course_number_and_phone_number
@parent.save!
get_first_nm(@reply.from)
elsif Parent.where(phone_number: @reply.from).last.state == "first_nm"
@parent=Parent.where(phone_number: @reply.from).last
@parent.first_nm = @reply.body
@parent.added_first_name
@parent.save!
get_last_nm(@reply.from)
elsif Parent.where(phone_number: @reply.from).last.state == "last_nm"
@parent=Parent.where(phone_number: @reply.from).last
@parent.last_nm = @reply.body
@parent.added_last_name
@parent.save!
get_child_nm(@reply.from)
elsif Parent.where(phone_number: @reply.from).last.state == "child_nm"
@parent=Parent.where(phone_number: @reply.from).last
@parent.child_nm = @reply.body
@parent.added_child_name
@parent.save!
get_relationship(@reply.from)
elsif Parent.where(phone_number: @reply.from).last.state == "relationship"
@parent=Parent.where(phone_number: @reply.from).last
@parent.relationship = @reply.body
@parent.added_relationship
@parent.save!
get_delivery_time(@reply.from)
elsif Parent.where(phone_number: @reply.from).last.state == "delivery_time"
@parent=Parent.where(phone_number: @reply.from).last
@parent.msg_time = @reply.body
if @parent.msg_time.nil?
@parent.msg_time = Time.new(2001, 1, 1, 1, 1, 1, 0)
end
lowest_allowable_time = Time.new(2000, 1, 1, 13, 0, 0, 0)
highest_allowable_time = Time.new(2000, 1, 1, 22, 0, 0, 0)
low_am_time = Time.new(2000, 1, 1, 1, 0, 0, 0)
high_am_time = Time.new(2000, 1, 1, 10, 0, 0, 0)
invalid_time = Time.new(2001, 1, 1, 1, 1, 1, 0)
puts @parent.msg_time
if @parent.msg_time >= lowest_allowable_time && @parent.msg_time <= highest_allowable_time
@parent.added_delivery_time
@parent.save!
send_sign_off(@reply.from)
elsif @parent.msg_time >= low_am_time && @parent.msg_time <= high_am_time
@parent.msg_time += 12.hours
@parent.added_delivery_time
@parent.save!
send_sign_off(@reply.from)
elsif @parent.msg_time == invalid_time
get_better_delivery_time(@reply.from)
else
get_better_delivery_time(@reply.from)
end
#Twilio will automatically send their own unsubscribe message if a parent texts stop and will prevent
#Bring Up from ever contacting them again but we also want to remove that parent from the database on our end
elsif Parent.find_by_phone_number(@reply.from).present? && @reply.body.downcase == "stop"
Parent.where(phone_number: @reply.from).destroy_all
elsif Parent.where(phone_number: @reply.from).last.state == 'course_num_and_phone' && @reply.body.length != 4
send_default_response(@reply.from)
end
respond_to do |format|
if @reply.save
#format.html { redirect_to @reply, notice: 'Reply was successfully created.' }
format.xml{ render xml: @reply, status: :created, location: @reply }
else
#format.html { render action: "new" }
format.xml { render xml: @reply.errors, status: :unprocessable_entity }
end
end
end
def get_first_nm(sendto)
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
from = "+15128618455" # Your Twilio number
to = sendto
client.account.sms.messages.create(
:from => from,
:to => to,
:body => "Welcome to BringUp! Let's begin. What is your first name?"
)
end
def get_last_nm(sendto)
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
from = "+15128618455" # Your Twilio number
to = sendto
client.account.sms.messages.create(
:from => from,
:to => to,
:body => "What is your last name?"
)
end
def get_child_nm(sendto)
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
from = "+15128618455" # Your Twilio number
to = sendto
client.account.sms.messages.create(
:from => from,
:to => to,
:body => "What is the name of the child?"
)
end
def get_relationship(sendto)
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
from = "+15128618455" # Your Twilio number
to = sendto
client.account.sms.messages.create(
:from => from,
:to => to,
:body => "What is your relationship to the child?"
)
end
def get_delivery_time(sendto)
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
from = "+15128618455" # Your Twilio number
to = sendto
client.account.sms.messages.create(
:from => from,
:to => to,
:body => "What time would you like your message delivered? Enter a time between 1:00 PM and 10:00 PM. Please enter it in HH:MM format Example 03:45."
)
end
def get_better_delivery_time(sendto)
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
from = "+15128618455" # Your Twilio number
to = sendto
client.account.sms.messages.create(
:from => from,
:to => to,
:body => "That time was invalid. Pick a time between 1:00 PM and 10:00 PM. Please enter it in HH:MM format Example 03:45 or 10:00"
)
end
def send_sign_off(sendto)
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
from = "+15128618455" # Your Twilio number
to = sendto
client.account.sms.messages.create(
:from => from,
:to => to,
:body => "Thank you. We have all the information we need."
)
end
def send_default_response(sendto)
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_TOKEN']
client = Twilio::REST::Client.new account_sid, auth_token
from = "+15128618455" # Your Twilio number
to = sendto
if Parent.where(phone_number: sendto).count == 1 && Parent.where(phone_number: sendto).last.course.response?
@body = Parent.where(phone_number: sendto).last.course.response
else
@body = Reply.global_default_response
end
client.account.sms.messages.create(
:from => from,
:to => to,
:body => @body
)
end
private
def replies_params
params.require(:reply).permit(:account_sid, :api_version, :body, :date_created, :date_updated, :direction, :from, :message_id,
:price, :price_unit, :uri, :status)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment