代码之家  ›  专栏  ›  技术社区  ›  bkmo

Twilio增强应答机检测c#

  •  0
  • bkmo  · 技术社区  · 7 年前

    我已经通读了 this page 有几次,但我还是不明白。下面是通过REST API调用的标准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"));
    

            using (var client = new HttpClient())
            {
                var byteArray = Encoding.ASCII.GetBytes($@"{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($@"{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”的函数,它获取“AnsweredBy”参数,并在调用后执行一些操作。这样行吗?因为它不是。调用通过了,我可以听到Twilio的示例文件播放,但它从未到达“PostTransfer”函数。

    2 回复  |  直到 7 年前
        1
  •  0
  •   philnash    7 年前

    Twilio开发者布道者。

    你看起来像是 making a call 检测成功。您正在从您的Twilio号码拨打用户号码。

    makes a webhook request to your URL 您在拨打电话时发送的信息。

    当Twilio制作webhook时,它将包括一个额外的参数: AnsweredBy MachineDetection DetectMessageEnd the values of AnsweredBy machine_end_beep , machine_end_silence , machine_end_other human fax unknown . 然后,您可以读取该值并决定此时如何处理调用。

    这有帮助吗?

        2
  •  0
  •   Reedwanul Islam    7 年前

    你能试试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());
                    }
                }