Skip to content
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

Tested Beta4 Changes #164

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Azure.Communication;
using Azure.Communication.CallAutomation;
using Azure.Messaging;
using Azure.Messaging.EventGrid;
using Azure.Messaging.EventGrid.SystemEvents;
using Microsoft.Extensions.Options;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


// Your ACS resource connection string
var acsConnectionString = "<ACS_CONNECTION_STRING>";

Expand Down Expand Up @@ -51,14 +51,26 @@ Your appointment tomorrow at 9am has been cancelled. Please call the bank direct
PhoneNumberIdentifier caller = new PhoneNumberIdentifier(acsPhonenumber);
var callbackUri = new Uri(new Uri(callbackUriHost), "/api/callbacks");
CallInvite callInvite = new CallInvite(target, caller);

MediaStreamingOptions mediaStreamingOptions = new MediaStreamingOptions(
new Uri("wss://a016-103-190-198-138.ngrok-free.app"),
MediaStreamingTransport.Websocket,
MediaStreamingContent.Audio,
MediaStreamingAudioChannel.Mixed,
false);
var createCallOptions = new CreateCallOptions(callInvite, callbackUri)
{
CallIntelligenceOptions = new CallIntelligenceOptions() { CognitiveServicesEndpoint = new Uri(cognitiveServiceEndpoint) }
CallIntelligenceOptions = new CallIntelligenceOptions() { CognitiveServicesEndpoint = new Uri(cognitiveServiceEndpoint) },
MediaStreamingOptions = mediaStreamingOptions
//TranscriptionOptions = new TranscriptionOptions(new Uri("wss://866a-103-180-73-34.ngrok-free.app"), TranscriptionTransport.Websocket, "", true),
};

CreateCallResult createCallResult = await callAutomationClient.CreateCallAsync(createCallOptions);

logger.LogInformation($"Created call with connection id: {createCallResult.CallConnectionProperties.CallConnectionId}");
logger.LogInformation($"Answered By: {createCallResult.CallConnectionProperties.AnsweredBy}");
logger.LogInformation($"Media Streaming: {createCallResult.CallConnectionProperties.MediaStreamingSubscription}");
//logger.LogInformation($"Transcription Subscription: {createCallResult.CallConnectionProperties.TranscriptionSubscription}");
});

app.MapPost("/api/callbacks", async (CloudEvent[] cloudEvents, ILogger<Program> logger) =>
Expand All @@ -73,10 +85,25 @@ Your appointment tomorrow at 9am has been cancelled. Please call the bank direct
parsedEvent.ServerCallId);

var callConnection = callAutomationClient.GetCallConnection(parsedEvent.CallConnectionId);

logger.LogInformation($"CALL CONNECTION ID : {parsedEvent.CallConnectionId}");
logger.LogInformation($"CORRELATION ID : {parsedEvent.CorrelationId}");

var callMedia = callConnection.GetCallMedia();

if (parsedEvent is CallConnected callConnected)
{
{
StartMediaStreamingOptions options = new StartMediaStreamingOptions()
{
OperationCallbackUrl = callbackUriHost,
OperationContext = "startMediaStreamingContext"
};

//callMedia.StartMediaStreaming(options);

await callMedia.StartMediaStreamingAsync(options);
logger.LogInformation("Start Media Streaming.....");

logger.LogInformation("Fetching recognize options...");

// prepare recognize tones
Expand All @@ -86,9 +113,19 @@ Your appointment tomorrow at 9am has been cancelled. Please call the bank direct

// Send request to recognize tones
await callMedia.StartRecognizingAsync(recognizeOptions);
await HandlePlayAsync(callMedia, "Play Started");
}
else if (parsedEvent is RecognizeCompleted recognizeCompleted)
{
StopMediaStreamingOptions options = new StopMediaStreamingOptions()
{
OperationCallbackUrl = callbackUriHost
};

//callMedia.StopMediaStreaming(options);
await callMedia.StopMediaStreamingAsync(options);
logger.LogInformation("Stop Media Streaming.....");

var choiceResult = recognizeCompleted.RecognizeResult as ChoiceResult;
var labelDetected = choiceResult?.Label;
var phraseDetected = choiceResult?.RecognizedPhrase;
Expand All @@ -98,6 +135,7 @@ Your appointment tomorrow at 9am has been cancelled. Please call the bank direct
var textToPlay = labelDetected.Equals(ConfirmChoiceLabel, StringComparison.OrdinalIgnoreCase) ? ConfirmedText : CancelText;

await HandlePlayAsync(callMedia, textToPlay);

}
else if (parsedEvent is RecognizeFailed { OperationContext: RetryContext })
{
Expand All @@ -124,6 +162,18 @@ var _ when reasonCode.Equals(MediaEventReasonCode.RecognizeIncorrectToneDetected
var recognizeOptions = GetMediaRecognizeChoiceOptions(replyText, targetPhonenumber, RetryContext);
await callMedia.StartRecognizingAsync(recognizeOptions);
}
else if ((parsedEvent is MediaStreamingStarted))
{
logger.LogInformation($"MediaStreamingStarted started event triggered.");
}
else if ((parsedEvent is MediaStreamingStopped))
{
logger.LogInformation($"MediaStreamingStopped started event triggered.");
}
else if ((parsedEvent is PlayStarted))
{
logger.LogInformation($"Play started event triggered.");
}
else if ((parsedEvent is PlayCompleted) || (parsedEvent is PlayFailed))
{
logger.LogInformation($"Terminating call.");
Expand All @@ -142,14 +192,16 @@ var _ when reasonCode.Equals(MediaEventReasonCode.RecognizeIncorrectToneDetected
CallMediaRecognizeChoiceOptions GetMediaRecognizeChoiceOptions(string content, string targetParticipant, string context = "")
{
var playSource = new TextSource(content) { VoiceName = SpeechToTextVoice };
var playSources = new List<PlaySource>() { new TextSource("Recognize Prompt One") { VoiceName = SpeechToTextVoice }, new TextSource("Recognize Prompt Two") { VoiceName = SpeechToTextVoice }, new TextSource(content) { VoiceName = SpeechToTextVoice } };

var recognizeOptions =
new CallMediaRecognizeChoiceOptions(targetParticipant: new PhoneNumberIdentifier(targetParticipant), GetChoices())
{
InterruptCallMediaOperation = false,
InterruptPrompt = false,
InitialSilenceTimeout = TimeSpan.FromSeconds(10),
Prompt = playSource,
//Prompt = playSource,
PlayPrompts = playSources,
OperationContext = context
};

Expand Down Expand Up @@ -185,8 +237,27 @@ async Task HandlePlayAsync(CallMedia callConnectionMedia, string text)
{
VoiceName = "en-US-NancyNeural"
};

PlayToAllOptions playOptions = new PlayToAllOptions(GoodbyePlaySource)
{
InterruptCallMediaOperation = false,
OperationCallbackUri = new Uri(callbackUriHost),
Loop = false
};

await callConnectionMedia.PlayToAllAsync(playOptions);
//var interrupt = new TextSource("Interrupt prompt message")
//{
// VoiceName = "en-US-NancyNeural"
//};

await callConnectionMedia.PlayToAllAsync(GoodbyePlaySource);
//PlayToAllOptions playInterrupt = new PlayToAllOptions(interrupt)
//{
// InterruptCallMediaOperation = true,
// OperationCallbackUri = new Uri(callbackUriHost),
// Loop = false
//};
//await callConnectionMedia.PlayToAllAsync(playInterrupt);
}

app.Run();