azure-sdk-for-js/sdk/communication/communication-call-automation
Matthew Podwysocki 0188ca180f
[communication] Move @azure/communication-call-automation to ESM/vitest (#31680)
### Packages impacted by this PR

- @azure/communication-call-automation

### Issues associated with this PR

- https://github.com/Azure/azure-sdk-for-js/issues/31338

### Describe the problem that is addressed by this PR

Migrates package to ESM/vitest. Note that ServiceBus has issues with
Browser loads that we can fix in a later revision as Buffer is not
defined when you try to import any files. But then including Buffer then
also causes issues with the unit tests.

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
2024-11-18 18:29:38 -05:00
..
recordings
review
samples-dev
src
swagger
test
.gitignore
CHANGELOG.md
LICENSE
README.md
api-extractor.json
assets.json
eslint.config.mjs
package.json
sample.env
test.env
tests.yml
tsconfig.browser.config.json
tsconfig.json
tsdoc.json
vitest.browser.config.ts
vitest.config.ts

README.md

Azure Communication Call Automation client library for JavaScript

This package contains a JavaScript SDK for Azure Communication Call Automation. Call Automation provides developers the ability to build server-based, intelligent call workflows, and call recording for voice and PSTN channels.

Overview of Call Automation | Product documentation

Getting started

Prerequisites

Installing

npm install @azure/communication-call-automation

Browser support

JavaScript Bundle

To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our bundling documentation.

Key concepts

Name Description
CallAutomationClient CallAutomationClient is the primary interface for developers using this client library. It can be used to initiate calls by createCall or answerCall.
CallConnection CallConnection represents a ongoing call. Once the call is established with createCall or answerCall, further actions can be performed for the call, such as transfer or addParticipant.
CallMedia CallMedia can be used to do media related actions, such as play, to play media file. This can be retrieved from established CallConnection.
CallRecording CallRecording can be used to do recording related actions, such as startRecording. This can be retrieved from CallAutomationClient.
Callback Events Callback events are events sent back during duration of the call. It gives information and state of the call, such as CallConnected. CallbackUrl must be provided during createCall and answerCall, and callback events will be sent to this url. You can use callAutomationEventParser to parse these events when it arrives.
Incoming Call Event When incoming call happens (that can be answered with answerCall), incoming call eventgrid event will be sent. This is different from Callback events above, and should be setup on Azure portal. See Incoming Call for detail.
CallAutomationEventProcessor CallAutomationEventProcessor is a convinient way to handle mid-call callback events such as CallConnected. This will ensure correlation between call and events more easily. See below example for its usage.

Examples

Initialize CallAutomationClient

import { CallAutomationClient } from '@azure/communication-call-automation';
import { DefaultAzureCredential } from "@azure/identity"; 

// Your unique Azure Communication service endpoint
const credential = new DefaultAzureCredential(); 
const endpointUrl = '<ENDPOINT>' 
const callAutomationClient = new CallAutomationClient(endpointUrl, credential); 

Create Call

import { CommunicationUserIdentifier } from "@azure/communication-common";
import { CallAutomationClient, CallInvite } from '@azure/communication-call-automation';

// target endpoint for ACS User
const target: CommunicationUserIdentifier = {
  communicationUserId:
    "8:acs:...",
}

// make invitation
const callInvite: CallInvite = {
   targetParticipant:target
};

// callback url to recieve callback events
const callbackUrl = "https://<MY-EVENT-HANDLER-URL>/events";

// send out the invitation, creating call
const response = callAutomationClient.createCall(callInvite, callbackUrl);

Play Media

// from callconnection of response above, play media of media file
const myFile: FileSource = { uri: "https://<FILE-SOURCE>/<SOME-FILE>.wav" }
const response = callConnection.getCallMedia().playToAll(myFile);

Handle Mid-Connection callback events

To easily handle mid-connection events, Call Automation's SDK provides easier way to handle these events. Take a look at CallAutomationEventProcessor. This will ensure correlation between call and events more easily.

const eventProcessor: CallAutomationEventProcessor = await callAutomationClient.getEventProcessor();
eventProcessor.processEvents(incomingEvent);

ProcessEvents is required for EventProcessor to work. After event is being consumed by EventProcessor, you can start using its feature.

See below for example: where you are making a call with CreateCall, and wait for CallConnected event of the call.

// send out the invitation, creating call
const callInvite = new CallInvite(target);
const callbackUrl = "https://<MY-EVENT-HANDLER-URL>/events";
const callResult = callAutomationClient.createCall(callInvite, callbackUrl);

// giving 30 seconds timeout for waiting on createCall's event, 'CallConnected'
const createCallEventResult : CreateCallEventResult = await callResult.waitForEventProcessor(undefined, 30000);
// once this returns, call is now established!

// check if it was successful
if (createCallEventResult.isSuccess)
{
  // work with callConnected event
  const callConnectedEvent : CallConnected = createCallEventResult.successResult!;
}

Troubleshooting

Next steps

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.