Exemplo n.º 1
0
        public async Task OrderInGetMessagesIteratorIsNotAlteredByPaging()
        {
            //arrange
            var threadId = "19:[email protected]";
            var uri      = new Uri("https://localHostTest");

            var responseItemsPage1 = new MockResponse(200);

            responseItemsPage1.SetContent(_page1MessagesApiResponsePayload);

            var responseItemsPage2 = new MockResponse(200);

            responseItemsPage2.SetContent(_page2MessagesApiResponsePayload);

            var chatClientOptions = new ChatClientOptions
            {
                Transport = new MockTransport(responseItemsPage1, responseItemsPage2)
            };

            //act
            var communicationTokenCredential        = new CommunicationTokenCredential(ChatRecordedTestSanitizer.SanitizedUnsignedUserTokenValue);
            var chatThreadClient                    = new ChatThreadClient(threadId, uri, communicationTokenCredential, chatClientOptions);
            AsyncPageable <ChatMessage> allMessages = chatThreadClient.GetMessagesAsync();

            //assert
            int pages               = 0;
            int idCounter           = 0;
            int textMessagesCounter = 0;

            await foreach (Page <ChatMessage> page in allMessages.AsPages(pageSizeHint: 4))
            {
                pages++;
                foreach (ChatMessage message in page.Values)
                {
                    idCounter++;
                    Assert.AreEqual($"{idCounter}", message.Id);
                    Assert.AreEqual($"{idCounter}", message.Version);
                    if (message.Type == "Text")
                    {
                        textMessagesCounter++;
                        Assert.AreEqual($"Content for async message {idCounter}", message.Content);
                    }
                }
            }
            Assert.AreEqual(2, pages);
            Assert.AreEqual(8, idCounter);
            Assert.AreEqual(5, textMessagesCounter);
        }
        /// <summary>
        /// ViewMessagesInAThread() - view messages in a chat thread
        /// </summary>
        /// <returns></returns>
        private static async Task ViewMessagesInAThread()
        {
            Console.WriteLine("\n# Viewing messages in a thread");

            Console.Write("Enter thread id: ");
            var threadId = Console.ReadLine();

            Uri endpoint = new Uri("https://ACS-INSTANCE.communication.azure.com/");

            CommunicationUserCredential communicationUserCredential = new CommunicationUserCredential(token);
            ChatClient chatClient = new ChatClient(endpoint, communicationUserCredential);

            ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId);

            AsyncPageable <ChatMessage> allMessages = chatThreadClient.GetMessagesAsync();

            await foreach (ChatMessage message in allMessages)
            {
                Console.WriteLine($"{message.Id}: {message.Sender.Id}: {message.Content}");
            }
        }
Exemplo n.º 3
0
        public async Task OrderInGetMessagesIteratorIsNotAltered()
        {
            //arrange
            var threadId         = "19:[email protected]";
            var uri              = new Uri("https://localHostTest");
            var responseAllItems = new MockResponse(200);

            responseAllItems.SetContent(_allMessagesApiResponsePayload);

            var chatClientOptions = new ChatClientOptions
            {
                Transport = new MockTransport(responseAllItems)
            };

            //act
            var communicationUserCredential         = new CommunicationUserCredential(ChatRecordedTestSanitizer.SanitizedChatAuthHeaderValue);
            var chatThreadClient                    = new ChatThreadClient(threadId, uri, communicationUserCredential, chatClientOptions);
            AsyncPageable <ChatMessage> allMessages = chatThreadClient.GetMessagesAsync();

            //assert
            int idCounter           = 0;
            int textMessagesCounter = 0;

            await foreach (ChatMessage message in allMessages)
            {
                idCounter++;
                Assert.AreEqual($"{idCounter}", message.Id);
                Assert.AreEqual($"{idCounter}", message.Version);
                if (message.Type == "Text")
                {
                    textMessagesCounter++;
                    Assert.AreEqual($"Content for async message {idCounter}", message.Content);
                }
            }
            Assert.AreEqual(8, idCounter);
            Assert.AreEqual(5, textMessagesCounter);
        }
Exemplo n.º 4
0
        public async Task SendGetUpdateDeleteMessagesSendNotificationReadReceiptsAsync()
        {
            CommunicationIdentityClient            communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString);
            Response <CommunicationUserIdentifier> threadMember = await communicationIdentityClient.CreateUserAsync();

            CommunicationUserToken communicationUserToken = await communicationIdentityClient.IssueTokenAsync(threadMember.Value, new[] { CommunicationTokenScope.Chat });

            string userToken            = communicationUserToken.Token;
            string endpoint             = TestEnvironment.ChatApiUrl();
            string theadCreatorMemberId = communicationUserToken.User.Id;

            ChatClient chatClient = new ChatClient(
                new Uri(endpoint),
                new CommunicationTokenCredential(userToken));

            var chatThreadMember = new ChatThreadMember(new CommunicationUserIdentifier(theadCreatorMemberId))
            {
                DisplayName      = "UserDisplayName",
                ShareHistoryTime = DateTime.MinValue
            };
            ChatThreadClient chatThreadClient = await chatClient.CreateChatThreadAsync(topic : "Hello world!", members : new[] { chatThreadMember });

            string threadId = chatThreadClient.Id;

            #region Snippet:Azure_Communication_Chat_Tests_Samples_SendMessage
            var content           = "hello world";
            var priority          = ChatMessagePriority.Normal;
            var senderDisplayName = "sender name";
            SendChatMessageResult sendMessageResult = await chatThreadClient.SendMessageAsync(content, priority, senderDisplayName);

            #endregion Snippet:Azure_Communication_Chat_Tests_SendMessage

            var messageId = sendMessageResult.Id;
            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetMessage
            ChatMessage chatMessage = await chatThreadClient.GetMessageAsync(messageId);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetMessage

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetMessages
            AsyncPageable <ChatMessage> allMessages = chatThreadClient.GetMessagesAsync();
            await foreach (ChatMessage message in allMessages)
            {
                Console.WriteLine($"{message.Id}:{message.Sender.Id}:{message.Content}");
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetMessages

            #region Snippet:Azure_Communication_Chat_Tests_Samples_UpdateMessage
            await chatThreadClient.UpdateMessageAsync(messageId, "updated message content");

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_UpdateMessage

            //Note : Due to the async nature of the storage, moving coverage to CI tests
            #region Snippet:Azure_Communication_Chat_Tests_Samples_SendReadReceipt
            //@@await chatThreadClient.SendReadReceiptAsync(messageId);
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_SendReadReceipt

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetReadReceipts
            AsyncPageable <ReadReceipt> allReadReceipts = chatThreadClient.GetReadReceiptsAsync();
            await foreach (ReadReceipt readReceipt in allReadReceipts)
            {
                Console.WriteLine($"{readReceipt.ChatMessageId}:{readReceipt.Sender.Id}:{readReceipt.ReadOn}");
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetReadReceipts

            #region Snippet:Azure_Communication_Chat_Tests_Samples_DeleteMessage
            await chatThreadClient.DeleteMessageAsync(messageId);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_DeleteMessage

            #region Snippet:Azure_Communication_Chat_Tests_Samples_SendTypingNotification
            await chatThreadClient.SendTypingNotificationAsync();

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_SendTypingNotification

            await chatClient.DeleteChatThreadAsync(threadId);
        }
Exemplo n.º 5
0
        public async Task E2E_ThreadCreateUpdateGetDelete_MemberAddUpdateRemove_MessageGetSendUpdate_NotificationTyping_ReadReceiptGetSend_Async()
        {
            //arr
            CommunicationUser           user1, user2, user3;
            string                      token1, token2, token3;
            CommunicationIdentityClient communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient();

            (user1, token1) = await CreateUserAndTokenAsync(communicationIdentityClient);

            (user2, token2) = await CreateUserAndTokenAsync(communicationIdentityClient);

            (user3, token3) = await CreateUserAndTokenAsync(communicationIdentityClient);

            var topic                 = "Thread Async from C# sdk";
            var contentMessage        = "This is message 1";
            var updatedMessageContent = "This is message 1 updated";
            var displayNameMessage    = "DisplayName sender message 1";
            var updatedTopic          = "Updated topic - C# sdk";
            var members               = new List <ChatThreadMember>
            {
                new ChatThreadMember(user1),
                new ChatThreadMember(user2)
            };
            ChatClient chatClient  = CreateInstrumentedChatClient(token1);
            ChatClient chatClient2 = CreateInstrumentedChatClient(token2);

            //act
            ChatThreadClient chatThreadClient = await CreateInstrumentedChatThreadClientAsync(chatClient, topic, members);

            ChatThreadClient chatThreadClient2 = await CreateInstrumentedChatThreadClientAsync(chatClient, topic, members);

            await chatThreadClient.UpdateThreadAsync(updatedTopic);

            ChatThread chatThread = await chatClient.GetChatThreadAsync(chatThreadClient.Id);

            AsyncPageable <ChatThreadInfo> threads = chatClient.GetChatThreadsInfoAsync();
            var threadsCount = threads.ToEnumerableAsync().Result.Count;

            SendChatMessageResult sendChatMessageResult = await chatThreadClient.SendMessageAsync(contentMessage, ChatMessagePriority.High, displayNameMessage);

            SendChatMessageResult sendChatMessageResult2 = await chatThreadClient.SendMessageAsync(contentMessage, ChatMessagePriority.High, displayNameMessage);

            ChatMessage message = await chatThreadClient.GetMessageAsync(sendChatMessageResult.Id);

            AsyncPageable <ChatMessage> messages = chatThreadClient.GetMessagesAsync();
            var getMessagesCount = messages.ToEnumerableAsync().Result.Count;

            var messageId = sendChatMessageResult.Id;
            await chatThreadClient.UpdateMessageAsync(messageId, updatedMessageContent);

            Response <ChatMessage> actualUpdateMessage = await chatThreadClient.GetMessageAsync(messageId);

            await chatThreadClient.DeleteMessageAsync(messageId);

            AsyncPageable <ChatMessage> messagesAfterOneDeleted = chatThreadClient.GetMessagesAsync();
            ChatMessage deletedChatMessage = messagesAfterOneDeleted.ToEnumerableAsync().Result.First(x => x.Id == messageId);

            AsyncPageable <ChatThreadMember> chatThreadMembers = chatThreadClient.GetMembersAsync();
            var chatThreadMembersCount = chatThreadMembers.ToEnumerableAsync().Result.Count;

            var newMember = new ChatThreadMember(user3);
            await chatThreadClient.AddMembersAsync(new List <ChatThreadMember> {
                newMember
            });

            AsyncPageable <ChatThreadMember> chatThreadMembersAfterOneAdded = chatThreadClient.GetMembersAsync();
            var chatThreadMembersAfterOneAddedCount = chatThreadMembersAfterOneAdded.ToEnumerableAsync().Result.Count();

            CommunicationUser userToBeRemoved = user3; //Better name for the snippet
            await chatThreadClient.RemoveMemberAsync(userToBeRemoved);

            AsyncPageable <ChatThreadMember> chatThreadMembersAfterOneDeleted = chatThreadClient.GetMembersAsync();
            var chatThreadMembersAfterOneDeletedCount = chatThreadMembersAfterOneDeleted.ToEnumerableAsync().Result.Count();

            Response typingNotificationResponse = await chatThreadClient.SendTypingNotificationAsync();

            await chatThreadClient.SendTypingNotificationAsync();

            await chatThreadClient.SendReadReceiptAsync(messageId);

            await chatThreadClient.SendReadReceiptAsync(sendChatMessageResult2.Id);

            AsyncPageable <ReadReceipt> readReceipts = chatThreadClient.GetReadReceiptsAsync();
            var readReceiptsCount = readReceipts.ToEnumerableAsync().Result.Count();

            await chatClient.DeleteChatThreadAsync(chatThreadClient.Id);

            //assert
            Assert.AreEqual(updatedTopic, chatThread.Topic);
            Assert.AreEqual(2, chatThread.Members.Count);
            Assert.AreEqual(contentMessage, message.Content);
            Assert.AreEqual(displayNameMessage, message.SenderDisplayName);
            Assert.AreEqual(ChatMessagePriority.High, message.Priority);
            Assert.AreEqual(2, threadsCount);
            Assert.AreEqual(5, getMessagesCount); //Including all types
            Assert.AreEqual(updatedMessageContent, actualUpdateMessage.Value.Content);
            Assert.IsTrue(deletedChatMessage.DeletedOn.HasValue);
            Assert.AreEqual(2, chatThreadMembersCount);
            Assert.AreEqual(3, chatThreadMembersAfterOneAddedCount);
            Assert.AreEqual(2, chatThreadMembersAfterOneDeletedCount);
            Assert.AreEqual((int)HttpStatusCode.OK, typingNotificationResponse.Status);
            //TODO: Commenting out the assert below for now as it is flakey due to server-side delay; currently in investigation
            // Assert.AreEqual(1, readReceiptsCount);
        }
        public async Task ThreadCGUD_ParticipantAUR_MessageGSU_NotificationT_Async()
        {
            //arr
            Console.WriteLine($"ThreadCGUD_ParticipantAUR_MessageGSU_NotificationT_Async Running on RecordedTestMode : {Mode}");
            CommunicationIdentityClient communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient();

            (CommunicationUserIdentifier user1, string token1) = await CreateUserAndTokenAsync(communicationIdentityClient);

            (CommunicationUserIdentifier user2, _) = await CreateUserAndTokenAsync(communicationIdentityClient);

            (CommunicationUserIdentifier user3, string token3) = await CreateUserAndTokenAsync(communicationIdentityClient);

            (CommunicationUserIdentifier user4, _) = await CreateUserAndTokenAsync(communicationIdentityClient);

            (CommunicationUserIdentifier user5, _) = await CreateUserAndTokenAsync(communicationIdentityClient);

            string repeatabilityRequestId1 = "contoso-C0A747F1-6245-4307-8267-B974340677DC";
            string repeatabilityRequestId2 = "contoso-D0A747F1-6245-4307-8267-B974340677DD";

            var topic = "Thread async from C# sdk";
            var displayNameMessage = "DisplayName sender message 1";
            var participants       = new List <ChatParticipant>
            {
                new ChatParticipant(user1)
                {
                    DisplayName = "user1"
                },
                new ChatParticipant(user2)
                {
                    DisplayName = "user2"
                },
                new ChatParticipant(user3)
                {
                    DisplayName = "user3"
                }
            };
            ChatClient chatClient  = CreateInstrumentedChatClient(token1);
            ChatClient chatClient3 = CreateInstrumentedChatClient(token3);

            //act
            CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(topic, participants, repeatabilityRequestId1);

            ChatThreadClient chatThreadClient = GetInstrumentedChatThreadClient(chatClient, createChatThreadResult.ChatThread.Id);
            var threadId = chatThreadClient.Id;
            CreateChatThreadResult createChatThreadResult2 = await chatClient.CreateChatThreadAsync(topic, participants, repeatabilityRequestId2);

            ChatThreadClient chatThreadClient2 = GetInstrumentedChatThreadClient(chatClient, createChatThreadResult2.ChatThread.Id);
            ChatThreadClient chatThreadClient3 = GetInstrumentedChatThreadClient(chatClient3, threadId);

            AsyncPageable <ChatParticipant> chatParticipantsOnCreation = chatThreadClient.GetParticipantsAsync();
            var chatParticipantsOnCreationCount = chatParticipantsOnCreation.ToEnumerableAsync().Result.Count;

            var updatedTopic = "Updated topic - C# sdk";
            await chatThreadClient.UpdateTopicAsync(updatedTopic);

            ChatThread chatThread = await chatClient.GetChatThreadAsync(threadId);

            string messageContent = "Content for message 1";
            string messageId      = await chatThreadClient.SendMessageAsync(messageContent, ChatMessageType.Text, displayNameMessage);

            string messageContent2 = "Content for message 2";
            string messageId2      = await chatThreadClient2.SendMessageAsync(messageContent2, ChatMessageType.Html, displayNameMessage);

            string messageContent3 = "Content for message 3";
            string messageId3      = await chatThreadClient3.SendMessageAsync(messageContent3, ChatMessageType.Text, displayNameMessage);

            string messageContent4 = "Content for message 4";
            string messageId4      = await chatThreadClient3.SendMessageAsync(messageContent4, ChatMessageType.Html, displayNameMessage);

            string messageContent5 = "Content for message 5";
            string messageId5      = await chatThreadClient3.SendMessageAsync(messageContent5, ChatMessageType.Text, displayNameMessage);

            string messageContent6 = "Content for message 6";
            string messageId6      = await chatThreadClient3.SendMessageAsync(messageContent6, ChatMessageType.Html, displayNameMessage);

            ChatMessage message = await chatThreadClient.GetMessageAsync(messageId);

            ChatMessage message2 = await chatThreadClient2.GetMessageAsync(messageId2);

            ChatMessage message3 = await chatThreadClient3.GetMessageAsync(messageId3);

            ChatMessage message4 = await chatThreadClient3.GetMessageAsync(messageId4);

            ChatMessage message5 = await chatThreadClient3.GetMessageAsync(messageId5);

            ChatMessage message6 = await chatThreadClient3.GetMessageAsync(messageId6);

            AsyncPageable <ChatMessage> messages  = chatThreadClient.GetMessagesAsync();
            AsyncPageable <ChatMessage> messages2 = chatThreadClient2.GetMessagesAsync();
            var getMessagesCount  = messages.ToEnumerableAsync().Result.Count;
            var getMessagesCount2 = messages2.ToEnumerableAsync().Result.Count;

            #region Messages Pagination assertions
            AsyncPageable <ChatMessage> messagesPaginationTest = chatThreadClient.GetMessagesAsync();
            await PageableTester <ChatMessage> .AssertPaginationAsync(enumerableResource : messagesPaginationTest, expectedPageSize : 2, expectedTotalResources : 8);

            #endregion

            var updatedMessageContent = "This is message 1 content updated";
            await chatThreadClient.UpdateMessageAsync(messageId, updatedMessageContent);

            Response <ChatMessage> actualUpdateMessage = await chatThreadClient.GetMessageAsync(messageId);

            await chatThreadClient.DeleteMessageAsync(messageId);

            List <ChatMessage> messagesAfterOneDeleted = chatThreadClient.GetMessagesAsync().ToEnumerableAsync().Result;
            ChatMessage        deletedChatMessage      = messagesAfterOneDeleted.First(x => x.Id == messageId);

            #region Participants Pagination assertions
            AsyncPageable <ChatParticipant> chatParticipants = chatThreadClient.GetParticipantsAsync();
            var chatParticipantsCount = chatParticipants.ToEnumerableAsync().Result.Count;
            #endregion

            var newParticipant = new ChatParticipant(user4)
            {
                DisplayName = "user4"
            };
            var newParticipant2 = new ChatParticipant(user5)
            {
                DisplayName = "user5"
            };
            AddChatParticipantsResult addChatParticipantsResult = await chatThreadClient.AddParticipantsAsync(participants : new[] { newParticipant });

            AddChatParticipantsResult addChatParticipantsResult2 = await chatThreadClient.AddParticipantAsync(newParticipant2);

            AsyncPageable <ChatParticipant> chatParticipantsAfterTwoOneAdded = chatThreadClient.GetParticipantsAsync();
            await PageableTester <ChatParticipant> .AssertPaginationAsync(enumerableResource : chatParticipantsAfterTwoOneAdded, expectedPageSize : 2, expectedTotalResources : 5);

            var chatParticipantsAfterTwoOneAddedCount = chatParticipantsAfterTwoOneAdded.ToEnumerableAsync().Result.Count;

            List <ChatMessage> messagesAfterParticipantsAdded = chatThreadClient.GetMessagesAsync().ToEnumerableAsync().Result;
            ChatMessage        participantAddedMessage        = messagesAfterParticipantsAdded.First(x => x.Type == ChatMessageType.ParticipantAdded);

            CommunicationUserIdentifier participantToBeRemoved = user4;
            await chatThreadClient.RemoveParticipantAsync(identifier : participantToBeRemoved);

            AsyncPageable <ChatParticipant> chatParticipantAfterOneDeleted = chatThreadClient.GetParticipantsAsync();
            var chatParticipantAfterOneDeletedCount = chatParticipantAfterOneDeleted.ToEnumerableAsync().Result.Count;

            List <ChatMessage> messagesAfterParticipantRemoved = chatThreadClient.GetMessagesAsync().ToEnumerableAsync().Result;
            ChatMessage        participantRemovedMessage       = messagesAfterParticipantRemoved.First(x => x.Type == ChatMessageType.ParticipantRemoved);

            ChatMessage participantRemovedMessage2 = await chatThreadClient.GetMessageAsync(participantRemovedMessage.Id);

            Response typingNotificationResponse = await chatThreadClient.SendTypingNotificationAsync();

            await chatThreadClient.SendTypingNotificationAsync();

            AsyncPageable <ChatThreadInfo> threads = chatClient.GetChatThreadsInfoAsync();
            var threadsCount = threads.ToEnumerableAsync().Result.Count;

            await chatClient.DeleteChatThreadAsync(threadId);

            //assert
            Assert.AreEqual(updatedTopic, chatThread.Topic);
            Assert.AreEqual(3, chatParticipantsOnCreationCount);
            Assert.AreEqual(messageContent, message.Content.Message);
            Assert.AreEqual(displayNameMessage, message.SenderDisplayName);
            Assert.AreEqual(updatedMessageContent, actualUpdateMessage.Value.Content.Message);
            Assert.AreEqual(ChatMessageType.Text, message.Type);

            Assert.AreEqual(messageContent2, message2.Content.Message);
            Assert.AreEqual(messageContent3, message3.Content.Message);
            Assert.AreEqual(messageContent4, message4.Content.Message);
            Assert.AreEqual(messageContent5, message5.Content.Message);
            Assert.AreEqual(messageContent6, message6.Content.Message);

            Assert.AreEqual(2, threadsCount);
            Assert.AreEqual(8, getMessagesCount);  //Including all types : 5 text message, 3 control messages
            Assert.AreEqual(3, getMessagesCount2); //Including all types : 1 text message, 2 control messages

            Assert.AreEqual(1, participantAddedMessage.Content.Participants.Count);
            Assert.AreEqual(user5.Id, CommunicationIdentifierSerializer.Serialize(participantAddedMessage.Content.Participants[0].User).CommunicationUser.Id);
            Assert.AreEqual("user5", participantAddedMessage.Content.Participants[0].DisplayName);

            Assert.AreEqual(1, participantRemovedMessage.Content.Participants.Count);
            Assert.AreEqual(user4.Id, CommunicationIdentifierSerializer.Serialize(participantRemovedMessage.Content.Participants[0].User).CommunicationUser.Id);
            Assert.AreEqual("user4", participantRemovedMessage.Content.Participants[0].DisplayName);

            Assert.IsTrue(deletedChatMessage.DeletedOn.HasValue);
            Assert.AreEqual(3, chatParticipantsCount);
            Assert.AreEqual(5, chatParticipantsAfterTwoOneAddedCount);
            Assert.AreEqual(4, chatParticipantAfterOneDeletedCount);
            Assert.IsNull(addChatParticipantsResult.Errors);
            Assert.IsNull(addChatParticipantsResult2.Errors);
            Assert.AreEqual((int)HttpStatusCode.OK, typingNotificationResponse.Status);
        }
        public async Task SendGetUpdateDeleteMessagesSendNotificationReadReceiptsAsync()
        {
            CommunicationIdentityClient            communicationIdentityClient = new CommunicationIdentityClient(TestEnvironment.ConnectionString);
            Response <CommunicationUserIdentifier> threadMember = await communicationIdentityClient.CreateUserAsync();

            AccessToken communicationUserToken = await communicationIdentityClient.GetTokenAsync(threadMember.Value, new[] { CommunicationTokenScope.Chat });

            string userToken            = communicationUserToken.Token;
            string theadCreatorMemberId = threadMember.Value.Id;

            ChatClient chatClient = new ChatClient(
                TestEnvironment.Endpoint,
                new CommunicationTokenCredential(userToken));

            var chatParticipant = new ChatParticipant(new CommunicationUserIdentifier(theadCreatorMemberId))
            {
                DisplayName      = "UserDisplayName",
                ShareHistoryTime = DateTimeOffset.MinValue
            };
            CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { chatParticipant });

            ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(createChatThreadResult.ChatThread.Id);

            #region Snippet:Azure_Communication_Chat_Tests_Samples_SendMessage
            SendChatMessageResult sendChatMessageResult = await chatThreadClient.SendMessageAsync(content : "hello world");

            var messageId = sendChatMessageResult.Id;
            #endregion Snippet:Azure_Communication_Chat_Tests_SendMessage

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetMessage
            ChatMessage chatMessage = await chatThreadClient.GetMessageAsync(messageId);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetMessage

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetMessages
            AsyncPageable <ChatMessage> allMessages = chatThreadClient.GetMessagesAsync();
            await foreach (ChatMessage message in allMessages)
            {
                Console.WriteLine($"{message.Id}:{message.Content.Message}");
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetMessages

            #region Snippet:Azure_Communication_Chat_Tests_Samples_UpdateMessage
            await chatThreadClient.UpdateMessageAsync(messageId, "updated message content");

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_UpdateMessage

            //Note : Due to the async nature of the storage, moving coverage to CI tests
            #region Snippet:Azure_Communication_Chat_Tests_Samples_SendReadReceipt
            //@@await chatThreadClient.SendReadReceiptAsync(messageId);
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_SendReadReceipt

            #region Snippet:Azure_Communication_Chat_Tests_Samples_GetReadReceipts
            AsyncPageable <ChatMessageReadReceipt> allReadReceipts = chatThreadClient.GetReadReceiptsAsync();
            await foreach (ChatMessageReadReceipt readReceipt in allReadReceipts)
            {
                Console.WriteLine($"{readReceipt.ChatMessageId}:{((CommunicationUserIdentifier)readReceipt.Sender).Id}:{readReceipt.ReadOn}");
            }
            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_GetReadReceipts

            #region Snippet:Azure_Communication_Chat_Tests_Samples_DeleteMessage
            await chatThreadClient.DeleteMessageAsync(messageId);

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_DeleteMessage

            #region Snippet:Azure_Communication_Chat_Tests_Samples_SendTypingNotification
            await chatThreadClient.SendTypingNotificationAsync();

            #endregion Snippet:Azure_Communication_Chat_Tests_Samples_SendTypingNotification

            await chatClient.DeleteChatThreadAsync(chatThreadClient.Id);
        }
Exemplo n.º 8
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            Uri endpoint = new Uri("https://<RESOURCE_NAME>.communication.azure.com");

            CommunicationTokenCredential communicationTokenCredential = new CommunicationTokenCredential("<Access_Token>");
            ChatClient chatClient = new ChatClient(endpoint, communicationTokenCredential);

            // <Start a chat thread>
            var chatParticipant = new ChatParticipant(identifier: new CommunicationUserIdentifier(id: "<Access_ID>"))
            {
                DisplayName = "UserDisplayName"
            };
            CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(topic : "Hello world!", participants : new[] { chatParticipant });

            // <Get a chat thread client>
            ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId: createChatThreadResult.ChatThread.Id);
            string           threadId         = chatThreadClient.Id;

            // <List all chat threads>
            AsyncPageable <ChatThreadItem> chatThreadItems = chatClient.GetChatThreadsAsync();

            await foreach (ChatThreadItem chatThreadItem in chatThreadItems)
            {
                Console.WriteLine($"{ chatThreadItem.Id}");
            }

            // <Send a message to a chat thread>
            SendChatMessageResult sendChatMessageResult = await chatThreadClient.SendMessageAsync(content : "hello world", type : ChatMessageType.Text);

            string messageId = sendChatMessageResult.Id;

            // <Receive chat messages from a chat thread>
            AsyncPageable <ChatMessage> allMessages = chatThreadClient.GetMessagesAsync();

            await foreach (ChatMessage message in allMessages)
            {
                Console.WriteLine($"{message.Id}:{message.Content.Message}");
            }

            // <Add a user as a participant to the chat thread>
            var josh   = new CommunicationUserIdentifier(id: "<Access_ID_For_Josh>");
            var gloria = new CommunicationUserIdentifier(id: "<Access_ID_For_Gloria>");
            var amy    = new CommunicationUserIdentifier(id: "<Access_ID_For_Amy>");

            var participants = new[]
            {
                new ChatParticipant(josh)
                {
                    DisplayName = "Josh"
                },
                new ChatParticipant(gloria)
                {
                    DisplayName = "Gloria"
                },
                new ChatParticipant(amy)
                {
                    DisplayName = "Amy"
                }
            };
            await chatThreadClient.AddParticipantsAsync(participants : participants);

            // <Get thread participants>
            AsyncPageable <ChatParticipant> allParticipants = chatThreadClient.GetParticipantsAsync();

            await foreach (ChatParticipant participant in allParticipants)
            {
                Console.WriteLine($"{((CommunicationUserIdentifier)participant.User).Id}:{participant.DisplayName}:{participant.ShareHistoryTime}");
            }

            // <Send read receipt>
            await chatThreadClient.SendReadReceiptAsync(messageId : messageId);
        }
Exemplo n.º 9
0
 // チャットのメッセージを取得
 public AsyncPageable <ChatMessage> GetMessagesAsync() =>
 _chatThreadClient.GetMessagesAsync();