How To Save a Recording in Tropo using ASP.NET MVC

I was having trouble getting recordings to save while adding the Tropo integration to VoiceModel and I could not find any good examples on how to do this.  Even the examples for TropoCSharp did not show how to save the recordings.  I was able to figure out a solution so I thought I would share it here since there is a lot of requests on the forums to see an example for C# and .NET.  This example was developed using ASP.NET MVC 4.

I thought I could reuse the same action on the VoiceModel controller to save recordings in VoiceXML applications, but it turned out that was not the case. When saving a recording in VoiceXML the voice browser wants the next VoiceXML document when it is completed, which is not the case for Tropo.  The only response that Tropo is looking for is whether it gets an HTTP 200 success code or not.

The other issue is that the MVC controller action wants to match the name in the multipart form data  request with name given for the HttpPostedFileBase object.  Let me show you the code for saving the recording to make this clearer.

        [HttpPost]
        public string SaveRecordingTropo(HttpPostedFileBase filename)
        {
            string msg = "Successfully saved recording.";
            if (filename != null && filename.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(filename.FileName);
                // store the file inside ~/App_Data/recordings folder
                var path = Path.Combine(Server.MapPath(recordingPath), fileName);
                _log.Debug("Received recording and will save as " + path);
                // save the audio file
                filename.SaveAs(path);
            }
            else
            {
                msg = "Recording was null or empty.";
                if (filename == null)
                    _log.Debug("Received request to save recording but it is null.");
                else
                    _log.Debug("Received request to save recording but it is empty.");

            }

            return msg;
        }


The action method takes a parameter called HttpPostedFileBase which is defined as "filename".  In VoiceXML you can specify what the name of this object is. I thought I could specify it using the "name" attribute of the "record" object in Tropo but that would not work using  TropoCSharp.  So I setup WireShark to capture the POST request from Tropo and found that the name it used for the multipart form data is "filename".  Once I changed the name of HttpPostedFileBase to "filename" it started working. When I used a different name this object was always null.

The other thing to remember when setting this up is to make sure that the directory you use to store the save recordings must have write privileges for IIS_USER.  That is all there is to saving an audio recording from Tropo Web API using HTTP POST.  You can get a working example from VoiceModel by getting the source code for changeset 20477 or greater and running the RecordingExample project.  When you setup the application in Tropo you will set the URL to http://server-name/app-path/Record/StartTropo.

There is one variation on handling recordings in VoiceXML and Tropo that I need to work out. With VoiceXML you can listen to the recording while it is still on the VoiceXML platform server and it sticks around for the whole call.  This is nice because you can have the caller listen to the message and confirm that they want it before sending it to your application server. I cannot find an option for doing this on Tropo. I will keep you  posted on what solution I can come up with for handling this.

Comments

Popular posts from this blog

Using Claims in ASP.NET Identity

Seeding & Customizing ASP.NET MVC SimpleMembership

Customizing Claims for Authorization in ASP.NET Core 2.0