Search code examples
twiliotwilio-apitwilio-phptwilio-twiml

Twilio Gather Only Allow 1 or 2 no other number


When gathering a DTMF response, how do you limit the response to say the number 1 or 2 but no other number? Here is what I have in my bin currently:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Gather action="https://example.com/index.php?nid={{nid}}" method="GET" numDigits="1">
        <Play loop="10">https://example.com/Message_large.mp3</Play>
    </Gather>
    <Say>Thank you, Goodbye</Say>
</Response>

But I couldn't find anything in the Gather docs that shows how to do this..

https://www.twilio.com/docs/voice/twiml/gather


Solution

  • It is not possible to constrain the input at the collection stage since you cannot analyze the input until after it is collected and submitted via the action URL.

    As the TwiML example you posted shows, you can constrain the number of digits entered, numDigits="1" to allow for quicker analysis of the entered digit and then respond accordingly, if the input is out of range.