Twilio增强答录机检测c#

Twilio enhanced answering machine detection c#

如何通过 Twilio 使用增强型 AMD?我知道它只能通过 REST API(没有 TwiML)来完成,但我很难看到标准呼叫和应答机检测之间的联系。

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"));

这是我从上述 link:

翻译过来的代码
        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 如何知道要听什么电话,以及这里的事件顺序应该是什么。最后,我应该如何查看结果?

编辑:

为了crystal清楚,这是我目前的代码:

            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" 函数。

您可以试试 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());
                }
            }

这里是 Twilio 开发人员布道者。

您看起来 making a call 检测成功。您正在使用 Twilio 号码拨打用户号码。

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

当 Twilio 制作 webhook 时,它会包含一个额外的参数:AnsweredBy。当你设置MachineDetectionDetectMessageEnd时,the values of AnsweredBy可以是:machine_end_beepmachine_end_silencemachine_end_otherhumanfax,以及 unknown。然后您可以读取此值并决定此时如何处理调用。

这有什么帮助吗?