Twilio增强的答录机检测c#

时间:2017-08-15 12:12:41

标签: c# twilio amd

我究竟如何通过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"));

这是我在上述链接中的c#翻译代码:

        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;
        }

我的代码中的其他地方是一个名为&#34; PostTransfer&#34;获得&#34; AnsweredBy&#34;参数并在拨打电话后做一些事情。这应该有用吗?因为它不是。通话结束,我可以听到Twilio的示例文件播放,但它永远不会进入&#34; PostTransfer&#34;功能。

2 个答案:

答案 0 :(得分: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());
                }
            }

答案 1 :(得分:0)

Twilio开发者传道者在这里。

您似乎已成功检测到making a call。您正在通过Twilio号码拨打用户号码。

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

当Twilio制作webhook时,它将包含一个额外的参数:AnsweredBy。将MachineDetection设置为DetectMessageEnd the values of AnsweredBy时,可以是:machine_end_beepmachine_end_silencemachine_end_otherhuman,{{1} }和fax。然后,您可以读取此值并决定此时如何处理呼叫。

这有帮助吗?