简体   繁体   English

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

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

The new Twilio 5x libraries have introduced a bit of an odd approach to gathering DTMF digits on phonecalls. 新的Twilio 5x库引入了一些奇怪的方法来收集手机上的DTMF数字。

The old 4x code for a gather would have looked something like: 收集的旧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();

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: 但是在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")
        );

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. 在收集(...)之后,你有一个收集响应的短窗口,如果你在响应上设置超时,twiml将不会进入下一个说法,直到超时到期。

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. 编辑:澄清4xx用例,这样人们就可以理解为什么链接了。在这里不行。

edit: Some people below are suggesting chaining the .Say() verb right after .Gather(). 编辑:下面的一些人建议在.Gather()之后链接.Say()动词。

This actually doesn't behave as expected either. 这实际上也不像预期的那样。 This is the C# code. 这是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");

This is the resulting twiml: 这是由此产生的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. 说动词需要在聚集标签内,以获得我们正在寻找的行为。

Okay I've found the issue. 好的,我发现了这个问题。 It looks like fourwhey was correct to point at the API docs there. 看起来像四个是正确的指向那里的API文档。 What I didn't catch was that the .Say was being appended to the gather in a specific way. 我没有注意到的是.Say以特定方式附加到聚集上。

This: 这个:

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

doesn't work the same as this: 与此不一样:

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

best I can work out is that there's actually two gather methods, and that twiml.Gather(...) is actually calling Twilio.TwiML.Gather. 我能解决的最好的是,实际上有两种聚集方法,而且twiml.Gather(...)实际上是在调用Twilio.TwiML.Gather。

From here we can build our dynamic voice message and nest the Say verb like so: 从这里我们可以构建我们的动态语音消息并嵌套Say动词,如下所示:

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

And the twiml will get spit out the way we intended like so: 而twiml会像我们想要的那样吐出来:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM