What does Gather return if there is no user input?

852 views
Skip to first unread message

Ben Clark

unread,
Sep 1, 2017, 7:20:35 AM9/1/17
to Twilio Functions
Hi there

I'm pretty new to Twilio in general. I've been trying to set up a basic phone menu system.

If the user holds the line, i.e doesn't press any digits, I want the menu system to call a different set of contacts. I've tried null and an empty string but it doesn't seem to work. How do I create a switch case for when no input is gathered? 

exports.handler = function(context, event, callback) {
  let twiml = new Twilio.twiml.VoiceResponse()
  switch (event.Digits) {
    case '1':
      twiml.dial(context.sales_number)
      twiml.dial().sip(context.sip_john)
      break
    case '2':
      twiml.dial().sip(context.sip_support)
      break
    case null:
        twiml.dial().sip(context.sip_ben)
        twiml.dial().sip(context.sip_bob)
        twiml.dial(context.office_number)
       break
    default:
      twiml.gather({numDigits: 1, timeout:3})
        .say('Press 1 for sales. Press 2 for support. Or hold the line to speak to one of our representatives')          
  }
    
  callback(null, twiml)
}

Many thanks

Ben 

Chris Corcoran

unread,
Sep 1, 2017, 2:45:02 PM9/1/17
to Twilio Functions
Hi Ben,

Thanks for your question, I'm always happy to help get new users up and running on Twilio.

The trick with the <Gather> verb is that if it times out and receives no input then Twilio will move onto the next TwiML verb in your response. For example, lets say you had a TwiML response that looks like this:

<Response>
    <Gather numDigits="1" timeout="3">
        <Say>Welcome! Press 1 for sales. Press 2 for support</Say> 
    </Gather>
</Response>

In this example if the timeout of 3 seconds is reached then Twilio will move onto the next verb. As there is no verb the call will hang up. To connect the caller to another set of contacts you need to include a <Dial> after the <Gather>. Your result should look something like this:

<Response>
    <Gather numDigits="1" timeout="3">
        <Say>Welcome! Press 1 for sales. Press 2 for support</Say> 
    </Gather>
    <Dial>
       <Sip>{Sip Address}</Sip>
    </Dial>
</Response>

Because of the way <Gather> works if the user enters a digit the <Dial> will never be reached. If they don't it will simply continue to call the provided number. You can learn more about the <Gather> verb here: https://www.twilio.com/docs/api/twiml/gather

Hopefully, this answered your question. Please let me know if there is anything else I can do to help.

Thanks,
Chris Corcoran
Product Manager, Twilio Runtime

Ben Clark

unread,
Sep 4, 2017, 5:46:20 AM9/4/17
to Twilio Functions
Ah I see, this makes sense now, thanks for the clarification. 

Ben
Reply all
Reply to author
Forward
0 new messages