繁体   English   中英

Twilio在ASP.NET-MVC中尝试创建可用的VoIP电话失败,因为在网站上只能看到XML而不是电话

[英]Attempt to create usable VoIP phone by Twilio in ASP.NET-MVC fails as Only XML is visible on the website instead of phone

我一直在寻找ASP.NET-MVC中的VoIP技术。 我唯一发现的是Twilio。

我创建了ASP.NET-MVC 5.2应用程序,并添加了对以下内容的引用:

  • Twilio.Api
  • Twilio.Mvc
  • Twilio.Twiml

我将_Layout.csthml减少到最小:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
        @RenderBody()
</body>
</html>

我发现了以下示例: https : //www.twilio.com/docs/howto/phonemenu我相信它应该会在我的网站中生成电话菜单。

我将动作添加到HomeController

控制器的动作:

 public ActionResult PhoneMenu() {
        return View();
    }

我从下载的指南内容 https://www.twilio.com/resources/tarball/phonemenu.zip并粘贴到:然后PhoneMenu.csthml把它添加到Views/Home/在我的项目。

@{

    @*@start snippet*@
    @* Define Menu *@
    Dictionary<string, string[]> web = new Dictionary<string, string[]>() {
        {"default",new string[] {"receptionist","hours", "location", "duck"} },
        {"location",new string[] {"receptionist","east-bay", "san-jose", "marin"} }
    };

    Uri cleanedUri = new Uri(Request.Url.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped));
    string baseUri = cleanedUri.AbsoluteUri.Remove(cleanedUri.AbsoluteUri.Length - cleanedUri.Segments.Last().Length);

    @* Get the menu node, index, and url *@
    string node = Request["node"] != null ? Request["node"] : "";
    int index = Request["Digits"] != null ? int.Parse(Request["Digits"]) : 0;

    string url = string.Format("{0}phonemenu.cshtml", baseUri);

    @* Check to make sure index is valid *@
    string destination = string.Empty;
    if (web.Keys.Contains(node) && web[node].Length >= index && Request["Digits"] != null) {
        destination = web[node][index];
    } else {
        destination = string.Empty;
    }
    @*end snippet*@

    @*start snippet*@
    @* Render TwiML *@
    Response.ContentType = "text/xml";
    var twiml = new Twilio.TwiML.TwilioResponse();

    switch (destination) {
        case "hours":
            twiml.Say("Initech is open Monday through Friday, 9am to 5pm");
            twiml.Say("Saturday, 10am to 3pm and closed on Sundays");
            break;
        case "location":
            twiml.Say("Initech is located at 101 4th St in San Francisco California");
            twiml.BeginGather(new { action = "phonemenu.cshtml?node=location", numDigits = "1" })
                .Say("For directions from the East Bay, press 1")
                .Say("For directions from San Jose, press 2");
            twiml.EndGather();
            break;
        case "east-bay":
            twiml.Say("Take BART towards San Francisco / Milbrae. Get off on Powell Street. Walk a block down 4th street");
            break;
        case "san-jose":
            twiml.Say("Take Cal Train to the Milbrae BART station. Take any Bart train to Powell Street");
            break;
        case "duck":
            twiml.Play("duck.mp3");
            break;
        case "receptionist":
            twiml.Say("Please wait while we connect you");
            twiml.Dial("NNNNNNNNNN");
            break;
        default:
            twiml.BeginGather(new { action = "phonemenu.cshtml?node=default", numDigits = "1" })
                .Say("Hello and welcome to the Initech Phone Menu")
                .Say("For business hours, press 1")
                .Say("For directions, press 2")
                .Say("To hear a duck quack, press 3")
                .Say("To speak to a receptionist, press 0");
            twiml.EndGather();
            break;
    }
    @*end snippet*@

    @*start snippet*@
    if (destination != String.Empty && destination != "receptionist") {
        twiml.Pause();
        twiml.Say("Main Menu");
        twiml.Redirect(url);
    }
    @*end snippet*@
}
@Html.Raw(twiml.ToString())

当我转到URL: http://localhost:43281/Home/PhoneMenu是生成的xml而不是电话菜单:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link href="/Content/bootstrap.css" rel="stylesheet"/>
    <link href="/Content/site.css" rel="stylesheet"/>    
    <script src="/Scripts/modernizr-2.6.2.js"></script>    
</head>
<body>



<Response>
  <Gather action="phonemenu.cshtml?node=default" numDigits="1">
    <Say>Hello and welcome to the Initech Phone Menu</Say>
    <Say>For business hours, press 1</Say>
    <Say>For directions, press 2</Say>
    <Say>To hear a duck quack, press 3</Say>
    <Say>To speak to a receptionist, press 0</Say>
  </Gather>
</Response>    
</body>
</html>

可见结果:

在此处输入图片说明 问题:如何生成可用电话? 由于我找不到任何来源来做到这一点,因此,如果有比Twilio更容易的替代方案,那就太好了。

Twilio TwiML代不需要进入Razor视图,而只需使用控制器即可。 因此,您可能会有类似以下内容的信息:

public ActionResult PhoneMenu() {
    var response = new TwimlResponse().Say("Hello"):
    return new TwimlResult(response);
}

或者,作为Twilio.Mvc的一部分,您可以选择继承自另一个控制器,称为TwilioController,这样您就可以执行以下操作:

public class PhoneMenuController : TwilioController
{
    public ActionResult PhoneMenu()
    {
        var response = new TwimlResponse().Say("Hello");
        return Twiml(response);
    }
}

完整文档可在GitHub上找到, 网址https://github.com/twilio/twilio-csharp/

暂无
暂无

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

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