<commit_before>class VoiceFeedbackController < ApplicationController

  #@@app_url = "1000in1000.com"

  def route_to_survey
    if !session[:survey_started]
    #if !params.has_key?("Digits") 
      session[:survey_started] = true
      session[:call_source] = call_source_from_twilio_phone_number(params["To"])
      response_xml = Twilio::TwiML::Response.new do |r| 
        r.Gather :timeout => 15, :numDigits => 5, :finishOnKey => '' do |g|
          g.Play VoiceFile.find_by_short_name("welcome_property").url
        end
        r.Redirect "route_to_survey"
      end.text
    # Eventually replace below with lookup and validation of property code
    else
      if params["Digits"].to_s.length == 5
        session[:property_id] = Property.find_by_property_code(params["Digits"]).id
        session[:survey] = "property"
      else
        # Need to change this: remove neighborhood survey and just ask for valid property code
        #session[:survey] = "neighborhood"
      end
      # Hard code neighborhood ID for now
      session[:neighborhood_id] = 1
      response_xml = Twilio::TwiML::Response.new do |r| 
        r.Redirect "voice_survey"
      end.text
    end
    render :inline => response_xml
  end

  def voice_survey
    # Set the index if none exists
    if session[:current_question_id] == nil
      @current_question = Question.find_by_short_name(Survey.questions_for(session[:survey])[0])
      session[:current_question_id] = @current_question.id
    else
      # Process data for existing question 
      @current_question = Question.find(session[:current_question_id])
      if @current_question.feedback_type == "numerical_response"
        FeedbackInput.create!(question_id: @current_question.id, neighborhood_id: session[:neighborhood_id], :property_id => session[:property_id], numerical_response: params["Digits"], phone_number: params["From"][1..-1].to_i, call_source: session[:call_source])
      elsif @current_question.feedback_type == "voice_file"
        FeedbackInput.create!(question_id: @current_question.id, neighborhood_id: session[:neighborhood_id], :property_id => session[:property_id], voice_file_url: params["RecordingUrl"], phone_number: params["From"][1..-1].to_i, call_source: session[:call_source])
      end
      # Then iterate counter
      current_index = Survey.questions_for(session[:survey]).index(@current_question.short_name)
      @current_question = Question.find_by_short_name(Survey.questions_for(session[:survey])[current_index+1])
      # If there remains a question
      if @current_question
        session[:current_question_id] = @current_question.id
      else
        @hang_up = true
      end
    end

    @response_xml = Twilio::TwiML::Response.new do |r| 
      if @hang_up
        #r.Say "Thank you very much for your feedback. Good bye."
        r.Play VoiceFile.find_by_short_name("thanks").url
        r.Hangup
      else
        if @current_question.feedback_type == "numerical_response"
          r.Gather :timeout => 10, :numDigits => 1 do |g|
            #r.Say @current_question.voice_text 
            r.Play @current_question.voice_file.url
          end
        else
          # Handle the voice recording here
          #r.Say @current_question.voice_text 
          r.Play @current_question.voice_file.url
          r.Record :maxLength => 60<commit_msg>Add empty finishOnKey to second question to try and make it work<commit_after>class VoiceFeedbackController < ApplicationController

  #@@app_url = "1000in1000.com"

  def route_to_survey
    if !session[:survey_started]
    #if !params.has_key?("Digits") 
      session[:survey_started] = true
      session[:call_source] = call_source_from_twilio_phone_number(params["To"])
      response_xml = Twilio::TwiML::Response.new do |r| 
        r.Gather :timeout => 15, :numDigits => 5, :finishOnKey => '' do |g|
          g.Play VoiceFile.find_by_short_name("welcome_property").url
        end
        r.Redirect "route_to_survey"
      end.text
    # Eventually replace below with lookup and validation of property code
    else
      if params["Digits"].to_s.length == 5
        session[:property_id] = Property.find_by_property_code(params["Digits"]).id
        session[:survey] = "property"
      else
        # Need to change this: remove neighborhood survey and just ask for valid property code
        #session[:survey] = "neighborhood"
      end
      # Hard code neighborhood ID for now
      session[:neighborhood_id] = 1
      response_xml = Twilio::TwiML::Response.new do |r| 
        r.Redirect "voice_survey"
      end.text
    end
    render :inline => response_xml
  end

  def voice_survey
    # Set the index if none exists
    if session[:current_question_id] == nil
      @current_question = Question.find_by_short_name(Survey.questions_for(session[:survey])[0])
      session[:current_question_id] = @current_question.id
    else
      # Process data for existing question 
      @current_question = Question.find(session[:current_question_id])
      if @current_question.feedback_type == "numerical_response"
        FeedbackInput.create!(question_id: @current_question.id, neighborhood_id: session[:neighborhood_id], :property_id => session[:property_id], numerical_response: params["Digits"], phone_number: params["From"][1..-1].to_i, call_source: session[:call_source])
      elsif @current_question.feedback_type == "voice_file"
        FeedbackInput.create!(question_id: @current_question.id, neighborhood_id: session[:neighborhood_id], :property_id => session[:property_id], voice_file_url: params["RecordingUrl"], phone_number: params["From"][1..-1].to_i, call_source: session[:call_source])
      end
      # Then iterate counter
      current_index = Survey.questions_for(session[:survey]).index(@current_question.short_name)
      @current_question = Question.find_by_short_name(Survey.questions_for(session[:survey])[current_index+1])
      # If there remains a question
      if @current_question
        session[:current_question_id] = @current_question.id
      else
        @hang_up = true
      end
    end

    @response_xml = Twilio::TwiML::Response.new do |r| 
      if @hang_up
        #r.Say "Thank you very much for your feedback. Good bye."
        r.Play VoiceFile.find_by_short_name("thanks").url
        r.Hangup
      else
        if @current_question.feedback_type == "numerical_response"
          r.Gather :timeout => 10, :numDigits => 1, :finishOnKey => '' do |g|
            #r.Say @current_question.voice_text 
            r.Play @current_question.voice_file.url
          end
        else
          # Handle the voice recording here
          #r.Say @current_question.voice_text 
          r.Play @current_question.voice_file.url
          r.Record :maxLength => 60
{'fraction_non_alphanumeric': 0.10613451589061346, 'fraction_numerical': 0.007095343680709534, 'mean_word_length': 3.362346872985171, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6261312', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1998, 'n_words': 435}