11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 
     | 
    
      # File 'app/operations/twilio/rails/phone/twiml/prompt_operation.rb', line 11
def execute
  return Twilio::Rails::Phone::Twiml::ErrorOperation.call(phone_call_id: phone_call.id, tree: tree) if phone_call.answering_machine?
  response = phone_call.responses.find(response_id)
  prompt = tree.prompts[response.prompt_handle]
  raise Twilio::Rails::Phone::InvalidTreeError, "cannot find #{response.prompt_handle} in #{tree.name}" unless prompt
  twiml_response = Twilio::TwiML::VoiceResponse.new do |twiml|
    unless prompt.gather&.interrupt?
      add_messages(twiml, message_set: prompt.messages, response: response)
    end
    case prompt.gather&.type
    when :digits
      args = {
        action: ::Twilio::Rails::Engine.routes.url_helpers.phone_prompt_response_path(
          format: :xml,
          tree_name: tree.name,
          response_id: response.id
        ),
        input: "dtmf",
        num_digits: prompt.gather.args[:number],
        timeout: prompt.gather.args[:timeout],
        action_on_empty_result: false
      }
      args[:finish_on_key] = prompt.gather.args[:finish_on_key] if prompt.gather.args[:finish_on_key]
      twiml.gather(**args) do |twiml|
        if prompt.gather&.interrupt?
          add_messages(twiml, message_set: prompt.messages, response: response)
        end
      end
      twiml.redirect(::Twilio::Rails::Engine.routes.url_helpers.phone_timeout_path(
        format: :xml,
        tree_name: tree.name,
        response_id: response.id
      ))
    when :voice
      args = {
        max_length: prompt.gather.args[:length],
                timeout: prompt.gather.args[:timeout],
        action: ::Twilio::Rails::Engine.routes.url_helpers.phone_prompt_response_path(
          format: :xml,
          tree_name: tree.name,
          response_id: response.id
        ),
        recording_status_callback: ::Twilio::Rails::Engine.routes.url_helpers.phone_receive_recording_path(
          response_id: response.id
        )
      }
      if prompt.gather.args[:transcribe]
        args[:transcribe] = true
        args[:transcribe_callback] = ::Twilio::Rails::Engine.routes.url_helpers.phone_transcribe_path(response_id: response.id)
      end
      args[:profanity_filter] = true if prompt.gather.args[:profanity_filter]
      twiml.record(**args)
    when :speech
      args = {
        action: ::Twilio::Rails::Engine.routes.url_helpers.phone_prompt_response_path(
          format: :xml,
          tree_name: tree.name,
          response_id: response.id
        ),
        input: "speech",
        timeout: prompt.gather.args[:timeout],
        action_on_empty_result: true,
        language: prompt.gather.args[:language].presence || "en-US",
        enhanced: !!prompt.gather.args[:enhanced]
      }
      args[:speech_timeout] = prompt.gather.args[:speech_timeout] if prompt.gather.args[:speech_timeout]
      args[:speech_model] = prompt.gather.args[:speech_model] if prompt.gather.args[:speech_model].present?
      args[:profanity_filter] = true if prompt.gather.args[:profanity_filter]
      twiml.gather(**args)
    when nil
      twiml.redirect(::Twilio::Rails::Engine.routes.url_helpers.phone_prompt_response_path(
        format: :xml,
        tree_name: tree.name,
        response_id: response.id
      ))
    else
      raise Twilio::Rails::Phone::InvalidTreeError, "unknown gather type #{prompt.gather.type.inspect}"
    end
  end
  Twilio::Rails.config.logger.info("prompt_twiml: #{twiml_response}")
  twiml_response.to_s
end
     |