I am using Twilio in my rails 3.1.3 app and I have got everything basically set up, i.e. a controller for sms and xml builders for the views depending on the response. The only thing I can't figure out is how to keep track of the conversation. The Twilio docs are pretty bad for using anything other than PHP to do this. I have tried using the Rails session hash, session[:variable], but it doesn't seem to be saving the session, as I tried redirecting and printing it out and got nothing. Below is the code of the controller.
def receive # Check for session variable and redirect if necessary @sms_state = session[:sms_state] if @sms_state == 'confirmation' redirect_to 'confirm' end if condition @sms_state = 'confirmation' session[:sms_state] = @sms_state render :action => "view.xml.builder", :layout => false else @sms_state = 'new_state' session[:sms_state] = @sms_state render :action => "error.xml.builder", :layout => false end end # method that should be called after user deals with first part def confirm if condition @sms_state = session[:sms_state] = nil render :action => "confirm_view.xml.builder", :layout => false else @sms_state = 'confirmation' session[:sms_state] = @sms_state render :action => "error.xml.builder", :layout => false end end
I have now set up a database table to track the current conversation state depending on the phone number contacting my app. The only thing now that I need to do is set an expiration for this conversation, just like a session or cookie. I am not sure how to do this or if its even possible.