Twilio不要暂停输入

时间:2016-01-19 23:22:20

标签: c# visual-studio-2015 asp.net-web-api2 asp.net-core twilio

我无法让Twilio进行聚会。调用初始化很好,但不是等待用户按键,Gather会直接进入下一个语句并挂起。

我的环境是Visual Studio 2015..NET 4.6,MVC6,asp.net5。我安装了RC1更新。 Nuget包是Twilio版本4.4.1,Twilio.TwiML 3.3.6。

这是一个测试WebAPI 2控制器:

[Route("api/[controller]")]
public class OutboundCallController : Controller
{
    [HttpPost]
    public IActionResult Post()
    {
        var twilioResponse = new TwilioResponse();
        twilioResponse.BeginGather(new { timeout = "60", numDigits = "1", action = "Foo", method = "POST" });
        twilioResponse.Say("Test Message Here");
        twilioResponse.EndGather();
        twilioResponse.Say("Fallthrough.  Goodbye.");
        return new ObjectResult(twilioResponse.ToString());
    }
}

当Twilio收到以下数据时,它表示"测试此处的消息。再见&#34。一下子,没有停顿,然后立即挂断。

使用ngrok我可以看到对我的控制器的Twilio POST的响应是:

<Response>
  <Gather timeout="60" numDigits="1" action="Foo" method="POST">
    <Say>Test Message Here</Say>
  </Gather>
  <Say>Fallthrough.  Goodbye.</Say>
</Response>

此外,我的Twilio日志看起来像(相同):

<Response>
  <Gather timeout="60" numDigits="1" action="Foo" method="POST">
    <Say>Test Message Here</Say>
  </Gather>
  <Say>Fallthrough.  Goodbye.</Say>
</Response>

编辑:

我还尝试更改WebAPI以返回字符串而不是IActionResult。什么都没有变化,结果相同。

[HttpPost]
    public string Post()
    {....}

解答:

原来我没有返回正确的内容类型,我修改了POST操作的返回,完整的控制器代码如下:

[Route("api/[controller]")]
public class OutboundCallController : Controller
{
    [HttpPost]
    public IActionResult Post()
    {
        var twilioResponse = new TwilioResponse();
        twilioResponse.BeginGather(new { timeout = "60", numDigits = "1", action = "Foo", method = "POST" });
        twilioResponse.Say("Test Message Here");
        twilioResponse.EndGather();
        twilioResponse.Say("Fallthrough.  Goodbye.");
        return Content(twilioResponse.ToString(), "application/xml");
    }
}

1 个答案:

答案 0 :(得分:2)

Twilio开发者传道者在这里。

我刚刚将您生成的TwiML复制到twimlbin,并且能够让它正常停顿并且无需跳过。

以下是your generated TwiML的精确副本。

显然,当您按下某个数字时,它会失败,因为该操作仅设置为foo。如果您将该操作设置为其他内容(如下面的示例),您将看到在按下数字时,您还应该收到一条消息“Hi there”

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Gather timeout="60" numDigits="1" action="http://twimlets.com/echo?Twiml=%3CResponse%3E%3CSay%3EHi+there.%3C%2FSay%3E%3C%2FResponse%3E" method="POST">
    <Say>Test Message Here</Say>
  </Gather>
  <Say>Fallthrough.  Goodbye.</Say>
</Response>

此外,您的C#代码看起来正确,但让我知道您希望将您的端点公开,以便我可以对其进行测试。值得检查的是,您返回Twilio的确是XML(即顶部有<?xml version="1.0" encoding="UTF-8"?>),其内容类型实际上是XML。

很乐意帮助解决任何其他问题。