Quickstart: Make an outbound call using Call Automation
Artikel
Azure Communication Services Call Automation APIs are a powerful way to create interactive calling experiences. In this quick start, we cover a way to make an outbound call and recognize various events in the call.
A phone number in your Azure Communication Services resource that can make outbound calls. If you have a free subscription, you can get a trial phone number.
Create and host an Azure Dev Tunnel. Instructions here.
(Optional) A Microsoft Teams user with a phone license that is voice enabled. Teams phone license is required to add Teams users to the call. Learn more about Teams licenses here. Learn about enabling phone system with voicehere.
Sample code
Download or clone quickstart sample code from GitHub.
Navigate to CallAutomation_OutboundCalling folder and open the solution in a code editor.
Setup and host your Azure DevTunnel
Azure DevTunnels is an Azure service that enables you to share local web services hosted on the internet. Run the commands to connect your local development environment to the public internet. DevTunnels creates a persistent endpoint URL and which allows anonymous access. We use this endpoint to notify your application of calling events from the Azure Communication Services Call Automation service.
Bash
devtunnel create --allow-anonymous
devtunnel port create -p 8080
devtunnel host
Alternatively, follow instructions to set up your Azure DevTunnel in Visual Studio
Update your application configuration
Next update your Program.cs file with the following values:
acsConnectionString: The connection string for your Azure Communication Services resource. You can find your Azure Communication Services connection string using the instructions here.
callbackUriHost: Once you have your DevTunnel host initialized, update this field with that URI.
acsPhonenumber: update this field with the Azure Communication Services phone number you have acquired. This phone number should use the E164 phone number format (e.g +18881234567)
targetPhonenumber: update field with the phone number you would like your application to call. This phone number should use the E164 phone number format (e.g +18881234567)
cognitiveServiceEndpoint: update field with your Azure AI services endpoint.
targetTeamsUserId: (Optional) update field with the Microsoft Teams user Id you would like to add to the call. See Use Graph API to get Teams user Id.
C#
// Your ACS resource connection string var acsConnectionString = "<ACS_CONNECTION_STRING>";
// Your ACS resource phone number will act as source number to start outbound call var acsPhonenumber = "<ACS_PHONE_NUMBER>";
// Target phone number you want to receive the call. var targetPhonenumber = "<TARGET_PHONE_NUMBER>";
// Base url of the app var callbackUriHost = "<CALLBACK_URI_HOST_WITH_PROTOCOL>";
// Your cognitive service endpoint var cognitiveServiceEndpoint = "<COGNITIVE_SERVICE_ENDPOINT>";
// (Optional) User Id of the target teams user you want to receive the call.var targetTeamsUserId = "<TARGET_TEAMS_USER_ID>";
Make an outbound call
To make the outbound call from Azure Communication Services, this sample uses the targetPhonenumber you defined earlier in the application to create the call using the CreateCallAsync API. This code will make an outbound call using the target phone number.
C#
PhoneNumberIdentifier target = new PhoneNumberIdentifier(targetPhonenumber);
PhoneNumberIdentifier caller = new PhoneNumberIdentifier(acsPhonenumber);
var callbackUri = new Uri(callbackUriHost + "/api/callbacks");
CallInvite callInvite = new CallInvite(target, caller);
var createCallOptions = new CreateCallOptions(callInvite, callbackUri) {
CallIntelligenceOptions = new CallIntelligenceOptions() {
CognitiveServicesEndpoint = new Uri(cognitiveServiceEndpoint)
}
};
CreateCallResult createCallResult = await callAutomationClient.CreateCallAsync(createCallOptions);
Handle call automation events
Earlier in our application, we registered the callbackUriHost to the Call Automation Service. The host indicates the endpoint the service requires to notify us of calling events that happen. We can then iterate through the events and detect specific events our application wants to understand. In the code be below we respond to the CallConnected event.
C#
app.MapPost("/api/callbacks", async (CloudEvent[] cloudEvents, ILogger < Program > logger) => {
foreach(var cloudEvent in cloudEvents) {
logger.LogInformation($"Event received: {JsonConvert.SerializeObject(cloudEvent)}");
CallAutomationEventBase parsedEvent = CallAutomationEventParser.Parse(cloudEvent);
logger.LogInformation($"{parsedEvent?.GetType().Name} parsedEvent received for call connection id: {parsedEvent?.CallConnectionId}");
var callConnection = callAutomationClient.GetCallConnection(parsedEvent.CallConnectionId);
var callMedia = callConnection.GetCallMedia();
if (parsedEvent is CallConnected) {
//Handle Call Connected Event
}
}
});
(Optional) Add a Microsoft Teams user to the call
You can add a Microsoft Teams user to the call using the AddParticipantAsync method with a MicrosoftTeamsUserIdentifier and the Teams user's Id. You first need to complete the prerequisite step Authorization for your Azure Communication Services Resource to enable calling to Microsoft Teams users. Optionally, you can also pass in a SourceDisplayName to control the text displayed in the toast notification for the Teams user.
The Call Automation service also enables the capability to start recording and store recordings of voice and video calls. You can learn more about the various capabilities in the Call Recording APIs here.
C#
CallLocator callLocator = new ServerCallLocator(parsedEvent.ServerCallId);
var recordingResult = await callAutomationClient.GetCallRecording().StartAsync(new StartRecordingOptions(callLocator));
recordingId = recordingResult.Value.RecordingId;
Play welcome message and recognize
Using the TextSource, you can provide the service with the text you want synthesized and used for your welcome message. The Azure Communication Services Call Automation service plays this message upon the CallConnected event.
Next, we pass the text into the CallMediaRecognizeChoiceOptions and then call StartRecognizingAsync. This allows your application to recognize the option the caller chooses.
C#
if (parsedEvent is CallConnected callConnected) {
logger.LogInformation($"Start Recording...");
CallLocator callLocator = new ServerCallLocator(parsedEvent.ServerCallId);
var recordingResult = await callAutomationClient.GetCallRecording().StartAsync(new StartRecordingOptions(callLocator));
recordingId = recordingResult.Value.RecordingId;
var choices = GetChoices();
// prepare recognize tones var recognizeOptions = GetMediaRecognizeChoiceOptions(mainMenu, targetPhonenumber, choices);
// Send request to recognize tones await callMedia.StartRecognizingAsync(recognizeOptions);
}
CallMediaRecognizeChoiceOptions GetMediaRecognizeChoiceOptions(string content, string targetParticipant, List < RecognitionChoice > choices, string context = "") {
var playSource = new TextSource(content) {
VoiceName = SpeechToTextVoice
};
var recognizeOptions = new CallMediaRecognizeChoiceOptions(targetParticipant: new PhoneNumberIdentifier(targetParticipant), choices) {
InterruptCallMediaOperation = false,
InterruptPrompt = false,
InitialSilenceTimeout = TimeSpan.FromSeconds(10),
Prompt = playSource,
OperationContext = context
};
return recognizeOptions;
}
List < RecognitionChoice > GetChoices() {
returnnew 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
}
};
}
Handle Choice Events
Azure Communication Services Call Automation triggers the api/callbacks to the webhook we have setup and will notify us with the RecognizeCompleted event. The event gives us the ability to respond to input received and trigger an action. The application then plays a message to the caller based on the specific input received.
C#
if (parsedEvent is RecognizeCompleted recognizeCompleted) {
var choiceResult = recognizeCompleted.RecognizeResult as ChoiceResult;
var labelDetected = choiceResult?.Label;
var phraseDetected = choiceResult?.RecognizedPhrase;
// If choice is detected by phrase, choiceResult.RecognizedPhrase will have the phrase detected, // If choice is detected using dtmf tone, phrase will be null
logger.LogInformation("Recognize completed successfully, labelDetected={labelDetected}, phraseDetected={phraseDetected}", labelDetected, phraseDetected);
var textToPlay = labelDetected.Equals(ConfirmChoiceLabel, StringComparison.OrdinalIgnoreCase) ? ConfirmedText : CancelText;
await HandlePlayAsync(callMedia, textToPlay);
}
async Task HandlePlayAsync(CallMedia callConnectionMedia, string text) {
// Play goodbye message var GoodbyePlaySource = new TextSource(text) {
VoiceName = "en-US-NancyNeural"
};
await callConnectionMedia.PlayToAllAsync(GoodbyePlaySource);
}
Hang up and stop recording
Finally, when we detect a condition that makes sense for us to terminate the call, we can use the HangUpAsync method to hang up the call.
C#
if ((parsedEvent is PlayCompleted) || (parsedEvent is PlayFailed))
{
logger.LogInformation($"Stop recording and terminating call.");
callAutomationClient.GetCallRecording().Stop(recordingId);
await callConnection.HangUpAsync(true);
}
To run the application with VS Code, open a Terminal window and run the following command
Bash
dotnet run
Open http://localhost:8080/swagger/index.html or your dev tunnel URL in browser. The tunnel URL looks like: <YOUR DEV TUNNEL ENDPOINT>/swagger/index.html
Press Ctrl+F5 to run the web application without the debugger. The web browser automatically opens to a tunnel URL. The tunnel URL looks like: <YOUR DEV TUNNEL ENDPOINT>/swagger/index.html
A phone number in your Azure Communication Services resource that can make outbound calls. If you have a free subscription, you can get a trial phone number.
Create and host an Azure Dev Tunnel. Instructions here.
(Optional) A Microsoft Teams user with a phone license that is voice enabled. Teams phone license is required to add Teams users to the call. Learn more about Teams licenses here. For more information to enable voice on your phone system, see setting up your phone system.
Sample code
Download or clone quickstart sample code from GitHub.
Navigate to CallAutomation_OutboundCalling folder and open the solution in a code editor.
Setup and host your Azure DevTunnel
Azure DevTunnels is an Azure service that enables you to share local web services hosted on the internet. Run the DevTunnel commands to connect your local development environment to the public internet. DevTunnels then creates a tunnel with a persistent endpoint URL and which allows anonymous access. Azure Communication Services uses this endpoint to notify your application of calling events from the Azure Communication Services Call Automation service.
Bash
devtunnel create --allow-anonymous
devtunnel port create -p MY_SPRINGAPP_PORT
devtunnel host
Update your application configuration
Then open the application.yml file in the /resources folder to configure the following values:
connectionstring: The connection string for your Azure Communication Services resource. You can find your Azure Communication Services connection string using the instructions here.
basecallbackuri: Once you have your DevTunnel host initialized, update this field with that URI.
callerphonenumber: update this field with the Azure Communication Services phone number you have acquired. This phone number should use the E164 phone number format (e.g +18881234567)
targetphonenumber: update field with the phone number you would like your application to call. This phone number should use the E164 phone number format (e.g +18881234567)
cognitiveServiceEndpoint: update field with your Azure AI services endpoint.
targetTeamsUserId: (Optional) update field with the Microsoft Teams user Id you would like to add to the call. See Use Graph API to get Teams user Id.
YAML
acs:connectionstring:<YOURACSCONNECTIONSTRING>basecallbackuri:<YOURDEVTUNNELENDPOINT>callerphonenumber:<YOURACSPHONENUMBERex."+1425XXXAAAA">
targetphonenumber: <YOUR TARGET PHONE NUMBER ex. "+1425XXXAAAA">
cognitiveServiceEndpoint: <YOUR COGNITIVE SERVICE ENDPOINT>
targetTeamsUserId:<(OPTIONAL)YOURTARGETTEAMSUSERIDex."00aa00aa-bb11-cc22-dd33-44ee44ee44ee">
Make an outbound call and play media
To make the outbound call from Azure Communication Services, this sample uses the targetphonenumber you defined in the application.yml file to create the call using the createCallWithResponse API.
Java
PhoneNumberIdentifier caller = new PhoneNumberIdentifier(appConfig.getCallerphonenumber());
PhoneNumberIdentifier target = new PhoneNumberIdentifier(appConfig.getTargetphonenumber());
CallInvite callInvite = new CallInvite(target, caller);
CreateCallOptions createCallOptions = new CreateCallOptions(callInvite, appConfig.getCallBackUri());
CallIntelligenceOptions callIntelligenceOptions = new CallIntelligenceOptions().setCognitiveServicesEndpoint(appConfig.getCognitiveServiceEndpoint());
createCallOptions = createCallOptions.setCallIntelligenceOptions(callIntelligenceOptions);
Response<CreateCallResult> result = client.createCallWithResponse(createCallOptions, Context.NONE);
(Optional) Add a Microsoft Teams user to the call
You can add a Microsoft Teams user to the call using the addParticipant method with a MicrosoftTeamsUserIdentifier and the Teams user's Id. You first need to complete the prerequisite step Authorization for your Azure Communication Services Resource to enable calling to Microsoft Teams users. Optionally, you can also pass in a SourceDisplayName to control the text displayed in the toast notification for the Teams user.
Java
client.getCallConnection(callConnectionId).addParticipant(
new CallInvite(new MicrosoftTeamsUserIdentifier(targetTeamsUserId))
.setSourceDisplayName("Jack (Contoso Tech Support)"));
Start recording a call
The Call Automation service also enables the capability to start recording and store recordings of voice and video calls. You can learn more about the various capabilities in the Call Recording APIs here.
Earlier in our application, we registered the basecallbackuri to the Call Automation Service. The URI indicates endpoint the service will use to notify us of calling events that happen. We can then iterate through the events and detect specific events our application wants to understand. In the code be below we respond to the CallConnected event.
Using the TextSource, you can provide the service with the text you want synthesized and used for your welcome message. The Azure Communication Services Call Automation service plays this message upon the CallConnected event.
Next, we pass the text into the CallMediaRecognizeChoiceOptions and then call StartRecognizingAsync. This allows your application to recognize the option the caller chooses.
Java
var playSource = new TextSource().setText(content).setVoiceName("en-US-NancyNeural");
var recognizeOptions = new CallMediaRecognizeChoiceOptions(new PhoneNumberIdentifier(targetParticipant), getChoices())
.setInterruptCallMediaOperation(false)
.setInterruptPrompt(false)
.setInitialSilenceTimeout(Duration.ofSeconds(10))
.setPlayPrompt(playSource)
.setOperationContext(context);
client.getCallConnection(callConnectionId)
.getCallMedia()
.startRecognizing(recognizeOptions);
private List < RecognitionChoice > getChoices() {
var choices = Arrays.asList(
new RecognitionChoice().setLabel(confirmLabel).setPhrases(Arrays.asList("Confirm", "First", "One")).setTone(DtmfTone.ONE),
new RecognitionChoice().setLabel(cancelLabel).setPhrases(Arrays.asList("Cancel", "Second", "Two")).setTone(DtmfTone.TWO)
);
return choices;
}
Handle Choice Events
Azure Communication Services Call Automation triggers the api/callbacks to the webhook we have setup and will notify us with the RecognizeCompleted event. The event gives us the ability to respond to input received and trigger an action. The application then plays a message to the caller based on the specific input received.
A phone number in your Azure Communication Services resource that can make outbound calls. If you have a free subscription, you can get a trial phone number.
Create and host an Azure Dev Tunnel. Instructions here.
(Optional) A Microsoft Teams user with a phone license that is voice enabled. Teams phone license is required to add Teams users to the call. Learn more about Teams licenses here. For more information to enable voice on your phone system, see setting up your phone system.
Sample code
Download or clone quickstart sample code from GitHub.
Navigate to CallAutomation_OutboundCalling folder and open the solution in a code editor.
Set up the environment
Download the sample code and navigate to the project directory and run the npm command that installs the necessary dependencies and set up your developer environment.
Bash
npm install
Set up and host your Azure DevTunnel
Azure DevTunnels is an Azure service that enables you to share local web services hosted on the internet. Use the DevTunnel CLI commands to connect your local development environment to the public internet. We use this endpoint to notify your application of calling events from the Azure Communication Services Call Automation service.
Bash
devtunnel create --allow-anonymous
devtunnel port create -p 8080
devtunnel host
Update your application configuration
Then update your .env file with following values:
CONNECTION_STRING: The connection string for your Azure Communication Services resource. You can find your Azure Communication Services connection string using the instructions here.
CALLBACK_URI: Once you have your DevTunnel host initialized, update this field with that URI.
TARGET_PHONE_NUMBER: update field with the phone number you would like your application to call. This phone number should use the E164 phone number format (e.g +18881234567)
ACS_RESOURCE_PHONE_NUMBER: update this field with the Azure Communication Services phone number you have acquired. This phone number should use the E164 phone number format (e.g +18881234567)
COGNITIVE_SERVICES_ENDPOINT: update field with your Azure AI services endpoint.
TARGET_TEAMS_USER_ID: (Optional) update field with the Microsoft Teams user Id you would like to add to the call. See Use Graph API to get Teams user Id.
To make the outbound call from Azure Communication Services, you use the phone number you provided to the environment. Ensure that the phone number is in the E164 phone number format (e.g +18881234567)
The code makes an outbound call using the target_phone_number you've provided and place an outbound call to that number:
The Call Automation service also enables the capability to start recording and store recordings of voice and video calls. You can learn more about the various capabilities in the Call Recording APIs here.
Earlier in our application, we registered the CALLBACK_URI to the Call Automation Service. The URI indicates the endpoint the service uses to notify us of calling events that happen. We can then iterate through the events and detect specific events our application wants to understand. We respond to the CallConnected event to get notified and initiate downstream operations. Using the TextSource, you can provide the service with the text you want synthesized and used for your welcome message. The Azure Communication Services Call Automation service plays this message upon the CallConnected event.
Next, we pass the text into the CallMediaRecognizeChoiceOptions and then call StartRecognizingAsync. This allows your application to recognize the option the caller chooses.
Azure Communication Services Call Automation triggers the api/callbacks to the webhook we have setup and will notify us with the RecognizeCompleted event. The event gives us the ability to respond to input received and trigger an action. The application then plays a message to the caller based on the specific input received.
A phone number in your Azure Communication Services resource that can make outbound calls. If you have a free subscription, you can get a trial phone number.
Create and host an Azure Dev Tunnel. Instructions here.
(Optional) A Microsoft Teams user with a phone license that is voice enabled. Teams phone license is required to add Teams users to the call. Learn more about Teams licenses here. For more information to enable voice on your phone system, see setting up your phone system.
Sample code
Download or clone quickstart sample code from GitHub.
Navigate to CallAutomation_OutboundCalling folder and open the solution in a code editor.
Set up the Python environment
Create and activate python environment and install required packages using following command. You can learn more about managing packages here
Bash
pip install -r requirements.txt
Set up and host your Azure DevTunnel
Azure DevTunnels is an Azure service that enables you to share local web services hosted on the internet. Use the commands to connect your local development environment to the public internet. DevTunnels creates a tunnel with a persistent endpoint URL and which allows anonymous access. We use this endpoint to notify your application of calling events from the Azure Communication Services Call Automation service.
Bash
devtunnel create --allow-anonymous
devtunnel port create -p 8080
devtunnel host
Update your application configuration
Then update your main.py file with the following values:
ACS_CONNECTION_STRING: The connection string for your Azure Communication Services resource. You can find your Azure Communication Services connection string using the instructions here.
CALLBACK_URI_HOST: Once you have your DevTunnel host initialized, update this field with that URI.
TARGET_PHONE_NUMBER: update field with the phone number you would like your application to call. This phone number should use the E164 phone number format (e.g +18881234567)
ACS_PHONE_NUMBER: update this field with the Azure Communication Services phone number you have acquired. This phone number should use the E164 phone number format (e.g +18881234567)
COGNITIVE_SERVICES_ENDPOINT: update field with your Azure AI services endpoint.
TARGET_TEAMS_USER_ID: (Optional) update field with the Microsoft Teams user Id you would like to add to the call. See Use Graph API to get Teams user Id.
Python
# Your ACS resource connection string
ACS_CONNECTION_STRING = "<ACS_CONNECTION_STRING>"# Your ACS resource phone number will act as source number to start outbound call
ACS_PHONE_NUMBER = "<ACS_PHONE_NUMBER>"# Target phone number you want to receive the call.
TARGET_PHONE_NUMBER = "<TARGET_PHONE_NUMBER>"# Callback events URI to handle callback events.
CALLBACK_URI_HOST = "<CALLBACK_URI_HOST_WITH_PROTOCOL>"
CALLBACK_EVENTS_URI = CALLBACK_URI_HOST + "/api/callbacks"#Your Cognitive service endpoint
COGNITIVE_SERVICES_ENDPOINT = "<COGNITIVE_SERVICES_ENDPOINT>"#(OPTIONAL) Your target Microsoft Teams user Id ex. "00aa00aa-bb11-cc22-dd33-44ee44ee44ee"
TARGET_TEAMS_USER_ID = "<TARGET_TEAMS_USER_ID>"
Make an outbound call
To make the outbound call from Azure Communication Services, first you provide the phone number you want to receive the call. To make it simple, you can update the target_phone_number with a phone number in the E164 phone number format (e.g +18881234567)
Make an outbound call using the target_phone_number you've provided:
You can add a Microsoft Teams user to the call using the add_participant method with a MicrosoftTeamsUserIdentifier and the Teams user's Id. You first need to complete the prerequisite step Authorization for your Azure Communication Services Resource to enable calling to Microsoft Teams users. Optionally, you can also pass in a source_display_name to control the text displayed in the toast notification for the Teams user.
The Call Automation service also enables the capability to start recording and store recordings of voice and video calls. You can learn more about the various capabilities in the Call Recording APIs here.
Earlier in our application, we registered the CALLBACK_URI_HOST to the Call Automation Service. The URI indicates the endpoint the service uses to notify us of calling events that happen. We can then iterate through the events and detect specific events our application wants to understand. In the code be below we respond to the CallConnected event.
Python
@app.route('/api/callbacks', methods=['POST'])defcallback_events_handler():for event_dict in request.json:
event = CloudEvent.from_dict(event_dict)
if event.type == "Microsoft.Communication.CallConnected":
# Handle Call Connected Event
...
return Response(status=200)
Play welcome message and recognize
Using the TextSource, you can provide the service with the text you want synthesized and used for your welcome message. The Azure Communication Services Call Automation service plays this message upon the CallConnected event.
Next, we pass the text into the CallMediaRecognizeChoiceOptions and then call StartRecognizingAsync. This allows your application to recognize the option the caller chooses.
Azure Communication Services Call Automation triggers the api/callbacks to the webhook we have setup and will notify us with the RecognizeCompleted event. The event gives us the ability to respond to input received and trigger an action. The application then plays a message to the caller based on the specific input received.
Finally, when we detect a condition that makes sense for us to terminate the call, we can use the hang_up() method to hang up the call. Finally, we can also safely stop the call recording operation.