Ruby on Rails Cookies

Forgive me if i'm in the wrong asking this here but i've looked everyhwere and tried reading tutorials and nothing helps. Im currently building a ruby on rails app where users will text a number and based on the text content(only two options) they receive one of two surveys. then they proceed to fill out the survey, when done it gets saved in a sqlite3 database for future reference.

My current problem is that I'm not sure how to use cookies to make sure that the conversation gets "continued" without reinitializing every time thehy text the number.


Thanks for any and all help

Answers

  • Hey @lerxcnm, I'm a Rails developer too. We do have a tutorial on using cookies with SMS and Ruby (though it uses a Sinatra app as an example).

    The idea is that when you respond to incoming webhook requests you can set session cookies and Twilio will return those cookies with the next webhook in the conversation. So, you might do something like:

    def messages
      twiml = Twilio::TwiML::MessagingResponse.new
      if session[:which_survey]
        # We have asked which survey, so we need to parse the answer, then send the first question
        session[:which_survey] = nil
        survey_type = parse_survey_from_answer(params[:Body])
        session[:survey_type] = survey_type
        question = get_first_question_for_survey(survey_type)
        twiml.message(question)
      elsif session[:survey_type]
        # We know which survey they are taking, we need to store the answer and ask the next question
        answer = params[:Body]
        question = save_answer_and_get_next_question(answer, survey_type)
        twiml.message(question)
      else
        # New conversation, need to ask which survey they want to take
        twiml.message("Which survey do you want to take? A or B?")
        session[:which_survey] = true
      end
      render :xml => twiml
    end
    

    In this case we are setting different session variables based on where we are in the conversation. I haven't tested the above, so take it more as inspiration than something to copy and paste.

    Let me know how you get on with that.

  • Hey thanks!! I think that this helps me understand a little bit better. I will try within the next few days and get back to you. I hope I dont bother you too much with questions h aha

  • Hey @lerxcnm, hope it goes well for you! You're always welcome to ask questions, and this community is here to help you get answers to them.

If this is an emergency, please contact Twilio Support. This is not an official Support channel. https://support.twilio.com/
Have an urgent question?
Please contact Twilio Support. This is not an official Support channel.
Contact Support