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.
Add this skill
npx mdskills install sickn33/azure-communication-callautomation-javaComprehensive Java SDK reference with excellent code examples for IVR, call automation, and recording workflows
1---2name: azure-communication-callautomation-java3description: 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.4package: com.azure:azure-communication-callautomation5---67# Azure Communication Call Automation (Java)89Build server-side call automation workflows including IVR systems, call routing, recording, and AI-powered interactions.1011## Installation1213```xml14<dependency>15 <groupId>com.azure</groupId>16 <artifactId>azure-communication-callautomation</artifactId>17 <version>1.6.0</version>18</dependency>19```2021## Client Creation2223```java24import com.azure.communication.callautomation.CallAutomationClient;25import com.azure.communication.callautomation.CallAutomationClientBuilder;26import com.azure.identity.DefaultAzureCredentialBuilder;2728// With DefaultAzureCredential29CallAutomationClient client = new CallAutomationClientBuilder()30 .endpoint("https://<resource>.communication.azure.com")31 .credential(new DefaultAzureCredentialBuilder().build())32 .buildClient();3334// With connection string35CallAutomationClient client = new CallAutomationClientBuilder()36 .connectionString("<connection-string>")37 .buildClient();38```3940## Key Concepts4142| Class | Purpose |43|-------|---------|44| `CallAutomationClient` | Make calls, answer/reject incoming calls, redirect calls |45| `CallConnection` | Actions in established calls (add participants, terminate) |46| `CallMedia` | Media operations (play audio, recognize DTMF/speech) |47| `CallRecording` | Start/stop/pause recording |48| `CallAutomationEventParser` | Parse webhook events from ACS |4950## Create Outbound Call5152```java53import com.azure.communication.callautomation.models.*;54import com.azure.communication.common.CommunicationUserIdentifier;55import com.azure.communication.common.PhoneNumberIdentifier;5657// Call to PSTN number58PhoneNumberIdentifier target = new PhoneNumberIdentifier("+14255551234");59PhoneNumberIdentifier caller = new PhoneNumberIdentifier("+14255550100");6061CreateCallOptions options = new CreateCallOptions(62 new CommunicationUserIdentifier("<user-id>"), // Source63 List.of(target)) // Targets64 .setSourceCallerId(caller)65 .setCallbackUrl("https://your-app.com/api/callbacks");6667CreateCallResult result = client.createCall(options);68String callConnectionId = result.getCallConnectionProperties().getCallConnectionId();69```7071## Answer Incoming Call7273```java74// From Event Grid webhook - IncomingCall event75String incomingCallContext = "<incoming-call-context-from-event>";7677AnswerCallOptions options = new AnswerCallOptions(78 incomingCallContext,79 "https://your-app.com/api/callbacks");8081AnswerCallResult result = client.answerCall(options);82CallConnection callConnection = result.getCallConnection();83```8485## Play Audio (Text-to-Speech)8687```java88CallConnection callConnection = client.getCallConnection(callConnectionId);89CallMedia callMedia = callConnection.getCallMedia();9091// Play text-to-speech92TextSource textSource = new TextSource()93 .setText("Welcome to Contoso. Press 1 for sales, 2 for support.")94 .setVoiceName("en-US-JennyNeural");9596PlayOptions playOptions = new PlayOptions(97 List.of(textSource),98 List.of(new CommunicationUserIdentifier("<target-user>")));99100callMedia.play(playOptions);101102// Play audio file103FileSource fileSource = new FileSource()104 .setUrl("https://storage.blob.core.windows.net/audio/greeting.wav");105106callMedia.play(new PlayOptions(List.of(fileSource), List.of(target)));107```108109## Recognize DTMF Input110111```java112// Recognize DTMF tones113DtmfTone stopTones = DtmfTone.POUND;114115CallMediaRecognizeDtmfOptions recognizeOptions = new CallMediaRecognizeDtmfOptions(116 new CommunicationUserIdentifier("<target-user>"),117 5) // Max tones to collect118 .setInterToneTimeout(Duration.ofSeconds(5))119 .setStopTones(List.of(stopTones))120 .setInitialSilenceTimeout(Duration.ofSeconds(15))121 .setPlayPrompt(new TextSource().setText("Enter your account number followed by pound."));122123callMedia.startRecognizing(recognizeOptions);124```125126## Recognize Speech127128```java129// Speech recognition with AI130CallMediaRecognizeSpeechOptions speechOptions = new CallMediaRecognizeSpeechOptions(131 new CommunicationUserIdentifier("<target-user>"))132 .setEndSilenceTimeout(Duration.ofSeconds(2))133 .setSpeechLanguage("en-US")134 .setPlayPrompt(new TextSource().setText("How can I help you today?"));135136callMedia.startRecognizing(speechOptions);137```138139## Call Recording140141```java142CallRecording callRecording = client.getCallRecording();143144// Start recording145StartRecordingOptions recordingOptions = new StartRecordingOptions(146 new ServerCallLocator("<server-call-id>"))147 .setRecordingChannel(RecordingChannel.MIXED)148 .setRecordingContent(RecordingContent.AUDIO_VIDEO)149 .setRecordingFormat(RecordingFormat.MP4);150151RecordingStateResult recordingResult = callRecording.start(recordingOptions);152String recordingId = recordingResult.getRecordingId();153154// Pause/resume/stop155callRecording.pause(recordingId);156callRecording.resume(recordingId);157callRecording.stop(recordingId);158159// Download recording (after RecordingFileStatusUpdated event)160callRecording.downloadTo(recordingUrl, Paths.get("recording.mp4"));161```162163## Add Participant to Call164165```java166CallConnection callConnection = client.getCallConnection(callConnectionId);167168CommunicationUserIdentifier participant = new CommunicationUserIdentifier("<user-id>");169AddParticipantOptions addOptions = new AddParticipantOptions(participant)170 .setInvitationTimeout(Duration.ofSeconds(30));171172AddParticipantResult result = callConnection.addParticipant(addOptions);173```174175## Transfer Call176177```java178// Blind transfer179PhoneNumberIdentifier transferTarget = new PhoneNumberIdentifier("+14255559999");180TransferCallToParticipantResult result = callConnection.transferCallToParticipant(transferTarget);181```182183## Handle Events (Webhook)184185```java186import com.azure.communication.callautomation.CallAutomationEventParser;187import com.azure.communication.callautomation.models.events.*;188189// In your webhook endpoint190public void handleCallback(String requestBody) {191 List<CallAutomationEventBase> events = CallAutomationEventParser.parseEvents(requestBody);192193 for (CallAutomationEventBase event : events) {194 if (event instanceof CallConnected) {195 CallConnected connected = (CallConnected) event;196 System.out.println("Call connected: " + connected.getCallConnectionId());197 } else if (event instanceof RecognizeCompleted) {198 RecognizeCompleted recognized = (RecognizeCompleted) event;199 // Handle DTMF or speech recognition result200 DtmfResult dtmfResult = (DtmfResult) recognized.getRecognizeResult();201 String tones = dtmfResult.getTones().stream()202 .map(DtmfTone::toString)203 .collect(Collectors.joining());204 System.out.println("DTMF received: " + tones);205 } else if (event instanceof PlayCompleted) {206 System.out.println("Audio playback completed");207 } else if (event instanceof CallDisconnected) {208 System.out.println("Call ended");209 }210 }211}212```213214## Hang Up Call215216```java217// Hang up for all participants218callConnection.hangUp(true);219220// Hang up only this leg221callConnection.hangUp(false);222```223224## Error Handling225226```java227import com.azure.core.exception.HttpResponseException;228229try {230 client.answerCall(options);231} catch (HttpResponseException e) {232 if (e.getResponse().getStatusCode() == 404) {233 System.out.println("Call not found or already ended");234 } else if (e.getResponse().getStatusCode() == 400) {235 System.out.println("Invalid request: " + e.getMessage());236 }237}238```239240## Environment Variables241242```bash243AZURE_COMMUNICATION_ENDPOINT=https://<resource>.communication.azure.com244AZURE_COMMUNICATION_CONNECTION_STRING=endpoint=https://...;accesskey=...245CALLBACK_BASE_URL=https://your-app.com/api/callbacks246```247248## Trigger Phrases249250- "call automation Java", "IVR Java", "interactive voice response"251- "call recording Java", "DTMF recognition Java"252- "text to speech call", "speech recognition call"253- "answer incoming call", "transfer call Java"254- "Azure Communication Services call automation"255
Full transparency — inspect the skill content before installing.