2017-08-15 45 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()); 
       } 
      }