Skip to content

Add region-based PMA endpoints and call transfer functionality #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: users/v-dharmarajv/CallAutomationTestApplication
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 165 additions & 125 deletions Call_Automation_GCCH/Call_Automation_GCCH/CallAutomationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,134 +4,174 @@

namespace Call_Automation_GCCH.Services
{
public class CallAutomationService
{
private readonly CallAutomationClient _client;
private readonly ILogger<CallAutomationService> _logger;
private static string? _recordingLocation;
private static string _recordingFileFormat = "mp4";

public CallAutomationService(string connectionString, string pmaEndpoint, ILogger<CallAutomationService> logger)
public class CallAutomationService
{
_client = new CallAutomationClient(pmaEndpoint: new Uri(pmaEndpoint), connectionString: connectionString);
// _client = new CallAutomationClient(connectionString: connectionString);
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

/// <summary>
/// Gets the call automation client for direct operations
/// </summary>
/// <returns>CallAutomationClient instance</returns>
public CallAutomationClient GetCallAutomationClient()
{
return _client;
}

/// <summary>
/// Gets the recording location
/// </summary>
/// <returns>Recording location string</returns>
public static string GetRecordingLocation()
{
return _recordingLocation;
}

/// <summary>
/// Sets the recording location
/// </summary>
/// <param name="location">The recording location to set</param>
public static void SetRecordingLocation(string location)
{
_recordingLocation = location;
}

/// <summary>
/// Gets the recording file format
/// </summary>
/// <returns>Recording file format string</returns>
public static string GetRecordingFileFormat()
{
return _recordingFileFormat;
}

/// <summary>
/// Sets the recording file format
/// </summary>
/// <param name="format">The recording file format to set</param>
public static void SetRecordingFileFormat(string format)
{
_recordingFileFormat = format;
}

public CallConnection GetCallConnection(string callConnectionId)
{
try
{
return _client.GetCallConnection(callConnectionId);
}
catch (Exception ex)
{
string errorMessage = $"Error in GetCallConnection: {ex.Message}. CallConnectionId: {callConnectionId}";
_logger.LogError(errorMessage);
throw;
}
}

public CallMedia GetCallMedia(string callConnectionId)
{
try
{
return _client.GetCallConnection(callConnectionId).GetCallMedia();
}
catch (Exception ex)
{
string errorMessage = $"Error in GetCallMedia: {ex.Message}. CallConnectionId: {callConnectionId}";
_logger.LogError(errorMessage);
throw; // Rethrow so the caller can handle or return an error response
}
}
private CallAutomationClient _client;
private readonly ICommunicationConfigurationService _communicationConfigurationService;
private readonly ILogger<CallAutomationService> _logger;
private static string? _recordingLocation;
private static string _recordingFileFormat = "mp4";

public CallAutomationService(ICommunicationConfigurationService communicationConfigurationService, ILogger<CallAutomationService> logger)
{
_communicationConfigurationService = communicationConfigurationService ?? throw new ArgumentNullException(nameof(communicationConfigurationService));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));

var pmaEndpoint = _communicationConfigurationService.communicationConfiguration.PmaEndpoint;
var connectionString = _communicationConfigurationService.communicationConfiguration.AcsConnectionString;

if (!string.IsNullOrEmpty(pmaEndpoint))
{
_client = new CallAutomationClient(pmaEndpoint: new Uri(pmaEndpoint), connectionString: connectionString);
}
else
{
_logger.LogWarning("PmaEndpoint is empty. Creating CallAutomationClient without PmaEndpoint parameter.");
_client = new CallAutomationClient(connectionString: connectionString);
}
}

/// <summary>
/// Gets the call automation client for direct operations
/// </summary>
/// <returns>CallAutomationClient instance</returns>
public CallAutomationClient GetCallAutomationClient()
{
return _client;
}

/// <summary>
/// Gets the recording location
/// </summary>
/// <returns>Recording location string</returns>
public static string? GetRecordingLocation()
{
return _recordingLocation;
}

/// <summary>
/// Sets the recording location
/// </summary>
/// <param name="location">The recording location to set</param>
public static void SetRecordingLocation(string location)
{
_recordingLocation = location;
}

/// <summary>
/// Gets the recording file format
/// </summary>
/// <returns>Recording file format string</returns>
public static string GetRecordingFileFormat()
{
return _recordingFileFormat;
}

/// <summary>
/// Sets the recording file format
/// </summary>
/// <param name="format">The recording file format to set</param>
public static void SetRecordingFileFormat(string format)
{
_recordingFileFormat = format;
}

public CallConnection GetCallConnection(string callConnectionId)
{
try
{
return _client.GetCallConnection(callConnectionId);
}
catch (Exception ex)
{
string errorMessage = $"Error in GetCallConnection: {ex.Message}. CallConnectionId: {callConnectionId}";
_logger.LogError(errorMessage);
throw;
}
}

public CallMedia GetCallMedia(string callConnectionId)
{
try
{
return _client.GetCallConnection(callConnectionId).GetCallMedia();
}
catch (Exception ex)
{
string errorMessage = $"Error in GetCallMedia: {ex.Message}. CallConnectionId: {callConnectionId}";
_logger.LogError(errorMessage);
throw; // Rethrow so the caller can handle or return an error response
}
}

public CallConnectionProperties GetCallConnectionProperties(string callConnectionId)
{
try
{
return _client.GetCallConnection(callConnectionId).GetCallConnectionProperties();
}
catch (Exception ex)
{
string errorMessage = $"Error in GetCallConnectionProperties: {ex.Message}. CallConnectionId: {callConnectionId}";
_logger.LogError(errorMessage);
throw;
}
}

/// <summary>
/// Updates the CallAutomationClient with new connection settings
/// </summary>
/// <param name="connectionString">The ACS connection string</param>
/// <param name="pmaEndpoint">The PMA endpoint to use</param>
// public void UpdateClient(string connectionString, string pmaEndpoint)
// {
// _logger = _logger ?? throw new InvalidOperationException("Logger is not initialized");
// _currentPmaEndpoint = pmaEndpoint;

// if (!string.IsNullOrEmpty(pmaEndpoint))
// {
// _client = new CallAutomationClient(pmaEndpoint: new Uri(pmaEndpoint), connectionString: connectionString);
// _logger.LogInformation($"CallAutomationClient recreated with PMA endpoint: {pmaEndpoint}");
// }
// else
// {
// _logger.LogWarning("PmaEndpoint is empty. Creating CallAutomationClient without PmaEndpoint parameter.");
// _client = new CallAutomationClient(connectionString: connectionString);
// }
// }

// public string GetCurrentPmaEndpoint()
// {
// return _currentPmaEndpoint;
// }

//Need Azure Cognitive services for this so in phase 2
//public List<RecognitionChoice> GetChoices()
//{
// return new List<RecognitionChoice> {
// new RecognitionChoice("Confirm", new List<string> {
// "Confirm",
// "First",
// "One"
// }) {
// Tone = DtmfTone.One
// },
// new RecognitionChoice("Cancel", new List<string> {
// "Cancel",
// "Second",
// "Two"
// }) {
// Tone = DtmfTone.Two
// }
//};
//public List<RecognitionChoice> GetChoices() => new List<RecognitionChoice>
// {
// // Only DTMF tones, no speech phrases
// new RecognitionChoice("Confirm", new List<string>()) { Tone = DtmfTone.One },
// new RecognitionChoice("Cancel", new List<string>()) { Tone = DtmfTone.Two }
// };

public CallConnectionProperties GetCallConnectionProperties(string callConnectionId)
{
try
{
return _client.GetCallConnection(callConnectionId).GetCallConnectionProperties();
}
catch (Exception ex)
{
string errorMessage = $"Error in GetCallConnectionProperties: {ex.Message}. CallConnectionId: {callConnectionId}";
_logger.LogError(errorMessage);
throw;
}
}

//Need Azure Cognitive services for this so in phase 2
//public List<RecognitionChoice> GetChoices()
//{
// return new List<RecognitionChoice> {
// new RecognitionChoice("Confirm", new List<string> {
// "Confirm",
// "First",
// "One"
// }) {
// Tone = DtmfTone.One
// },
// new RecognitionChoice("Cancel", new List<string> {
// "Cancel",
// "Second",
// "Two"
// }) {
// Tone = DtmfTone.Two
// }
//};
//public List<RecognitionChoice> GetChoices() => new List<RecognitionChoice>
// {
// // Only DTMF tones, no speech phrases
// new RecognitionChoice("Confirm", new List<string>()) { Tone = DtmfTone.One },
// new RecognitionChoice("Cancel", new List<string>()) { Tone = DtmfTone.Two }
// };

}
}


Expand Down
Loading