Azure Communication Callautomation Java

作者 microsoft354361d83247MIT收录于 2026年10月8日更新于 2026年10月8日

Build call automation workflows with Azure Communication Services Call Automation Java SDK. Use when implementing IVR systems, call routing, call recording, DTMF recognition, text-to-speech, or AI-powered call flows.

AI 生成的概览

为 Java 开发者提供 Azure 通信服务呼叫自动化工作流的指导,如 IVR、呼叫路由、录音和语音识别。

功能
该技能提供 Azure 通信服务呼叫自动化 SDK 的 Java 参考指南,涵盖客户端创建、呼出与呼入呼叫、文本转语音播放、DTMF 与语音识别、录音、参与者管理、呼叫转移、Webhook 事件解析和错误处理。它产出代码片段和配置说明,而非可运行脚本。它还列出了端点、连接字符串和回调 URL 所需的环境变量。
适用场景
适用于用 Java 实现服务端呼叫自动化的场景,例如 IVR 菜单、呼叫路由、通话录音或 AI 语音交互。适合需要具体 SDK 用法模式和事件处理示例的开发者。
运行要求
需要 Java 及 com.azure:azure-communication-callautomation Maven 依赖,Azure 通信服务凭据(端点或连接字符串,或通过 azure-identity 使用 Entra ID),以及可公开访问的 Webhook 回调 URL。需要访问 Azure 服务的网络。不附带脚本,仅为说明文档。

Azure Communication Call Automation (Java)

Build server-side call automation workflows including IVR systems, call routing, recording, and AI-powered interactions.

Installation

xml
<dependency>    <groupId>com.azure</groupId>    <artifactId>azure-communication-callautomation</artifactId>    <version>1.6.0</version></dependency>

Client Creation

java
import com.azure.communication.callautomation.CallAutomationClient;import com.azure.communication.callautomation.CallAutomationClientBuilder;import com.azure.core.credential.TokenCredential;import com.azure.identity.AzureIdentityEnvVars;import com.azure.identity.DefaultAzureCredentialBuilder;import com.azure.identity.ManagedIdentityCredentialBuilder;
// Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>TokenCredential credential = new DefaultAzureCredentialBuilder()    .requireEnvVars(AzureIdentityEnvVars.AZURE_TOKEN_CREDENTIALS)    .build();// Or use a specific credential directly in production:// See https://learn.microsoft.com/java/api/overview/azure/identity-readme?view=azure-java-stable#credential-classes// TokenCredential credential = new ManagedIdentityCredentialBuilder().build();
// With DefaultAzureCredentialCallAutomationClient client = new CallAutomationClientBuilder()    .endpoint("https://<resource>.communication.azure.com")    .credential(credential)    .buildClient();
// With connection stringCallAutomationClient client = new CallAutomationClientBuilder()    .connectionString("<connection-string>")    .buildClient();

Key Concepts

ClassPurpose
CallAutomationClientMake calls, answer/reject incoming calls, redirect calls
CallConnectionActions in established calls (add participants, terminate)
CallMediaMedia operations (play audio, recognize DTMF/speech)
CallRecordingStart/stop/pause recording
CallAutomationEventParserParse webhook events from ACS

Create Outbound Call

java
import com.azure.communication.callautomation.models.*;import com.azure.communication.common.CommunicationUserIdentifier;import com.azure.communication.common.PhoneNumberIdentifier;
// Call to PSTN numberPhoneNumberIdentifier target = new PhoneNumberIdentifier("+14255551234");PhoneNumberIdentifier caller = new PhoneNumberIdentifier("+14255550100");
CreateCallOptions options = new CreateCallOptions(    new CommunicationUserIdentifier("<user-id>"),  // Source    List.of(target))                                // Targets    .setSourceCallerId(caller)    .setCallbackUrl("https://your-app.com/api/callbacks");
CreateCallResult result = client.createCall(options);String callConnectionId = result.getCallConnectionProperties().getCallConnectionId();

Answer Incoming Call

java
// From Event Grid webhook - IncomingCall eventString incomingCallContext = "<incoming-call-context-from-event>";
AnswerCallOptions options = new AnswerCallOptions(    incomingCallContext,    "https://your-app.com/api/callbacks");
AnswerCallResult result = client.answerCall(options);CallConnection callConnection = result.getCallConnection();

Play Audio (Text-to-Speech)

java
CallConnection callConnection = client.getCallConnection(callConnectionId);CallMedia callMedia = callConnection.getCallMedia();
// Play text-to-speechTextSource textSource = new TextSource()    .setText("Welcome to Contoso. Press 1 for sales, 2 for support.")    .setVoiceName("en-US-JennyNeural");
PlayOptions playOptions = new PlayOptions(    List.of(textSource),    List.of(new CommunicationUserIdentifier("<target-user>")));
callMedia.play(playOptions);
// Play audio fileFileSource fileSource = new FileSource()    .setUrl("https://storage.blob.core.windows.net/audio/greeting.wav");
callMedia.play(new PlayOptions(List.of(fileSource), List.of(target)));

Recognize DTMF Input

java
// Recognize DTMF tonesDtmfTone stopTones = DtmfTone.POUND;
CallMediaRecognizeDtmfOptions recognizeOptions = new CallMediaRecognizeDtmfOptions(    new CommunicationUserIdentifier("<target-user>"),    5)  // Max tones to collect    .setInterToneTimeout(Duration.ofSeconds(5))    .setStopTones(List.of(stopTones))    .setInitialSilenceTimeout(Duration.ofSeconds(15))    .setPlayPrompt(new TextSource().setText("Enter your account number followed by pound."));
callMedia.startRecognizing(recognizeOptions);

Recognize Speech

java
// Speech recognition with AICallMediaRecognizeSpeechOptions speechOptions = new CallMediaRecognizeSpeechOptions(    new CommunicationUserIdentifier("<target-user>"))    .setEndSilenceTimeout(Duration.ofSeconds(2))    .setSpeechLanguage("en-US")    .setPlayPrompt(new TextSource().setText("How can I help you today?"));
callMedia.startRecognizing(speechOptions);

Call Recording

java
CallRecording callRecording = client.getCallRecording();
// Start recordingStartRecordingOptions recordingOptions = new StartRecordingOptions(    new ServerCallLocator("<server-call-id>"))    .setRecordingChannel(RecordingChannel.MIXED)    .setRecordingContent(RecordingContent.AUDIO_VIDEO)    .setRecordingFormat(RecordingFormat.MP4);
RecordingStateResult recordingResult = callRecording.start(recordingOptions);String recordingId = recordingResult.getRecordingId();
// Pause/resume/stopcallRecording.pause(recordingId);callRecording.resume(recordingId);callRecording.stop(recordingId);
// Download recording (after RecordingFileStatusUpdated event)callRecording.downloadTo(recordingUrl, Paths.get("recording.mp4"));

Add Participant to Call

java
CallConnection callConnection = client.getCallConnection(callConnectionId);
CommunicationUserIdentifier participant = new CommunicationUserIdentifier("<user-id>");AddParticipantOptions addOptions = new AddParticipantOptions(participant)    .setInvitationTimeout(Duration.ofSeconds(30));
AddParticipantResult result = callConnection.addParticipant(addOptions);

Transfer Call

java
// Blind transferPhoneNumberIdentifier transferTarget = new PhoneNumberIdentifier("+14255559999");TransferCallToParticipantResult result = callConnection.transferCallToParticipant(transferTarget);

Handle Events (Webhook)

java
import com.azure.communication.callautomation.CallAutomationEventParser;import com.azure.communication.callautomation.models.events.*;
// In your webhook endpointpublic void handleCallback(String requestBody) {    List<CallAutomationEventBase> events = CallAutomationEventParser.parseEvents(requestBody);        for (CallAutomationEventBase event : events) {        if (event instanceof CallConnected) {            CallConnected connected = (CallConnected) event;            System.out.println("Call connected: " + connected.getCallConnectionId());        } else if (event instanceof RecognizeCompleted) {            RecognizeCompleted recognized = (RecognizeCompleted) event;            // Handle DTMF or speech recognition result            DtmfResult dtmfResult = (DtmfResult) recognized.getRecognizeResult();            String tones = dtmfResult.getTones().stream()                .map(DtmfTone::toString)                .collect(Collectors.joining());            System.out.println("DTMF received: " + tones);        } else if (event instanceof PlayCompleted) {            System.out.println("Audio playback completed");        } else if (event instanceof CallDisconnected) {            System.out.println("Call ended");        }    }}

Hang Up Call

java
// Hang up for all participantscallConnection.hangUp(true);
// Hang up only this legcallConnection.hangUp(false);

Error Handling

java
import com.azure.core.exception.HttpResponseException;
try {    client.answerCall(options);} catch (HttpResponseException e) {    if (e.getResponse().getStatusCode() == 404) {        System.out.println("Call not found or already ended");    } else if (e.getResponse().getStatusCode() == 400) {        System.out.println("Invalid request: " + e.getMessage());    }}

Environment Variables

bash
AZURE_COMMUNICATION_ENDPOINT=https://<resource>.communication.azure.com  # Required for all auth methodsAZURE_COMMUNICATION_CONNECTION_STRING=endpoint=https://...;accesskey=...  # Alternative to Entra ID authCALLBACK_BASE_URL=https://your-app.com/api/callbacks  # Required for webhook callbacksAZURE_TOKEN_CREDENTIALS=prod  # Required only if DefaultAzureCredential is used in production

Trigger Phrases

  • "call automation Java", "IVR Java", "interactive voice response"
  • "call recording Java", "DTMF recognition Java"
  • "text to speech call", "speech recognition call"
  • "answer incoming call", "transfer call Java"
  • "Azure Communication Services call automation"

来源与署名

来源:microsoft/skills位于.github/plugins/azure-sdk-java/skills/azure-communication-callautomation-java提交354361d

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架