Search code examples
twiliotwilio-apitwilio-twiml

How to prevent Twilio Gather from hanging up when the input is ONLY the finishOnKey?


We have a simple case where a user will call our phone number, and in certain cases will need to enter a PIN, followed by a pound sign, to authenticate and continue on. In this case, the pound sign is defined as our finishOnKey.

Everything works perfectly if the user enters a PIN. If it's valid, they move on; if it's not, it blocks them.

However, if the user ONLY hits pound, without any preceding input, the call hangs up.

  • This does not throw any exceptions.
  • We do not have the pound sign listed as a hangup key anywhere else in our code.
  • In fact, if I redefine the finishOnKey to an asterisk, the behavior persists - it hangs up if the user only types an asterisk (although, importantly, it no longer hangs up on the pound sign).

This leads me to believe that either this is built into Twilio or there is possibly something I'm missing in our code, but I can't imagine what. Is this an intended behavior from Twilio, and/or how can this be prevented? We would like the system to simply react as if the user entered an invalid PIN.


Solution

  • Twilio developer evangelist here.

    From the <Gather> documentation on the action attribute:

    If the 'timeout' is reached before the caller enters any digits, or if the caller enters the 'finishOnKey' value before entering any other digits, Twilio will not make a request to the 'action' URL but instead continue processing the current TwiML document with the verb immediately following the <Gather>.

    I presume that you don't have any TwiML that follows your <Gather> as you expect it always to submit to the action URL. I recommend you <Say> a message to the user and then <Redirect> back to the same URL, a bit like this:

    <Response>
      <Gather finishOnKey="#" action="https://example.com/gather_response">
        <Say>Please enter the PIN</Say>
      </Gather>
      <Say>We did not receive a PIN, please try again.</Say>
      <Redirect>https://example.com/gather</Redirect>
    </Response>
    

    Let me know if that helps.