Question

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.

Was it helpful?

Solution

Twilio evangelist here.

What you have looks really close. Lets look at passing back the Recording SID in scenarios where the user presses one since thats relatively easy to do by using the action URL your setting on the Gather verb to hold some state for you:

action = baseUrl + "/api/Recording/Delete?recordingSid=" + recording.Sid;

Now when the user presses one, Twilio will make a request to the Action URL which includes the recordingSid parameter.

Once you've deleted the Recording, you can just redirect back to your Messages endpoint to continue to listen to more recordings. In order to keep track of which recordings you've listened to already, you might need to pass some additional parameters in the action URL that can you can pass through your delete workflow and back into the listen workflow.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top