Edit

Share via


Control and steer calls with Call Automation

Call Automation uses a REST API interface to receive requests for actions and provide responses to notify whether the request was successfully submitted or not. Because of the asynchronous nature of calling, most actions have corresponding events that are triggered when the action finishes successfully or fails. This article covers the actions that are available for steering calls, like CreateCall, Transfer, and Redirect, and managing participants. Sample code shows how to invoke the particular action. Sequence diagrams describe the events that are expected after you invoke an action. The diagrams help you visualize how to program your service application with Call Automation.

Call Automation supports other actions to manage call media and recording that have separate articles.

Prerequisites

  • Read the Call Automation concepts article that describes the action-event programming model and event callbacks.
  • Learn about the user identifiers like CommunicationUserIdentifier and PhoneNumberIdentifier that are used in this article.

For all the code samples, client is the CallAutomationClient object that you can create, as shown. Also, callConnection is the CallConnection object that you obtain from the Answer or CreateCall response. You can also obtain it from callback events that your application receives.

var client = new CallAutomationClient("<resource_connection_string>"); 

Make an outbound call

You can place a 1:1 or group call to a communication user or phone number (a public number or one that Azure Communication Services owns). When you call a public-switched telephone network (PSTN) endpoint, you also need to provide a phone number to use as the source caller ID and that shows as the call notification to the target PSTN endpoint.

To place a call to an Azure Communication Services user, you need to provide a CommunicationUserIdentifier object instead of PhoneNumberIdentifier.

Uri callbackUri = new Uri("https://<myendpoint>/Events"); //the callback endpoint where you want to receive subsequent events 
var callerIdNumber = new PhoneNumberIdentifier("+16044561234"); // This is the Azure Communication Services provisioned phone number for the caller  
var callThisPerson = new CallInvite(new PhoneNumberIdentifier("+16041234567"), callerIdNumber); // person to call
CreateCallResult response = await client.CreateCallAsync(callThisPerson, callbackUri);

When you make a group call that includes a phone number, you must provide a phone number to use as a caller ID number for the PSTN endpoint.

Uri callbackUri = new Uri("https://<myendpoint>/Events"); //the callback endpoint where you want to receive subsequent events 
var pstnEndpoint = new PhoneNumberIdentifier("+16041234567");
var voipEndpoint = new CommunicationUserIdentifier("<user_id_of_target>"); //user id looks like 8:a1b1c1-...
var groupCallOptions = new CreateGroupCallOptions(new List<CommunicationIdentifier>{ pstnEndpoint, voipEndpoint }, callbackUri)
{
    SourceCallerIdNumber = new PhoneNumberIdentifier("+16044561234"), // This is the Azure Communication Services provisioned phone number for the caller
};
CreateCallResult response = await client.CreateGroupCallAsync(groupCallOptions);

The response provides you with the CallConnection object that you can use to take further actions on this call after it connects. After the call is answered, two events are published to the callback endpoint that you provided earlier:

  • CallConnected: Notifies that the call was established with the caller.

  • ParticipantsUpdated: Contains the latest list of participants on the call.

    Diagram that shows the sequence for placing an outbound call.

If the call fails, you receive a CallDisconnected event and a CreateCallFailed event with error codes for further troubleshooting. For more information on error codes, see Troubleshooting call end response codes.

Connect to a call

The connect action enables your service to establish a connection with an ongoing call and take actions on it. This capability is useful to manage a Rooms call or when client applications start a 1:1 or group call in which Call Automation isn't a part. Use the CallLocator property to establish the connection. The type options are ServerCallLocator, GroupCallLocator, and RoomCallLocator. You can find these IDs when the call is originally established or a Room is created, and can also be published as part of CallStarted event.

To connect to any 1:1 or group call, use ServerCallLocator. If you used GroupCallId to start a call, you can also use GroupCallLocator.

Uri callbackUri = new Uri("https://<myendpoint>/Events"); //the callback endpoint where you want to receive subsequent events
CallLocator serverCallLocator = new ServerCallLocator("<ServerCallId>");
ConnectCallResult response = await client.ConnectCallAsync(serverCallLocator, callbackUri);

To connect to a Rooms call, use RoomCallLocator, which takes RoomId. Learn more about Rooms and how you can use the Call Automation API to manage an ongoing Rooms call.

Uri callbackUri = new Uri("https://<myendpoint>/Events"); //the callback endpoint where you want to receive subsequent events
CallLocator roomCallLocator = new RoomCallLocator("<RoomId>");
ConnectCallResult response = await client.ConnectCallAsync(roomCallLocator, callbackUri);

A successful response provides you with a CallConnection object that you can use to take further actions on this call. Two events are published to the callback endpoint that you provided earlier:

  • CallConnected: Notifies that you successfully connected to the call.
  • ParticipantsUpdated: Contains the latest list of participants on the call.

At any point after a successful connection, if your service disconnects from this call, a CallDisconnected event notifies you. Failure to connect to the call in the first place results in the ConnectFailed event.

Diagram that shows the sequence for connecting to a call.

Answer an incoming call

After you subscribe to receive incoming call notifications to your resource, you can answer an incoming call. When you answer a call, you need to provide a callback URL. Azure Communication Services posts all subsequent events about this call to that URL.

string incomingCallContext = "<IncomingCallContext_From_IncomingCall_Event>"; 
Uri callBackUri = new Uri("https://<myendpoint_where_I_want_to_receive_callback_events"); 

var answerCallOptions = new AnswerCallOptions(incomingCallContext, callBackUri);  
AnswerCallResult answerResponse = await client.AnswerCallAsync(answerCallOptions);
CallConnection callConnection = answerResponse.CallConnection; 

The response provides you with a CallConnection object that you can use to take further actions on this call after it connects. After the call is answered, two events are published to the callback endpoint that you provided earlier:

  • CallConnected: Notifies that the call was established with the caller.
  • ParticipantsUpdated: Contains the latest list of participants on the call.

Diagram that shows the sequence for answering an incoming call.

If the answer operation fails, you receive an AnswerFailed event with error codes for further troubleshooting. For more information on error codes, see Troubleshooting call end response codes.

Reject a call

You can reject an incoming call. Reasons for the rejection are None, Busy, or Forbidden. If nothing is provided, the default is None.

string incomingCallContext = "<IncomingCallContext_From_IncomingCall_Event>"; 
var rejectOption = new RejectCallOptions(incomingCallContext); 
rejectOption.CallRejectReason = CallRejectReason.Forbidden; 
_ = await client.RejectCallAsync(rejectOption); 

No events are published for the reject action.

Redirect a call

You can redirect an incoming call to another endpoint without answering it. Redirecting a call removes your application's ability to control the call by using Call Automation.

string incomingCallContext = "<IncomingCallContext_From_IncomingCall_Event>"; 
var target = new CallInvite(new CommunicationUserIdentifier("<user_id_of_target>")); //user id looks like 8:a1b1c1-... 
_ = await client.RedirectCallAsync(incomingCallContext, target); 

To redirect the call to a phone number, construct the target and caller ID with PhoneNumberIdentifier.

var callerIdNumber = new PhoneNumberIdentifier("+16044561234"); // This is the Azure Communication Services provisioned phone number for the caller
var target = new CallInvite(new PhoneNumberIdentifier("+16041234567"), callerIdNumber);

No events are published for redirect. If the target is an Azure Communication Services user or a phone number that your resource owns, it generates a new IncomingCall event with the to field set to the target that you specify.

Transfer a participant in a call

When your application answers a call or places an outbound call to an endpoint, your app can transfer the endpoint to another destination endpoint. Transferring a 1:1 call removes your application from the call and removes its ability to control the call by using Call Automation. The call invite to the target shows the caller ID of the endpoint being transferred. Providing a custom caller ID isn't supported.

var transferDestination = new CommunicationUserIdentifier("<user_id>"); 
var transferOption = new TransferToParticipantOptions(transferDestination) {
    OperationContext = "<Your_context>",
    OperationCallbackUri = new Uri("<uri_endpoint>") // Sending event to a non-default endpoint.
};
// adding customCallingContext
transferOption.CustomCallingContext.AddVoip("customVoipHeader1", "customVoipHeaderValue1");
transferOption.CustomCallingContext.AddVoip("customVoipHeader2", "customVoipHeaderValue2");

TransferCallToParticipantResult result = await callConnection.TransferCallToParticipantAsync(transferOption);

When your application answers a group call, places an outbound group call to an endpoint, or adds a participant to a 1:1 call, the app can transfer the endpoint from the call to another destination endpoint, except for the Call Automation endpoint. Transferring a participant in a group call removes the endpoint being transferred from the call. The call invite to the target shows the caller ID of the endpoint being transferred. Providing a custom caller ID isn't supported.

// Transfer User
var transferDestination = new CommunicationUserIdentifier("<user_id>");
var transferee = new CommunicationUserIdentifier("<transferee_user_id>"); 
var transferOption = new TransferToParticipantOptions(transferDestination);
transferOption.Transferee = transferee;

// adding customCallingContext
transferOption.CustomCallingContext.AddVoip("customVoipHeader1", "customVoipHeaderValue1");
transferOption.CustomCallingContext.AddVoip("customVoipHeader2", "customVoipHeaderValue2");

transferOption.OperationContext = "<Your_context>";
transferOption.OperationCallbackUri = new Uri("<uri_endpoint>");
TransferCallToParticipantResult result = await callConnection.TransferCallToParticipantAsync(transferOption);

// Transfer PSTN User
var transferDestination = new PhoneNumberIdentifier("<target_phoneNumber>");
var transferee = new PhoneNumberIdentifier("<transferee_phoneNumber>"); 
var transferOption = new TransferToParticipantOptions(transferDestination);
transferOption.Transferee = transferee;

// adding customCallingContext
transferOption.CustomCallingContext.AddSipUui("uuivalue");
transferOption.CustomCallingContext.AddSipX("header1", "headerValue");

transferOption.OperationContext = "<Your_context>";

// Sending event to a non-default endpoint.
transferOption.OperationCallbackUri = new Uri("<uri_endpoint>");

TransferCallToParticipantResult result = await callConnection.TransferCallToParticipantAsync(transferOption);

The sequence diagram shows the expected flow when your application places an outbound call and then transfers it to another endpoint.

Diagram that shows the sequence for placing a 1:1 call and then transferring it.

Add a participant to a call

You can add a participant such as an Azure Communication Services user or a phone number to an existing call. When you add a phone number, it's mandatory to provide a caller ID. This caller ID is shown on call notification to the added participant.

// Add user
var addThisPerson = new CallInvite(new CommunicationUserIdentifier("<user_id>"));
// add custom calling context
addThisPerson.CustomCallingContext.AddVoip("myHeader", "myValue");
AddParticipantsResult result = await callConnection.AddParticipantAsync(addThisPerson);

// Add PSTN user
var callerIdNumber = new PhoneNumberIdentifier("+16044561234"); // This is the Azure Communication Services provisioned phone number for the caller
var addThisPerson = new CallInvite(new PhoneNumberIdentifier("+16041234567"), callerIdNumber);
// add custom calling context
addThisPerson.CustomCallingContext.AddSipUui("value");
addThisPerson.CustomCallingContext.AddSipX("header1", "customSipHeaderValue1");

// Use option bag to set optional parameters
var addParticipantOptions = new AddParticipantOptions(new CallInvite(addThisPerson))
{
    InvitationTimeoutInSeconds = 60,
    OperationContext = "operationContext",
    OperationCallbackUri = new Uri("uri_endpoint"); // Sending event to a non-default endpoint.
};

AddParticipantsResult result = await callConnection.AddParticipantAsync(addParticipantOptions); 

To add an Azure Communication Services user, provide CommunicationUserIdentifier instead of PhoneNumberIdentifier. Caller ID isn't mandatory in this case.

Next, AddParticipant publishes an AddParticipantSucceeded or AddParticipantFailed event, along with ParticipantUpdated that provides the latest list of participants on the call.

Diagram that shows the sequence for adding a participant to the call.

Cancel an add participant request

// add a participant
var addThisPerson = new CallInvite(new CommunicationUserIdentifier("<user_id>"));
var addParticipantResponse = await callConnection.AddParticipantAsync(addThisPerson);

// cancel the request with optional parameters
var cancelAddParticipantOperationOptions = new CancelAddParticipantOperationOptions(addParticipantResponse.Value.InvitationId)
{
    OperationContext = "operationContext",
    OperationCallbackUri = new Uri("uri_endpoint"); // Sending event to a non-default endpoint.
}
await callConnection.CancelAddParticipantOperationAsync(cancelAddParticipantOperationOptions);

Remove a participant from a call

var removeThisUser = new CommunicationUserIdentifier("<user_id>"); 

// remove a participant from the call with optional parameters
var removeParticipantOptions = new RemoveParticipantOptions(removeThisUser)
{
    OperationContext = "operationContext",
    OperationCallbackUri = new Uri("uri_endpoint"); // Sending event to a non-default endpoint.
}

RemoveParticipantsResult result = await callConnection.RemoveParticipantAsync(removeParticipantOptions);

RemoveParticipant publishes a RemoveParticipantSucceeded or RemoveParticipantFailed event, along with a ParticipantUpdated event that provides the latest list of participants on the call. The removed participant is omitted from the list.

Diagram that shows the sequence for removing a participant from a call.

Hang up on a call

You can use the hangUp action to remove your application from the call or to terminate a group call by setting the forEveryone parameter to true. For a 1:1 call, hangUp terminates the call with the other participant by default.

_ = await callConnection.HangUpAsync(forEveryone: true); 

The CallDisconnected event is published after the hangUp action successfully finishes.

Get information about a call participant

CallParticipant participantInfo = await callConnection.GetParticipantAsync(new CommunicationUserIdentifier("<user_id>"));

Get information about all call participants

List<CallParticipant> participantList = (await callConnection.GetParticipantsAsync()).Value.ToList(); 

Get the latest information about a call

CallConnectionProperties callConnectionProperties = await callConnection.GetCallConnectionPropertiesAsync();