RamJet
RamJet

Reputation: 313

How do I make a Twilio call hangup if my app doesn't get a valid response from the call recipient

I want the code below to hangup if I don't get a valid response within 5 seconds after the "last chance" message. I've set :timeout to 11. The call should end in 10 seconds, (excluding the time to ask the questions). The 1st question is asked and waits 5 seconds before asking the 2nd. I want the call to hangup after the 2nd g.pause. I've tried r.hangup in the main block and g.hangup in the gather block. Neither of those worked for me. How should it be done?

def digits
  twiml_response = Twilio::TwiML::Response.new do |r|
    r.Gather numDigits: '1', timeout: 11, action: communications_menu_path do |g|
      g.Say "Please press one to continue", voice: 'alice', language: 'an-AU'
      g.Pause length: 5
      g.Say "Last chance. I didn't get any response. Please press one to continue.", voice: 'alice', language: 'an-AU'
      g.Pause length: 5
    end
  end
  render :xml => twiml_response.to_xml
end

Upvotes: 1

Views: 82

Answers (1)

philnash
philnash

Reputation: 73090

Twilio developer evangelist here.

When your <Gather> times out it will still make the request to your action attribute. However, the Digits parameter to that request will be empty.

So, instead of hanging up in this part of the TwiML, during your next TwiML (under communications_menu_path) you should check if the Digits parameter is present but empty and then hang up. Something like:

def communications_menu
  if params["Digits"] && params["Digits"].blank?
    render :xml => Twilio::TwiML::Response.new { |r| r.Hangup }.to_xml 
  else
    # the rest of the TwiML
  end
end

Let me know if that helps at all.

Upvotes: 1

Related Questions