@azure/communication-call-automation
Version:
Azure client library for Azure Communication Call Automation services
290 lines • 12.8 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { createCommunicationAuthPolicy } from "@azure/communication-common";
import { CallMedia } from "./callMedia.js";
import { CallAutomationApiClient } from "./generated/src/index.js";
import { CallConnectionImpl } from "./generated/src/operations/index.js";
import { callParticipantConverter, communicationIdentifierConverter, communicationIdentifierModelConverter, communicationUserIdentifierConverter, phoneNumberIdentifierConverter, PhoneNumberIdentifierModelConverter, teamsPhoneCallDetailsModelConverter, } from "./utli/converters.js";
import { randomUUID } from "@azure/core-util";
/**
* CallConnection class represents call connection based APIs.
*/
export class CallConnection {
callConnectionId;
callConnection;
callAutomationApiClient;
endpoint;
credential;
callAutomationApiClientOptions;
constructor(callConnectionId, endpoint, credential, options) {
this.callAutomationApiClient = new CallAutomationApiClient(endpoint, options);
const authPolicy = createCommunicationAuthPolicy(credential);
this.callAutomationApiClient.pipeline.addPolicy(authPolicy);
this.callConnectionId = callConnectionId;
this.callConnection = new CallConnectionImpl(this.callAutomationApiClient);
this.endpoint = endpoint;
this.credential = credential;
this.callAutomationApiClientOptions = options;
}
/**
* Initializes a new instance of CallMedia.
*/
getCallMedia() {
return new CallMedia(this.callConnectionId, this.endpoint, this.credential, this.callAutomationApiClientOptions);
}
/**
* Get call connection properties of the call
*/
async getCallConnectionProperties(options = {}) {
const { targets, sourceCallerIdNumber, answeredBy, source, answeredFor, ...result } = await this.callConnection.getCall(this.callConnectionId, options);
const callConnectionProperties = {
...result,
source: source ? communicationIdentifierConverter(source) : undefined,
answeredby: communicationUserIdentifierConverter(answeredBy),
answeredFor: answeredFor ? phoneNumberIdentifierConverter(answeredFor) : undefined,
targetParticipants: targets?.map((target) => communicationIdentifierConverter(target)),
sourceCallerIdNumber: sourceCallerIdNumber
? phoneNumberIdentifierConverter(sourceCallerIdNumber)
: undefined,
};
return callConnectionProperties;
}
/**
* Hang up the call for itself or terminate the whole call.
*
* @param isForEveryOne - Determine if every one in the call would be hung up or not.
*/
async hangUp(isForEveryone, options = {}) {
if (isForEveryone) {
const optionsInternal = {
...options,
repeatabilityFirstSent: new Date(),
repeatabilityRequestID: randomUUID(),
};
await this.callConnection.terminateCall(this.callConnectionId, optionsInternal);
}
else {
await this.callConnection.hangupCall(this.callConnectionId, options);
}
return;
}
/**
* Get a participant from the call
*
* @param targetParticipant - The communication identifier of requested participant.
*/
async getParticipant(targetParticipant, options = {}) {
let rawId = communicationIdentifierModelConverter(targetParticipant).rawId;
rawId = rawId === undefined ? "" : rawId;
const result = await this.callConnection.getParticipant(this.callConnectionId, rawId, options);
const callParticipant = {
identifier: result.identifier
? communicationIdentifierConverter(result.identifier)
: undefined,
isMuted: result.isMuted,
isOnHold: result.isOnHold,
};
return callParticipant;
}
/**
* Get all participants from the call
*/
async listParticipants(options = {}) {
const result = this.callConnection.listParticipants(this.callConnectionId, options);
const participants = [];
const pages = result?.byPage();
for await (const page of pages) {
for (const participant of page) {
participants.push(callParticipantConverter(participant));
}
}
const listParticipantResponse = {
...result,
values: participants,
};
return listParticipantResponse;
}
createCustomCallingContextInternal(customCallingContext) {
const sipHeaders = {};
const voipHeaders = {};
let teamsPhoneCallDetails = undefined;
if (customCallingContext) {
for (const header of customCallingContext) {
if (header.kind === "sipuui") {
sipHeaders[`User-To-User`] = header.value;
}
else if (header.kind === "sipx") {
if (header.sipHeaderPrefix === "X-") {
sipHeaders[`X-${header.key}`] = header.value;
}
else {
sipHeaders[`X-MS-Custom-${header.key}`] = header.value;
}
}
else if (header.kind === "voip") {
voipHeaders[`${header.key}`] = header.value;
}
else if (header.kind === "TeamsPhoneCallDetails") {
teamsPhoneCallDetails = teamsPhoneCallDetailsModelConverter(header);
}
}
}
return {
sipHeaders: sipHeaders,
voipHeaders: voipHeaders,
teamsPhoneCallDetails: teamsPhoneCallDetails,
};
}
/**
* Add a participant to the call
*
* @param participant - The participant is going to be added.
*/
async addParticipant(targetParticipant, options = {}) {
const addParticipantRequest = {
participantToAdd: communicationIdentifierModelConverter(targetParticipant.targetParticipant),
sourceCallerIdNumber: PhoneNumberIdentifierModelConverter(targetParticipant.sourceCallIdNumber),
sourceDisplayName: targetParticipant.sourceDisplayName,
invitationTimeoutInSeconds: options.invitationTimeoutInSeconds,
operationContext: options.operationContext,
operationCallbackUri: options.operationCallbackUrl,
customCallingContext: this.createCustomCallingContextInternal(targetParticipant.customCallingContext),
};
const optionsInternal = {
...options,
repeatabilityFirstSent: new Date(),
repeatabilityRequestID: randomUUID(),
};
const result = await this.callConnection.addParticipant(this.callConnectionId, addParticipantRequest, optionsInternal);
const addParticipantsResult = {
...result,
participant: {
...result.participant,
identifier: result.participant?.identifier
? communicationIdentifierConverter(result.participant?.identifier)
: undefined,
},
};
return addParticipantsResult;
}
/**
* Transfer the call to a target participant
*
* @param targetParticipant - The target to be transferred to.
*/
async transferCallToParticipant(targetParticipant, options = {}) {
const transferToParticipantRequest = {
targetParticipant: communicationIdentifierModelConverter(targetParticipant),
operationContext: options.operationContext ? options.operationContext : randomUUID(),
operationCallbackUri: options.operationCallbackUrl,
transferee: options.transferee && communicationIdentifierModelConverter(options.transferee),
customCallingContext: this.createCustomCallingContextInternal(options.customCallingContext),
sourceCallerIdNumber: PhoneNumberIdentifierModelConverter(options.sourceCallIdNumber),
};
const optionsInternal = {
...options,
repeatabilityFirstSent: new Date(),
repeatabilityRequestID: randomUUID(),
};
const result = await this.callConnection.transferToParticipant(this.callConnectionId, transferToParticipantRequest, optionsInternal);
const transferCallResult = {
...result,
};
return transferCallResult;
}
/**
* Remove a participant from the call
*
* @param participant - The participant is going to be removed from the call.
*/
async removeParticipant(participant, options = {}) {
const removeParticipantRequest = {
participantToRemove: communicationIdentifierModelConverter(participant),
operationContext: options.operationContext ? options.operationContext : randomUUID(),
operationCallbackUri: options.operationCallbackUrl,
};
const optionsInternal = {
...options,
repeatabilityFirstSent: new Date(),
repeatabilityRequestID: randomUUID(),
};
const result = await this.callConnection.removeParticipant(this.callConnectionId, removeParticipantRequest, optionsInternal);
const removeParticipantsResult = {
...result,
};
return removeParticipantsResult;
}
/**
* Mute participant from the call.
*
* @param participant - Participant to be muted from the call.
* @param options - Additional attributes for mute participant.
*/
async muteParticipant(participant, options = {}) {
const muteParticipantsRequest = {
targetParticipants: [communicationIdentifierModelConverter(participant)],
operationContext: options.operationContext,
};
const optionsInternal = {
...options,
repeatabilityFirstSent: new Date(),
repeatabilityRequestID: randomUUID(),
};
const result = await this.callConnection.mute(this.callConnectionId, muteParticipantsRequest, optionsInternal);
const muteParticipantResult = {
...result,
};
return muteParticipantResult;
}
/** Cancel add participant request.
*
* @param invitationId - Invitation ID used to cancel the add participant request.
*/
async cancelAddParticipantOperation(invitationId, options = {}) {
const { operationContext, operationCallbackUrl: operationCallbackUri, ...operationOptions } = options;
const cancelAddParticipantRequest = {
invitationId,
operationContext: operationContext ? operationContext : randomUUID(),
operationCallbackUri,
};
const optionsInternal = {
...operationOptions,
repeatabilityFirstSent: new Date(),
repeatabilityRequestID: randomUUID(),
};
const result = await this.callConnection.cancelAddParticipant(this.callConnectionId, cancelAddParticipantRequest, optionsInternal);
const cancelAddParticipantResult = {
...result,
};
return cancelAddParticipantResult;
}
/**
* Move participants to the call.
*
* @param targetParticipants - The participants to move to the call.
* @param fromCall - The CallConnectionId for the call you want to move the participant from.
* @param options - Additional options for moving participants.
*/
async moveParticipants(targetParticipants, fromCall, options = {}) {
const { operationContext, operationCallbackUrl: operationCallbackUri, ...operationOptions } = options;
const moveParticipantsRequest = {
targetParticipants: targetParticipants.map((participant) => communicationIdentifierModelConverter(participant)),
fromCall,
operationContext: operationContext ? operationContext : randomUUID(),
operationCallbackUri,
};
const optionsInternal = {
...operationOptions,
repeatabilityFirstSent: new Date(),
repeatabilityRequestID: randomUUID(),
};
const result = await this.callConnection.moveParticipants(this.callConnectionId, moveParticipantsRequest, optionsInternal);
const moveParticipantsResult = {
...result,
participants: result.participants?.map((participant) => callParticipantConverter(participant)),
fromCall: fromCall,
};
return moveParticipantsResult;
}
}
//# sourceMappingURL=callConnection.js.map