Retrieving information about multiple call recordings using C#

Using Vonage Contact Center’s Conversation Analyzer API, you can retrieve data about conversations your agents had with your customers. Use the information in this guide to configure the sample code for your account.

To use the sample code, review the code and perform the following tasks:

When you have correctly configured the sample code, running the code should return a JSON response in the following format:

Conversation Analyzer (v2) response

[
	{
    "categories": [
        {
            "category": "Politeness",
            "categories": [
                "name": "greeting",
                "matches": 1
            ]
        }
    ],
    "guid": "0164705c-c206-dce9-d61a-9899855326a0",
    "status": "processed",
    "confidence": "Low",
    "callTimeBreakDown": {
        "agent": 5,
        "customer": 1,
        "silence": 4,
        "crossTalk": 0
    },
    "transcript": "Case.\nHi, and yeah, just just a call here. Thank you bye."
	},
	{
    ... record 2
	},
...
	{
    ... record n
	}
]

Conversation Analyzer (v3) response

[
	{
    "categories": [
        {
            "category": "Politeness",
            "categories": [
                "name": "greeting",
                "matches": 1
            ]
        }
    ],
    "guid": "0164705c-c206-dce9-d61a-9899855326a0",
    "language": "en-us",
    "status": "processed",
    "confidence": "Low",
    "callTimeBreakDown": {
        "agent": 5,
        "customer": 1,
        "silence": 4,
        "crossTalk": 0
    },
    "transcript": "Case.\nHi, and yeah, just just a call here. Thank you bye."
},
	{
    ... record 2
	},
...
	{
    ... record n
	}
]

Sample code

The following example includes the following files:

  • Program.cs Contains code that retrieves a bearer access token for authenticating the subsequent request for data about specified interactions. The code retrieves data for multiple interactions, including transcripts of the recordings.
  • App.config A corresponding configuration file containing values for the variables required by the code.

Program.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
  
namespace ApiGatewayTest
{
    class Program
    {
        public const string ConversationAnalyserResource = "/conversation-analyzer/interactions";
        private const string ConversationAnalyzerScope = "interaction-content:read";
  
        private static readonly HttpClient _httpClient = new HttpClient();
  
        public static string RequestToken(string identityServerUrl, string clientId, string clientSecret, IEnumerable<string> scopes)
        {
            var identityServerUri = new Uri(identityServerUrl);
            var content = new FormUrlEncodedContent(
                new[]
                {
                    new KeyValuePair<string, string>("grant_type", "client_credentials"),
                    new KeyValuePair<string, string>("client_id", clientId),
                    new KeyValuePair<string, string>("scope", string.Join(" ", scopes)),
                    new KeyValuePair<string, string>("client_secret", clientSecret)
                });
  
            var httpRequest = new HttpRequestMessage(HttpMethod.Post, identityServerUri) { Content = content };
  
            var httpResponse = _httpClient.SendAsync(httpRequest).Result;
            httpResponse.EnsureSuccessStatusCode();
  
            var responseBody = httpResponse.Content.ReadAsStringAsync().Result;
            dynamic tokenObject = JsonConvert.DeserializeObject(responseBody);
            return tokenObject.access_token;
        }
  
        public static string MakeRequest(string url, HttpMethod method, string token, int version = 1)
        {
            var request = new HttpRequestMessage();
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue($"application/vnd.newvoicemedia.v{version}+json"));
            request.RequestUri = new Uri(url);
            request.Method = method;
  
            var response = _httpClient.SendAsync(request).Result;
            return response.Content.ReadAsStringAsync().Result;
        }
  
        static void Main()
        {
            var ClientId = ConfigurationManager.AppSettings["ClientId"];
            var ClientSecret = ConfigurationManager.AppSettings["ClientSecret"];
            var IdentityServerUrlBase = ConfigurationManager.AppSettings["AuthURL"];
            var ApiGatewayUrl = ConfigurationManager.AppSettings["ApiURL"];
 
            // You can discover your call GUIDs via the Stats API
            var callGuids = ConfigurationManager.AppSettings["Guids"].Split(',');
  
            var token = RequestToken($"{IdentityServerUrlBase}/Auth/connect/token", ClientId, ClientSecret, new [] {ConversationAnalyzerScope});
            var result = MakeRequest($"{ApiGatewayUrl}{ConversationAnalyserResource}/{callGuids[0]}?transcript=true", HttpMethod.Get, token);
            Console.WriteLine(result);
            result = MakeRequest($"{ApiGatewayUrl}{ConversationAnalyserResource}?calls={String.Join(",",callGuids)}&transcript=true", HttpMethod.Get, token);
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
    </startup>
    <appSettings>
      <add key="AuthURL" value="{URL_FOR_AUTHORIZATION}"/>
      <add key="ApiURL" value="{URL_FOR_API}"/>
      <add key="ClientId" value="{CLIENT_ID}"/>
      <add key="ClientSecret" value="{CLIENT_SECRET}"/>
      <add key="Guids" value="{guid1},{guid2},{guid3},...,{guidN}"/>
    </appSettings>
</configuration>

Defining your variables in App.config

To use this example, you need to provide the following values in the App.config file:

Placeholder Value
{URL_FOR_AUTHORIZATION} The base URL for the API that Program.cs uses to authenticate your request. Replace the placeholder with the authorization URL for your location. For URLs, see Authorization and API URLs.
{URL_FOR_API} The base URL for the Conversation Analyzer API. Replace the placeholder with the API URL for your location. For URLs, see Authorization and API URLs.
{CLIENT_ID} The client ID provided by Vonage Contact Center. For information about getting your client ID, see Getting API credentials from Vonage.
{CLIENT_SECRET} The client secret provided by Vonage. For information about getting your client secret, see Getting API credentials from Vonage.
{guid1} to {guidN} The unique identifiers of the interactions for which you want to retrieve call recording data.
Note: If you do not already know the unique identifiers, you must use the GET /interactions endpoint in the Stats API. This endpoint will return a collection of interactions started, ended or processed within the specified time period. The collection of interactions include the individual interactions’ guids. For information about using the Stats API, see Stats API. You can then use the Conversation Analyzer API to retrieve information about the interactions’ recordings.

When you have these values, replace the placeholders ({URL_FOR_AUTHORIZATION}, {URL_FOR_API}, {CLIENT_ID}, {CLIENT_SECRET}, and {guid1} to {guidN}) in App.config. The following code contains an example App.config.

Example App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
    </startup>
    <appSettings>
      <add key="AuthURL" value="https://emea.newvoicemedia.com"/>
      <add key="ApiURL" value="https://emea.api.newvoicemedia.com"/>
      <add key="ClientId" value="654b0xs233x564b28765m363g7n9873250"/>
      <add key="ClientSecret" value="9r8ApKv9WwXUnZJD6PwH6azHens1o9MuN29vmBC0"/>
      <add key="Guids" value="016613ed-799f-4914-b2ec-4314a72dd9c9,01646a8f-dce7-6994-07d0-c196786ab876,01646004-b2aa-2923-f470-bc8c224a2d68,...,016613ed-796c-4a9a-9e1c-6d07912485ee"/>
    </appSettings>
</configuration>

Authorization and API URLs

Region Authorization URL ({URL_FOR_AUTHORIZATION}) API URL ({URL_FOR_API})
EMEA https://emea.newvoicemedia.com https://emea.api.newvoicemedia.com
US https://nam.newvoicemedia.com https://nam.api.newvoicemedia.com
APAC https://apac.newvoicemedia.com https://apac.api.newvoicemedia.com

Getting API credentials from Vonage Contact Center

To make calls to an API, you require API credentials which authenticate your request.

Your API credentials consist of:

  • your client ID. Your client ID identifies who you are, a bit like a user name.
  • your client secret. Your client secret is a password that’s generated by Vonage Contact Center.

If you do not already have API credentials, you will need to create them in API Admin. For information about creating new API credentials, see Creating new API credentials. If you already have API credentials and want to reuse these for an additional scope, you can add that scope to your existing credentials. For information about adding a new scope to existing API credentials, see Adding a scope to your existing API credentials.