2017-08-15 47 views
0

我到底如何通过Twilio使用增强型AMD?我知道它只能通过REST API(没有TwiML)完成,但我很难看到标准呼叫和应答机检测之间的连接。我看过this page几次,但我还是不明白。所以在这里是通过REST API发出呼叫的标准的C#代码:Twilio增强型应答机检测c#

TwilioClient.Init(AccountSid, AuthToken); 

    var to = new PhoneNumber("+14155551212"); 
    var from = new PhoneNumber("+15017250604"); 
    var call = CallResource.Create(to, from, url: new Uri("http://demo.twilio.com/docs/voice.xml")); 

,这里是我的C#从上述链接转换代码:

 using (var client = new HttpClient()) 
     { 
      var byteArray = Encoding.ASCII.GetBytes([email protected]"{AccountSid}:{AuthToken}"); 
      var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); 
      client.DefaultRequestHeaders.Authorization = header; 

      var requestContent = new FormUrlEncodedContent(new[] 
                  { 
                   new KeyValuePair<string, string>("To", "+15017250604"), 
                   new KeyValuePair<string, string>("From", "+15017250604"), 
                   new KeyValuePair<string, string>("MachineDetection", "DetectMessageEnd"), 
                   new KeyValuePair<string, string>("Url", Url.Action("PostTransfer")) 
                  }); 

      var response = client.PostAsync(_amdRequest, requestContent); 
      var responseContent = response.Result.Content; 
     } 

所以我缺少什么?我相信这很简单,但我没有看到增强型AMD知道如何呼叫,以及这里的事件顺序应该是什么。最后,我该如何看待结果?

编辑:

所以要一清二楚,这里是我的代码,因为它目前是:

  TwilioClient.Init(AccountSid, AuthToken); 

     var toPhone = new PhoneNumber(to); 
     var fromPhone = new PhoneNumber(from); 
     var call = CallResource.Create(toPhone, fromPhone, url: new Uri("http://demo.twilio.com/docs/voice.xml")); 

     using (var client = new HttpClient()) 
     { 
      var byteArray = Encoding.ASCII.GetBytes([email protected]"{AccountSid}:{AuthToken}"); 
      var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); 
      client.DefaultRequestHeaders.Authorization = header; 

      var requestContent = new FormUrlEncodedContent(new[] 
                  { 
                   new KeyValuePair<string, string>("To", to), 
                   new KeyValuePair<string, string>("From", from), 
                   new KeyValuePair<string, string>("MachineDetection", "DetectMessageEnd"), 
                   new KeyValuePair<string, string>("Url", Url.Action("PostTransfer")) 
                  }); 

      var response = client.PostAsync(_amdRequest, requestContent); 
      var responseContent = response.Result.Content; 
     } 

在我的代码的其他地方是一个叫做“PostTransfer”功能,获得了“作答”参数并在通话结束后执行一些操作。这应该工作吗?因为它不是。通话过程中,我可以听到Twilio的示例文件播放,但它永远不会进入“PostTransfer”功能。

回答

0

Twilio开发人员传道这里。

您看起来像是making a call,检测成功。您正在通过Twilio号码拨打用户号码。

当Twilio决定它是机器还是人类时,您会看到应答机检测的结果,此时您拨打makes a webhook request to your URL作为拨打电话的一部分。

当Twilio制作webhook时,它将包含一个额外的参数:AnsweredBy。当您设置MachineDetectionDetectMessageEndthe values of AnsweredBy可以是:machine_end_beepmachine_end_silencemachine_end_otherhumanfaxunknown。然后,您可以读取该值,并决定在这一点上如何处理此呼叫。

这有帮助吗?

+0

我编辑了一些原始问题。我无法知道应答机检测是否出现问题,因为它从未到达我提供的网址末尾的功能。 – bkmo

+0

啊,我相信,从检查[文档](https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v = vs.118).aspx),那'URL.Action'返回一个路径,Twilio需要一个完整的URL和域名等。 – philnash

+0

这样做。我以前只能获得部分网址。 – bkmo

0

你可以试试HttpWebRequest吗?这里是一个例子如何做到这一点,

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); 
request.Method = "POST"; 
request.Headers.Add("Authorization", string.Format("Bearer {0}",AccessToken)); 
request.ContentType = "application/json;charset=utf-8"; 
request.ContentLength = body.Length; 
request.Accept = "application/json" 

if (!string.IsNullOrWhiteSpace(body)) 
      { 
       System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); 
       byte[] bytes = encoding.GetBytes(body); 

       request.ContentLength = bytes.Length; 

       using (Stream requestStream = request.GetRequestStream()) 
       { 
        // Send the data. 
        requestStream.Write(bytes, 0, bytes.Length); 
       } 
      } 

      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       if (callback != null) 
       { 
        var reader = new StreamReader(response.GetResponseStream()); 
        callback(reader.ReadToEnd()); 
       } 
      }