|
| 1 | +# Azure Communication CallAutomation client library for .NET |
| 2 | + |
| 3 | +This package contains a C# SDK for Azure Communication Call Automation. |
| 4 | + |
| 5 | +[Source code][source] |[Product documentation][product_docs] |
| 6 | +## Getting started |
| 7 | + |
| 8 | +### Install the package |
| 9 | +Install the Azure Communication CallAutomation client library for .NET with [NuGet][nuget]: |
| 10 | + |
| 11 | +```dotnetcli |
| 12 | +dotnet add package Azure.Communication.CallAutomation --prerelease |
| 13 | +``` |
| 14 | + |
| 15 | +### Prerequisites |
| 16 | +You need an [Azure subscription][azure_sub] and a [Communication Service Resource][communication_resource_docs] to use this package. |
| 17 | + |
| 18 | +To create a new Communication Service, you can use the [Azure Portal][communication_resource_create_portal], the [Azure PowerShell][communication_resource_create_power_shell], or the [.NET management client library][communication_resource_create_net]. |
| 19 | + |
| 20 | +### Key concepts |
| 21 | +`CallAutomationClient` provides the functionality to answer incoming call or initialize an outbound call. |
| 22 | + |
| 23 | +### Using statements |
| 24 | +```C# |
| 25 | +using System; |
| 26 | +using System.Collections.Generic; |
| 27 | +using Azure.Communication.CallAutomation; |
| 28 | +``` |
| 29 | + |
| 30 | +### Authenticate the client |
| 31 | +Call Automation client can be authenticated using the connection string acquired from an Azure Communication Resource in the [Azure Portal][azure_portal]. |
| 32 | + |
| 33 | +```C# |
| 34 | +var connectionString = "<connection_string>"; // Find your Communication Services resource in the Azure portal |
| 35 | +CallAutomationClient callAutomationClient = new CallAutomationClient(connectionString); |
| 36 | +``` |
| 37 | + |
| 38 | +Or alternatively using a valid Active Directory token. |
| 39 | +```C# |
| 40 | +var endpoint = new Uri("https://my-resource.communication.azure.com"); |
| 41 | +TokenCredential tokenCredential = new DefaultAzureCredential(); |
| 42 | +var client = new CallAutomationClient(endpoint, tokenCredential); |
| 43 | +``` |
| 44 | + |
| 45 | +## Examples |
| 46 | +### Make a call to a phone number recipient |
| 47 | +To make an outbound call, call the `CreateCall` or `CreateCallAsync` function from the `CallAutomationClient`. |
| 48 | +```C# |
| 49 | +CallSource callSource = new CallSource( |
| 50 | + new CommunicationUserIdentifier("<source-identifier>"), // Your Azure Communication Resource Guid Id used to make a Call |
| 51 | + ); |
| 52 | +callSource.CallerId = new PhoneNumberIdentifier("<caller-id-phonenumber>") // E.164 formatted phone number that's associated to your Azure Communication Resource |
| 53 | +``` |
| 54 | +```C# |
| 55 | +CreateCallResult createCallResult = await callAutomationClient.CreateCallAsync( |
| 56 | + source: callSource, |
| 57 | + targets: new List<CommunicationIdentifier>() { new PhoneNumberIdentifier("<targets-phone-number>") }, // E.164 formatted recipient phone number |
| 58 | + callbackEndpoint: new Uri(TestEnvironment.AppCallbackUrl) |
| 59 | + ); |
| 60 | +Console.WriteLine($"Call connection id: {createCallResult.CallConnectionProperties.CallConnectionId}"); |
| 61 | +``` |
| 62 | + |
| 63 | +### Handle Mid-Connection call back events |
| 64 | +Your app will receive mid-connection call back events via the callbackEndpoint you provided. You will need to write event handler controller to receive the events and direct your app flow based on your business logic. |
| 65 | +```C# |
| 66 | + /// <summary> |
| 67 | + /// Handle call back events. |
| 68 | + /// </summary>> |
| 69 | + [HttpPost] |
| 70 | + [Route("/CallBackEvent")] |
| 71 | + public IActionResult OnMidConnectionCallBackEvent([FromBody] CloudEvent[] events) |
| 72 | + { |
| 73 | + try |
| 74 | + { |
| 75 | + if (events != null) |
| 76 | + { |
| 77 | + // Helper function to parse CloudEvent to a CallAutomation event. |
| 78 | + CallAutomationEventBase callBackEvent = EventParser.Parse(events.FirstOrDefault()); |
| 79 | + |
| 80 | + switch (callBackEvent) |
| 81 | + { |
| 82 | + case CallConnected ev: |
| 83 | + # logic to handle a CallConnected event |
| 84 | + break; |
| 85 | + case CallDisconnected ev: |
| 86 | + # logic to handle a CallDisConnected event |
| 87 | + break; |
| 88 | + case ParticipantsUpdated ev: |
| 89 | + # cast the event into a ParticipantUpdated event and do something with it. Eg. iterate through the participants |
| 90 | + ParticipantsUpdated updatedEvent = (ParticipantsUpdated)ev; |
| 91 | + break; |
| 92 | + case AddParticipantsSucceeded ev: |
| 93 | + # logic to handle an AddParticipantsSucceeded event |
| 94 | + break; |
| 95 | + case AddParticipantsFailed ev: |
| 96 | + # logic to handle an AddParticipantsFailed event |
| 97 | + break; |
| 98 | + case CallTransferAccepted ev: |
| 99 | + # logic to handle CallTransferAccepted event |
| 100 | + break; |
| 101 | + case CallTransferFailed ev: |
| 102 | + # logic to handle CallTransferFailed event |
| 103 | + break; |
| 104 | + default: |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + catch (Exception ex) |
| 110 | + { |
| 111 | + // handle exception |
| 112 | + } |
| 113 | + return Ok(); |
| 114 | + } |
| 115 | +``` |
| 116 | + |
| 117 | +## Troubleshooting |
| 118 | +A `RequestFailedException` is thrown as a service response for any unsuccessful requests. The exception contains information about what response code was returned from the service. |
| 119 | + |
| 120 | +## Next steps |
| 121 | + |
| 122 | +## Contributing |
| 123 | +This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit [cla.microsoft.com][cla]. |
| 124 | + |
| 125 | +This project has adopted the [Microsoft Open Source Code of Conduct ][coc]. For more information see the [Code of Conduct FAQ ][coc_faq] or contact [[email protected]][coc_contact] with any additional questions or comments. |
| 126 | + |
| 127 | +<!-- LINKS --> |
| 128 | +[azure_sub]: https://azure.microsoft.com/free/dotnet/ |
| 129 | +[azure_portal]: https://portal.azure.com |
| 130 | +[cla]: https://cla.microsoft.com |
| 131 | +[coc]: https://opensource.microsoft.com/codeofconduct/ |
| 132 | +[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ |
| 133 | +[coc_contact]: mailto:[email protected] |
| 134 | +[communication_resource_docs]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-azp |
| 135 | +[communication_resource_create_portal]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-azp |
| 136 | +[communication_resource_create_power_shell]: https://docs.microsoft.com/powershell/module/az.communication/new-azcommunicationservice |
| 137 | +[communication_resource_create_net]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-net |
| 138 | +[product_docs]: https://docs.microsoft.com/azure/communication-services/overview |
| 139 | +[nuget]: https://www.nuget.org/ |
| 140 | +[source]: https://github.com/Azure/azure-sdk-for-net/tree/a20e269162fa88a43e5ba0e5bb28f2e76c74a484/sdk/communication/Azure.Communication.CallingServer/src |
0 commit comments