The new Twilio 5x libraries have introduced a bit of an odd approach to gathering DTMF digits on phonecalls.
The old 4x code for a gather would have looked something like:
twiml.BeginGathertwiml.BeginGather(new { numDigits = "1", action = "/TwilioCallbacks/InputResponse" });
if(x == 10){
twiml.Say("I am saying a thing because x = 10");
}
else{
twiml.Say("I am saying the other thing");
}
twiml.EndGather();
Now, if you wanted to let the user punch digits on the keypad while your bot was talking to them, that'd work just fine.
However in Twilio 5x, it looks like this:
twiml.Say("I am saying a really long thing where the user must wait until the twiml script reaches the gather phrase");
twiml.Say("press 1 if stack overflow is awesome, press 2 to quit programming forever");
twiml.Gather(
numDigits: 1,
input: new List<InputEnum>() { InputEnum.Dtmf },
timeout: 10,
method: "POST",
action: new System.Uri(Startup.hostAddress + "/TwilioCallbacks/InputResponse")
);
Right after Gather(...) you have a short window to collect the response, if you set a timeout on the response, the twiml won't proceed to the next say until the timeout expires.
How can I Gather digits in such a way that the user can interact with the keypad at any point during the message? The new approach seems to be a step backward.
edit: Clarified the 4xx use case, such that folks can understand why chaining .Say won't work here.
edit: Some people below are suggesting chaining the .Say() verb right after .Gather().
This actually doesn't behave as expected either. This is the C# code.
twiml.Gather(
numDigits: 1,
input: new List<InputEnum>() { InputEnum.Dtmf },
timeout: 10,
method: "POST",
action: new System.Uri(Startup.hostAddress + "/TwilioCallBot/InputResponse")
).Say("this is a test");
This is the resulting twiml:
<Gather input="dtmf" action="https://callbot21.ngrok.io/RCHHRATool//TwilioCallBot/InputResponse" method="POST" timeout="10" numDigits="1">
</Gather>
<Say>this is a test</Say>
The say verb needs to be inside the gather tag to get the behavior we're looking for.
Say()
on thetwml/ response
object, rather you callSay()
on the newly createdGather
object... – IronGeek