Twilio 5x C# 库 - 在读取消息时收集数字

Twilio 5x C# Libraries - Gather digits while message is being read

新的 Twilio 5x 库引入了一些奇怪的方法来收集电话中的 DTMF 数字。

收集的旧 4x 代码看起来像这样:

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();

现在,如果您想让用户在您的机器人与他们交谈时在键盘上敲击数字,那会很好。

但是在 Twilio 5x 中,它看起来像这样:

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")
        );

在 Gather(...) 之后你有一个简短的 window 来收集响应,如果你在响应上设置超时,twiml 将不会继续下一个说直到超时到期.

如何收集数字,以便用户可以在消息发送过程中随时与键盘进行交互?新方法似乎是倒退了一步。

编辑: 澄清了 4xx 用例,这样人们就可以理解为什么链接 .Say 在这里不起作用。

编辑: 下面的一些人建议在 .Gather() 之后链接 .Say() 动词。

这实际上也不符合预期。这是 C# 代码。

    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");

这是生成的 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>

say 动词需要在 gather 标签内才能获得我们正在寻找的行为。

好的,我找到问题了。看起来 fourwhey 指向那里的 API 文档是正确的。我没有注意到的是 .Say 以特定方式附加到收集中。

这个:

twiml.Gather(...).Say("say a thing");

与此不同:

var gather = new Twilio.TwiML.Voice.Gather(...).Say("say a thing");

我能解决的最好问题是实际上有两个收集方法,twiml.Gather(...) 实际上是在调用 Twilio.TwiML.Gather.

从这里我们可以构建动态语音消息并像这样嵌套 Say 动词:

gather.Say("Say a thing");
gather.Say("Say another thing");

twiml 将按照我们的预期方式吐出:

<gather>
    <say>say a thing</say>
    <say>say another thing</say>
</gather>