TaCode Tuesday: How to Record and Transcribe Calls Using Zang APIs

Posted by Pedram Mohammadi on March 28, 2017

For e-commerce companies who have in-house call centers, investing in an enterprise grade interactive voice response (IVR) system doesn’t make much sense. Throwing thousands of dollars into a solution that swallows a huge chunk of the annual budget isn’t practical, especially if their customer base is just starting to build.

However, just because you’re a start-up doesn’t mean you have to compromise quality. If you are like many others who either subscribe to a cloud-based IVR or maintain a home-grown system, you can extend this to include a call recording or transcribing functionality which is standard in high-scaled and enterprise-grade call centers.

TACODE TUESDAY: HOW TO RECORD AND TRANSCRIBE CALLS USING ZANG APIS | http://zang.io 

Why Record and Transcribe Calls?

There are five reasons why businesses would want to record and transcribe calls with customers:

  1. It is a good way to identify key areas of improvement of your customer service representatives;
  2. It is a perfect capturing and storing mechanism of all customer cases. Recordings can be revisited when something is missed or disputed, even after a phone conversation has long concluded;
  3. You get to know customers better by revisiting calls to derive common issue patterns and satisfaction triggers;
  4. New product and services ideas can come from a simple purchase dispute or special handling of a case. Analyzing call recordings can help you get that discovery; and
  5. Finally, call recordings can be a good defense in the unfortunate event of litigation.

In this week’s TaCode Tuesday, we will teach you how to record and transcribe calls using Zang’s Record and Transcribe APIs. This is a good way to extend your already existing IVR system to automatically record calls upon customer consent and consequently save a transcription which can be stored and reviewed serving a valuable source of customer insight.

 

Requirements

  • Intermediate knowledge of:
    • NET technology
    • SQL Lite
  • Zang Subscription
  • Working knowledge of Zang APIs

 

Step 1: Set-up your Zang Phone Number

When you register with Zang, you automatically get $3 free credits. In order to make a call, you need to purchase a Zang phone number which starts for as low as $1/month.  If you already have an existing number, this should work fine as well.

 

Step 2: Create a Database Table named "Call Records and Transcript". The Call Records column must have the following attributes:

  • call_sid
  • date_created
  • date_updated
  • recording_url.

Call Transcript column on the other hand must have:

  • recording_sid
  • date_created
  • date_updated
  • status
  • type
  • audio_url
  • transcription_text
  • transcribe_callback
  • callback_method

There are also common fields that both Call Records and Call Transcript share like:

  • Sid
  • account_sid
  • duration

 

Step 3: Set business rules that will define your call recording and transcribing attributes such as:

  • Record - Specifies if a call recording should start or end. Allowed values are "true" to start recording and "false" to end recording. Any number of simultaneous, separate recordings can be initiated.
  • Direction - Specifies which audio stream to record. Allowed values are "in" to record the incoming caller's audio, "out" to record the outgoing caller's audio, and "both" to record both
  • TimeLimit - The maximum duration of the recording. Allowed value is an integer greater than 0.
  • CallbackUrl - A URL that will be requested when the recording ends, sending information about the recording. The longer the recording, the longer the delay in processing the recording and requesting the CallbackUrl. URL length is limited to 200 characters.
  • FileFormat - Specifies the file format of the recording. Allowed values are "mp3" or "wav" - any other value will default to "mp3."
  • TrimSilence - Trims all silence from the beginning of the recording. Allowed values are "true" or "false" - any other value will default to "false."
  • Transcribe - Specifies if this recording should be transcribed. Allowed values are "true" and "false" - all other values will default to "false."
  • TranscribeQuality - Specifies the quality of the transcription. Allowed values are "auto" for automated transcriptions and "hybrid" for human-reviewed transcriptions - all other values will default to "auto."
  • TranscribeCallback - A URL that will be requested when the call ends, sending information about the transcription. The longer the recording, the longer the delay in processing the transcription and requesting the TranscribeCallback. URL length is limited to 200 characters.

 

Step 4: Write your call recording code.

 private static void RecordCall()

       {

           var httpWebRequest = (HttpWebRequest)WebRequest.Create("{https://api.zang.io/v2/Accounts/:AccountSid/Calls/:CallSid/Recordings.json}");

           httpWebRequest.ContentType = "application/json";

           httpWebRequest.Method = "POST";

           using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))

           {         // building JSON string manually from strings

               string json = "{\"sid\":\"{CallSid}\"," +

                 "\"date_created\":\"Tue, 24 Jul 2012 14:08:23 +0000"}" + "," +

                 "\"date_updated\":\"Tue, 24 Jul 2012 14:08:24 +0000\"}" + "," +

                 "\"parent_call_sid\":\"\"}" + "," +

                 "\"account_sid"\":\"{AccountSid}"}" + "," +

                 "\"to\":\"+17325551234\"}" + "," +

                 "\"from\":\"+14245551234\"}" + "," +

                 "\"phone_number_sid\": \"{phone_number_sid}\" + "," +

                 "\"status\":\"sent\"}" + "," +

                 "\"start_time\": \"{Tue, 24 Nov 2016 14:56:32 +0000}\" + "," +

                 "\"end_time\": \"{Tue, 24 Nov 2016 14:56:35 +0000}\" + "," +

                 "\"duration\": \"3\" + "," +

                 "\"price\": \"0.020000\" + "," +

                 "\"direction\": \"outbound-api\" + "," +

                 "\"answered_by\": \"None\" + "," +

                 "\"api_version\": \"v2\" + "," +

                 "\"forwarded_from\": \"\" + "," +

                 "\"p_asserted_identity\": \"\" + "," +

                 "\"sip_privacy\": \"\" + "," +

                 "\"privacy_hide_number\": \"False\" + "," +

                 "\"date_sent\":\"{Tue, 24 Nov 2016 14:08:24 +0000"\"}" +

                "\"body\":\"{You have reach ..\"}" + "," +

                 "\"status\":\"{sent\"}" + "," +

                 "\"price\":\"{0.070000"}" + "," +

                 "\"uri\":\"{/v2/Accounts/{AccountSid}/Calls/{CallSid}.json\"}" + "," +

                 "\"subresource_uris\": {

                 "\"notifications\": \"/v2/Accounts/{AccountSid}/Calls/{CallSid}/Notifications.json\"

                 "\"recordings\": \"/v2/Accounts/{AccountSid}/Calls/{CallSid}/Recordings.json\"}";

 

               streamWriter.Write(json);

               streamWriter.Flush();

               streamWriter.Close();

           }

 

           var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

           using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))

           {

               var result = streamReader.ReadToEnd();

           }

       }

 

Step 5: Write your call transcription code.

  private static void Transcribe(int TranscriptionSid)

       {

           var httpWebRequest = (HttpWebRequest)WebRequest.Create("{https://api.zang.io/v2/Accounts/:AccountSid/Recordings/:RecordingSid/Transcriptions.json}");

           httpWebRequest.ContentType = "application/json";

           httpWebRequest.Method = "POST";

           using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))

           {    //using the built in C# JavascriptSerialiser class

               var TranscribeItems = new List<Transcribe>();

               //create an object of Transcribe class and fill up the values

               TranscribeItems.Add(new Transcribe() { sid = TranscriptionSid, date_created = '{Fri, 27 Jul 2012 15:31:53 +0000"}',

                   date_updated ='{Fri, 27 Jul 2012 15:32:28 +0000}' ,account_sid = '{AccountSid}',

                   status = 'completed',type = 'auto',

                   audio_url = '',duration = '2',

                   transcription_text = '',api_version = 'v2',

                   price = '0.050000',transcribe_callback = '',

                   callback_method = 'POST', uri ='/v2/Accounts/{AccountSid}/Transcriptions/{TranscriptionSid}.json'});

               var serializer = new JavaScriptSerializer();

               var serializedResult = serializer.Serialize(TranscribeItems);

               streamWriter.Write(json);

               streamWriter.Flush();

               streamWriter.Close();

           }

 

           var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

           using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))

           {

               var result = streamReader.ReadToEnd();

           }

       }

 

Step 6: View your call recording and save the information.

 private static void ViewRecording()

       {

           var httpWebRequest = (HttpWebRequest)WebRequest.Create("{https://api.zang.io/v2/Accounts/:AccountSid/Recordings/:RecordingSid.json }");

           httpWebRequest.ContentType = "application/json";

           httpWebRequest.Method = "POST";

           using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))

           {    //using the builtin C# JavascriptSerialiser class

               var ViewRecordingItems = new List<ViewRecording>();

               //create an object of Transcribe class and fill up the values

              /*template to build your JSON string

              ViewRecordingItems.Add(new Transcribe() { sid = TranscriptionSid, date_created = '{Fri, 27 Jul 2012 15:31:53 +0000"}',

                   date_updated ='{Fri, 27 Jul 2012 15:32:28 +0000}' ,account_sid = '{AccountSid}',

                   status = 'completed',type = 'auto',

                   audio_url = '',duration = '2',

                   transcription_text = '',api_version = 'v2',

                   price = '0.050000',transcribe_callback = '',

                   callback_method = 'POST', uri ='/v2/Accounts/{AccountSid}/Transcriptions/{TranscriptionSid}.json'});

               */

               var serializer = new JavaScriptSerializer();

               var serializedResult = serializer.Serialize(TranscribeItems);

               streamWriter.Write(json);

               streamWriter.Flush();

               streamWriter.Close();

           }

 

           var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

           using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))

           {

               var result = streamReader.ReadToEnd();

           }

       }

 

Step 7: View your call transcription and save the information.

private static void ViewTranscription()

       {

           // refer to method Transcribe and using the ViewTranscription class of the //TranscribeApplication namespace in step 6 create an object out of it and pass it through

           // the Zang api https://api.zang.io/v2/Accounts/:AccountSid/Transcriptions/:TranscriptionSid.json

           //... implementation goes here

       }

 

Step 8: Create your custom C# namespace for mapping the getter and setter properties of Transcribe, ViewRecording and ViewTranscription classes.

using  System;

namespace TranscribeApplication

 {

   public class Transcribe

   {

       public int sid{ get; set; }

       public Date date_created { get; set;}

       public Date date_updated {get; set;}

       public int account_id {get; set;}

       public string status {get; set;}

       public string type {get; set;}

       public string audio_url {get; set;}

       public string transcription_text {get; set;}

       public string api_version {get; set;}

       public float price {get; set;}

       public string transcribe_callback {get; set;}

       public string callback_method {get; set;}

       public string uri {get; set;}

       

   }

 

   public class ViewRecording

   {

      public int sid { get; set; }

      public int account_sid { get; set; }

      public int call_sid { get; set; }

      public int duration { get; set; }

      public Date date_created { get; set; }

      public string api_version { get; set; }

      public Date date_updated { get; set; }

      public string recording_url { get; set; }

      public string uri { get; set; }

   }

 

   public class ViewTranscription

   {

       public int sid { get; set; }

       public Date date_created { get; set;}

       public Date date_updated {get; set;}

       public int account_sid { get; set; }

       public string status {get; set;}

       public string type {get; set;}

       public string audio_url {get; set;}

       public int duration {get; set;}

       public string transcription_text {get; set;}

       public string api_version { get; set; }

       public float price {get; set;}

       public string transcribe_callback {get; set;}

       public string callback_method {get; set;}

       public string uri {get; set;}

   }

}

 

Step 9: Using the terminal window, show all recording and transcription.

private static ListViewRecording()

       {

            const string connectionString = "URI=file:SqliteTest.db";

           IDbConnection dbcon = new SqliteConnection(connectionString);

           dbcon.Open();

           IDbCommand dbcmd = dbcon.CreateCommand();

    

           const string sql =

               "SELECT * FROM table_recording WHERE 1";

   

           dbcmd.CommandText = sql;

           IDataReader reader = dbcmd.ExecuteReader();

           var columns;

              while(reader.Read())

           {

               string sid = reader.GetString(0);

               string account_sid = reader.GetString(1);

               string call_sid = reader.GetString(2);

               string duration = reader.GetString(3);

               string date_created = reader.GetString(4);



               Console.WriteLine("Columns: {0} {1} {2} {3} {4}",

               sid, account_sid, call_sid,duration,date_created);

               

           }

           reader.Dispose();

           dbcmd.Dispose();

           dbcon.Close();

       }

 

Step 10: Using the terminal window, show all transcriptions.

private static ListViewTranscription()

       {

            const string connectionString = "URI=file:SqliteTest.db";

           IDbConnection dbcon = new SqliteConnection(connectionString);

           dbcon.Open();

           IDbCommand dbcmd = dbcon.CreateCommand();

    

           const string sql =

               "SELECT * FROM table_transcription WHERE 1";

   

           dbcmd.CommandText = sql;

           IDataReader reader = dbcmd.ExecuteReader();

           var columns;

              while(reader.Read())

           {

               string sid = reader.GetString(0);

               string account_sid = reader.GetString(1);

               string call_sid = reader.GetString(2);

               string duration = reader.GetString(3);

               string date_created = reader.GetString(4);



               Console.WriteLine("Columns: {0} {1} {2} {3} {4}",

               sid, account_sid, call_sid,duration,date_created);

               

           }

           reader.Dispose();

           dbcmd.Dispose();

           dbcon.Close();

       }

 

Step 11: Call methods from Steps 4 to 10, and you’re done!

 static void Main(string[]args)

       {

           TcpClient tcpClient = new TcpClient();

           try

           {

               tcpClient.Connect("{ZANG IP ADDRESS FOR DEVELOPERS", 22);

               Console.WriteLine("Port open");

               https://api.zang.io/v2/Accounts/{AccountSid}

               var client = new HttpClient();

               // Create the HttpContent for the form to be posted.

               var requestContent = new FormUrlEncodedContent(new [] {

               new KeyValuePair<string, string>("AccountSid", "{AccountSid}"),

               });

               // Get the response.

               HttpResponseMessage response = await client.PostAsync(

               "https://api.zang.io/v2/Accounts/",

               requestContent);

               // Get the response content.

               HttpContent responseContent = response.Content;

               // Get the stream of the content.

               using (var reader = new StreamReader(await   

               responseContent.ReadAsStreamAsync()))

               {

               // Write the output.

                   Console.WriteLine(await reader.ReadToEndAsync());

                   //call method from step 4 to 10

               }

           }

           catch (Exception)

           {

               Console.WriteLine("Port closed");

           }

       }

 

So, that’s it for this week’s tutorial!

Remember, it is one thing to record and transcribe calls with customers, but how you analyze and act on the insights you derived from the data is more important.

Zang’s record and transcribe APIs will help you automatically store and sift through the data, but your reporting analytics should be as powerful to enable interpretation from large data sets. Some companies use big data as a service (BaaS) to analyze huge chunks of information and derive insights out of it. Otherwise, you can implement your own algorithm to browse through text transcriptions and generate a business intelligence report. If you need help starting out on this, our IT support can help.

Start building on Zang

Topics: APIs, TaCode Tuesday, cPaaS

IMAGINE IT. BUILD IT.

Communicate better. 

Cloud communication technology that transforms your conversations — and your business.

 

Subscribe to Email Updates