How exactly do I use the enhanced AMD through Twilio? I understand that it can only be done through the REST API (no TwiML) but I'm having a hard time seeing the connection between a standard call and the answering machine detection.
I've read through this page a few times, but I still don't understand. So here is the standard c# code for placing a call through the REST API:
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"));
And here is my c# translated code from the aforementioned 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;
}
So what am I missing? I'm sure it's something simple, but I'm not seeing how the enhanced AMD knows what call to listen to, and what the order of events here should be. And finally, how am I supposed to see the results?
EDIT:
So to be crystal clear, here is my code as it currently sits:
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;
}
And elsewhere in my code is a function called "PostTransfer" that gets the "AnsweredBy" parameter and does some stuff after the call is placed. Should this be working? Because it isn't. The call goes through and I can hear Twilio's example file play, but it never gets to the "PostTransfer" function.
Twilio developer evangelist here.
You look like you're making a call with detection successfully. You're making a call from your Twilio number to a users number.
You see the results of the answering machine detection when Twilio has decided on whether it is a machine or a human, at which point it makes a webhook request to your URL that you send as part of making the call.
When Twilio makes the webhook, it will include an extra parameter: AnsweredBy
. When you set MachineDetection
to DetectMessageEnd
the values of AnsweredBy
can be: machine_end_beep
, machine_end_silence
, machine_end_other
, human
, fax
, and unknown
. You can then read this value and decide what to do with the call at this point.
Does that help at all?