Skip to content

Commit 710e337

Browse files
authored
Update callflow-for-customer-interactions-csharp.md
add recognize code and testing the application
1 parent 4214bcd commit 710e337

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

articles/communication-services/quickstarts/voice-video-calling/includes/call-automation/callflow-for-customer-interactions-csharp.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,16 @@ From the Azure portal, locate your Communication Service resource and click on t
7878
Using the minimal API feature in .NET 6, we can easily add an HTTP POST map and answer the call. A callback URI is required so the service knows how to contact your web server for subsequent calls state events such as `CallConnected` and `PlayCompleted`.
7979

8080
NOTE: The code sample also illustrates how you can control the callback URI by setting your own context/ID when you answer the call. All events generated by the call will be sent to the specific route you provide when answering an inbound call and the same applies to when you place an outbound call.
81-
```csharp
81+
``` csharp
8282
using Azure.Communication;
8383
using Azure.Communication.CallingServer;
8484
using Azure.Messaging.EventGrid;
8585
using Microsoft.AspNetCore.Mvc;
8686
using System.Text.Json;
87-
8887
var builder = WebApplication.CreateBuilder(args);
89-
9088
var client = new CallAutomationClient(builder.Configuration["ACS:ConnectionString"]);
9189
var callbackUriBase = "<YOUR_NGROK_FQDN>"; // i.e. https://someguid.ngrok.io
92-
9390
var app = builder.Build();
94-
9591
app.MapPost("/api/incomingCall", async (
9692
[FromBody] EventGridEvent[] eventGridEvents) =>
9793
{
@@ -110,16 +106,14 @@ app.MapPost("/api/incomingCall", async (
110106
return Results.Ok(responseData);
111107
}
112108
}
113-
114109
var jsonObject = JsonNode.Parse(eventGridEvent.Data).AsObject();
115110
var incomingCallContext = (string)jsonObject["incomingCallContext"];
116111
var callbackUri = new Uri(callbackUriBase + $"/api/calls/{Guid.NewGuid()}");
117112
AnswerCallResult answerCallResult = await client.AnswerCallAsync(incomingCallContext, callbackUri);
118113
}
119-
120114
return Results.Ok();
121115
});
122-
116+
123117
app.MapPost("/api/calls/{contextId}", async (
124118
[FromBody] CloudEvent[] cloudEvents,
125119
[FromRoute] string contextId) =>
@@ -129,24 +123,30 @@ app.MapPost("/api/calls/{contextId}", async (
129123
CallAutomationEventBase @event = CallAutomationEventParser.Parse(cloudEvent);
130124
if (@event == typeof(CallConnected))
131125
{
132-
// play audio file to caller
126+
// recognize pin input of 123
133127
var playSource = new FileSource("<INSERT_AUDIO_FILE_URI>");
134-
var playOptions = new PlayOptions() { Loop = true };
128+
var recognizeOptions = new CallMediaRecognizeDtmfOptions(new PhoneNumberIdentifier("<Target-Participant-Phone-Number>"), 3)
129+
{
130+
InterruptPrompt = true,
131+
InterToneTimeoutInSeconds = TimeSpan.FromSeconds(10),
132+
StopTones = new DtmfTone[] { DtmfTone.Pound },
133+
InitialSilenceTimeoutInSeconds = TimeSpan.FromSeconds(5),
134+
RecognizeOptions = recognizeOptions,
135+
Prompt = playSource
136+
}
135137
await client.GetCallConnection(@event.CallConnectionId)
136138
.GetCallMedia()
137-
.PlayToAllAsync(playSource, playOptions);
139+
.StartRecognizingAsync(recognizeOptions);
138140
}
139-
if (@event == typeof(PlayCompleted))
140-
{
141-
// add ACS user as a participant to the call after audio has completed playing
141+
if @event == typeof(RecognizeCompleted))
142+
{
142143
await client.GetCallConnection(@event.CallConnectionId)
143144
.AddParticipantsAsync(new List<CommunicationIdentifier>() { new CommunicationUserIdentifier("<ACS_USER_ID>")});
144145
}
145146
}
146-
147+
147148
return Results.Ok();
148149
}).Produces(StatusCodes.Status200OK);
149-
150150
app.Run();
151151
```
152152

@@ -155,5 +155,5 @@ app.Run();
155155
1. Place a call to the number you acquired in the Azure portal (see prerequisites above).
156156
2. Your Event Grid subscription to the `IncomingCall` should execute and call your web server.
157157
3. The call will be answered, and an asynchronous web hook callback will be sent to the NGROK callback URI.
158-
4. When the call is connected, a `CallConnected` event will be sent to your web server, wrapped in a `CloudEvent` schema and can be easily deserialized using the Call Automation SDK parser. At this point, the application will request audio to be played in a loop to all participants on the call.
159-
5. When the audio file has played, a `PlayCompleted` event is received, and the web server will make a request to add a participant to the call.
158+
4. When the call is connected, a `CallConnected` event will be sent to your web server, wrapped in a `CloudEvent` schema and can be easily deserialized using the Call Automation SDK parser. At this point the application will request audio to be played and input from a targeted phone number.
159+
5. When the input has been received and recognized, the web server will make a request to add a participant to the call.

0 commit comments

Comments
 (0)