![]() |
VOOZH | about |
dotnet add package Azure.Communication.Chat --version 1.4.0
NuGet\Install-Package Azure.Communication.Chat -Version 1.4.0
<PackageReference Include="Azure.Communication.Chat" Version="1.4.0" />
<PackageVersion Include="Azure.Communication.Chat" Version="1.4.0" />Directory.Packages.props
<PackageReference Include="Azure.Communication.Chat" />Project file
paket add Azure.Communication.Chat --version 1.4.0
#r "nuget: Azure.Communication.Chat, 1.4.0"
#:package Azure.Communication.Chat@1.4.0
#addin nuget:?package=Azure.Communication.Chat&version=1.4.0Install as a Cake Addin
#tool nuget:?package=Azure.Communication.Chat&version=1.4.0Install as a Cake Tool
This package contains a C# SDK for Azure Communication Services for chat.
Source code | Package (NuGet) | Product documentation
Install the Azure Communication Chat client library for .NET with NuGet:
dotnet add package Azure.Communication.Chat
You need an Azure subscription and a Communication Service Resource to use this package.
To create a new Communication Service, you can use the Azure Portal, the Azure PowerShell, or the .NET management client library.
User access tokens enable you to build client applications that directly authenticate to Azure Communication Services. For the generation of user access tokens, refer to User Access Tokens.
using Azure.Communication.Identity;
using Azure.Communication.Chat;
This will allow you to create, get, or delete chat threads.
ChatClient chatClient = new ChatClient(
endpoint,
new CommunicationTokenCredential(userToken));
The ChatThreadClient will allow you to perform operations specific to a chat thread, like update the chat thread topic, send a message, add participants to the chat thread, etc.
You can instantiate a new ChatThreadClient using the GetChatThread operation of the ChatClient with an existing thread id:
ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(chatThread.Id);
A chat conversation is represented by a thread. Each user in the thread is called a thread participant. Thread participants can chat with one another privately in a 1:1 chat or huddle up in a 1:N group chat. Users also get near-real time updates for when others are typing and when they have read the messages.
Once you initialized a ChatClient class, you can do the following chat operations:
CreateChatThreadOptions createChatThreadOptions = new CreateChatThreadOptions("Hello world!");
createChatThreadOptions.Metadata.Add("MetadataKey1", "MetadataValue1");
createChatThreadOptions.Metadata.Add("MetadataKey2", "MetadataValue2");
createChatThreadOptions.RetentionPolicy = ChatRetentionPolicy.ThreadCreationDate(40);
CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(createChatThreadOptions);
ChatThreadProperties chatThread = createChatThreadResult.ChatThread;
ChatThread chatThread = chatClient.GetChatThread(chatThread.Id);
Pageable<ChatThreadItem> threads = chatClient.GetChatThreads();
chatClient.DeleteChatThread(chatThread.Id);
Once you initialized a ChatThreadClient class, you can do the following chat operations:
UpdateChatThreadPropertiesOptions updateChatThreadPropertiesOptions = new UpdateChatThreadPropertiesOptions();
updateChatThreadPropertiesOptions.Topic = "Launch meeting";
updateChatThreadPropertiesOptions.Metadata.Add("UpdateMetadataKey", "UpdateMetadataValue");
updateChatThreadPropertiesOptions.RetentionPolicy = ChatRetentionPolicy.None();
await chatThreadClient.UpdatePropertiesAsync(updateChatThreadPropertiesOptions);
SendChatMessageResult sendChatMessageResult = chatThreadClient.SendMessage("Let's meet at 11am");
chatThreadClient.UpdateMessage(sendChatMessageResult.Id, content: "Instead of 11am, let's meet at 2pm");
ChatMessage message = chatThreadClient.GetMessage(sendChatMessageResult.Id);
chatThreadClient.DeleteMessage(sendChatMessageResult.Id);
Pageable<ChatMessage> messages = chatThreadClient.GetMessages();
Pageable<ChatParticipant> chatParticipants = chatThreadClient.GetParticipants();
chatThreadClient.AddParticipants(participants: new[] { new ChatParticipant(participantIdentifier) });
chatThreadClient.RemoveParticipant(identifier: participantIdentifier);
chatThreadClient.SendTypingNotification();
Pageable<ChatMessageReadReceipt> readReceipts = chatThreadClient.GetReadReceipts();
chatThreadClient.SendReadReceipt(sendChatMessageResult.Id);
We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.
Client options | Accessing the response | Long-running operations | Handling failures | Diagnostics | Mocking | Client lifetime
The following sections provide several code snippets covering some of the most common tasks, including:
Use CreateChatThread to create a chat thread client object.
topic to give a thread topic.communicationUser, required, it is the identification for the thread participant.displayName, optional, is the display name for the thread participantshareHistoryTime, optional, time from which the chat history is shared with the participant.ChatThreadClient is the result returned from creating a thread, you can use it to perform other operations on the chat thread.
ChatClient chatClient = new ChatClient(
endpoint,
new CommunicationTokenCredential(userToken));
var chatParticipant = new ChatParticipant(identifier: kimberly)
{
DisplayName = "Kim"
};
chatParticipant.Metadata.Add("MetadataKey1", "MetadataValue1");
chatParticipant.Metadata.Add("MetadataKey2", "MetadataValue2");
CreateChatThreadOptions createChatThreadOptions = new CreateChatThreadOptions("Hello world!");
createChatThreadOptions.Participants.Add(chatParticipant);
createChatThreadOptions.Metadata.Add("MetadataKey1", "MetadataValue1");
createChatThreadOptions.Metadata.Add("MetadataKey2", "MetadataValue2");
createChatThreadOptions.RetentionPolicy = ChatRetentionPolicy.ThreadCreationDate(60);
CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(createChatThreadOptions);
string threadId = createChatThreadResult.ChatThread.Id;
ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId);
Use GetChatThread to retrieve a chat thread from the service.
threadId is the unique id of the thread.
ChatThreadProperties chatThread = await chatThreadClient.GetPropertiesAsync();
Use GetChatThreads to get the list of chat threads for the participant that instantiated the chatClient.
AsyncPageable<ChatThreadItem> chatThreadItems = chatClient.GetChatThreadsAsync();
await foreach (ChatThreadItem chatThreadItem in chatThreadItems)
{
Console.WriteLine($"{ chatThreadItem.Id}");
}
Use DeleteChatThread to delete a thread.
threadId is the unique id of the thread.
await chatClient.DeleteChatThreadAsync(threadId);
Use UpdatePropertiesAsync to update the chat thread topic or metadata.
UpdateChatThreadPropertiesOptions updateChatThreadPropertiesOptions = new UpdateChatThreadPropertiesOptions();
updateChatThreadPropertiesOptions.Topic = "new topic !";
updateChatThreadPropertiesOptions.Metadata.Add("UpdateMetadataKey", "UpdateMetadataValue");
updateChatThreadPropertiesOptions.RetentionPolicy = ChatRetentionPolicy.ThreadCreationDate(60);
await chatThreadClient.UpdatePropertiesAsync(updateChatThreadPropertiesOptions);
Use SendMessage to send a message to a thread.
content to provide the content for the message, it is required.type for the content type of the message such as 'Text' or 'Html'. If not specified, 'Text' will be set.senderDisplayName to specify the display name of the sender. If not specified, empty string will be set.SendChatMessageResult sendChatMessageResult = await chatThreadClient.SendMessageAsync(content:"hello world");
var messageId = sendChatMessageResult.Id;
Use GetMessage to retrieve a message from the service.
messageId is the unique id of the message.
ChatMessage is the response returned from getting a message, it contains an id, which is the unique identifier of the message, among other fields. Please refer to Azure.Communication.Chat.ChatMessage
ChatMessage chatMessage = await chatThreadClient.GetMessageAsync(messageId);
Use GetMessages to retrieve all messages for the chat thread.
AsyncPageable<ChatMessage> allMessages = chatThreadClient.GetMessagesAsync();
await foreach (ChatMessage message in allMessages)
{
Console.WriteLine($"{message.Id}:{message.Content.Message}");
}
Use UpdateMessage to update a message.
messageId is the unique identifier of the message.content is the message content to be updated.await chatThreadClient.UpdateMessageAsync(messageId, "updated message content");
Use DeleteMessage to delete a message.
messageId is the unique identifier of the message.await chatThreadClient.DeleteMessageAsync(messageId);
Use GetParticipants to retrieve the participants of the chat thread.
AsyncPageable<ChatParticipant> allParticipants = chatThreadClient.GetParticipantsAsync();
await foreach (ChatParticipant participant in allParticipants)
{
Console.WriteLine($"{((CommunicationUserIdentifier)participant.User).Id}:{participant.DisplayName}:{participant.ShareHistoryTime}");
}
Use AddParticipants to add one or more participants to the chat thread. The following are the supported attributes for each thread participant(s):
communicationUser, required, it is the identification for the thread participant.displayName, optional, is the display name for the thread participant.shareHistoryTime, optional, time from which the chat history is shared with the participant.var participants = new[]
{
new ChatParticipant(josh) { DisplayName = "Josh" },
new ChatParticipant(gloria) { DisplayName = "Gloria" },
new ChatParticipant(amy) { DisplayName = "Amy" }
};
await chatThreadClient.AddParticipantsAsync(participants);
Use RemoveParticipant to remove a thread participant from the thread.
communicationUser is the identification of the chat participant.
await chatThreadClient.RemoveParticipantAsync(gloria);
Use SendTypingNotification to indicate that the user is typing a response in the thread.
await chatThreadClient.SendTypingNotificationAsync();
Use SendReadReceipt to notify other participants that the message is read by the user.
await chatThreadClient.SendReadReceiptAsync(messageId);
Use GetReadReceipts to check the status of messages to see which ones are read by other participants of a chat thread.
AsyncPageable<ChatMessageReadReceipt> allReadReceipts = chatThreadClient.GetReadReceiptsAsync();
await foreach (ChatMessageReadReceipt readReceipt in allReadReceipts)
{
Console.WriteLine($"{readReceipt.ChatMessageId}:{((CommunicationUserIdentifier)readReceipt.Sender).Id}:{readReceipt.ReadOn}");
}
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.
try
{
CreateChatThreadResult createChatThreadErrorResult = await chatClient.CreateChatThreadAsync(topic: "Hello world!", participants: new[] { josh });
}
catch (RequestFailedException ex)
{
Console.WriteLine(ex.Message);
}
Read more about Chat in Azure Communication Services
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.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact with any additional questions or comments.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 1 NuGet packages that depend on Azure.Communication.Chat:
| Package | Downloads |
|---|---|
|
ToolsLibrary1
Package Description |
Showing the top 2 popular GitHub repositories that depend on Azure.Communication.Chat:
| Repository | Stars |
|---|---|
|
Azure-Samples/communication-services-AI-customer-service-sample
A sample app for the customer support center running in Azure, using Azure Communication Services and Azure OpenAI for text and voice bots.
|
|
|
Azure-Samples/communication-services-dotnet-quickstarts
Sample code for Azure Communication Services .Net quickstarts
|
| Version | Downloads | Last Updated |
|---|---|---|
| 1.4.0 | 358,007 | 6/24/2025 |
| 1.4.0-beta.1 | 912 | 5/13/2025 |
| 1.3.1 | 315,968 | 9/19/2024 |
| 1.3.0 | 298,377 | 4/15/2024 |
| 1.3.0-beta.1 | 1,494 | 4/17/2024 |
| 1.2.0 | 100,976 | 12/4/2023 |
| 1.2.0-beta.1 | 8,463 | 8/15/2023 |
| 1.1.2 | 79,580 | 6/19/2023 |
| 1.1.1 | 289,484 | 9/16/2022 |
| 1.1.0 | 149,128 | 9/16/2021 |
| 1.1.0-beta.1 | 942 | 7/26/2021 |
| 1.0.1 | 40,830 | 5/11/2021 |
| 1.0.0 | 30,800 | 3/29/2021 |
| 1.0.0-beta.5 | 546 | 3/10/2021 |
| 1.0.0-beta.4 | 917 | 2/10/2021 |
| 1.0.0-beta.3 | 4,759 | 11/16/2020 |
| 1.0.0-beta.2 | 15,198 | 10/6/2020 |
| 1.0.0-beta.1 | 2,135 | 9/22/2020 |