I am building a Twilio IVR using WebAPI and hit a bit of a snag. I am trying to loop through the recordings and at the end of each one offer the option to press 1 to delete the recording.
I can't find any examples anywhere of this in a C# WebAPI implementation and the following won't work and I am not sure how to go about this.
What I know;
- Need to GATHER the digits entered
- Somehow pass back the recording sid to the api to know which recording to delete.
Here is the code I've written so far:
public HttpResponseMessage Messages() {
string baseUrl = Url.Request.RequestUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
var twilio = new TwilioRestClient(_accountSid, _authToken);
var recordings = twilio.ListRecordings(null, DateTime.Today, null, null);
var twilioResponse = new TwilioResponse();
if (recordings != null && recordings.Recordings.Count > 0)
{
var msgCount = 1;
var msgTotal = recordings.Recordings.Count();
foreach (var recording in recordings.Recordings)
{
var caller = twilio.GetCall(recording.CallSid);
var callerNumber = Regex.Replace(caller.From, @"([0-9]{1})", "$1,");
var callDate = recording.DateCreated.ToString("dddd MMMM d");
var callTime = recording.DateCreated.ToString("h m t");
twilioResponse.Say(string.Format("Playing message {0} of {1}, from {2} on {3} at {4} M.", msgCount, msgTotal, callerNumber, callDate, callTime),
new { voice = "woman" });
var voiceFile = string.Format("{0}2010-04-01/Accounts/{1}/Recordings/{2}.mp3", twilio.BaseUrl, _accountSid, recording.Sid);
twilioResponse.Play(voiceFile);
//twilioResponse.BeginGather(new
// {
// action = baseUrl + "/api/Recording/Delete",
// numDigits = 1,
// });
//twilioResponse.Say("TO DELETE THIS MESSAGE PRESS 1");
//twilioResponse.EndGather();
msgCount++;
}
}
return Request.CreateResponse(HttpStatusCode.OK, twilioResponse.Element, Configuration.Formatters.XmlFormatter);
}
I've worked with Twilio's API before but this is a new one for me and I can't really see if this is possible. The PHP example they have is doing it though so maybe I am simply missing the mark because I should be going a different way about this.