Search in sources :

Example 1 with SendChatMessageOptions

use of com.azure.android.communication.chat.models.SendChatMessageOptions in project azure-sdk-for-android by Azure.

the class MainActivity method sendChatMessage.

public void sendChatMessage(View view) {
    if (chatThreadAsyncClient == null) {
        createChatThreadAsyncClient();
    }
    if (chatThreadAsyncClient != null) {
        // The chat message content, required.
        final String content = "Test message 1";
        // The display name of the sender, if null (i.e. not specified), an empty name will be set.
        final String senderDisplayName = "First participant";
        SendChatMessageOptions chatMessageOptions = new SendChatMessageOptions().setType(ChatMessageType.TEXT).setContent(content).setSenderDisplayName(senderDisplayName).setMetadata(new HashMap<String, String>() {

            {
                put("tags", "tag1");
                put("deliveryMode", "deliveryMode value");
            }
        });
        // message.
        try {
            chatMessageId = chatThreadAsyncClient.sendMessage(chatMessageOptions).get().getId();
            unreadMessages.add(chatMessageId);
            logAndToast("Message sent with ID: " + chatMessageId);
        } catch (InterruptedException | ExecutionException e) {
            logAndToast("Send message failed: " + e.getMessage());
        }
    } else {
        logAndToast("ChatThreadAsyncClient creation failed");
    }
}
Also used : SendChatMessageOptions(com.azure.android.communication.chat.models.SendChatMessageOptions) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with SendChatMessageOptions

use of com.azure.android.communication.chat.models.SendChatMessageOptions in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method canDeleteExistingMessage.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canDeleteExistingMessage(HttpClient httpClient) throws ExecutionException, InterruptedException {
    setupTest(httpClient);
    SendChatMessageOptions messageRequest = super.sendMessageOptions();
    CompletableFuture<SendChatMessageResult> completableFuture1 = this.chatThreadClient.sendMessage(messageRequest);
    String messageId = completableFuture1.get().getId();
    assertNotNull(messageId);
    CompletableFuture<Void> completableFuture2 = this.chatThreadClient.deleteMessage(messageId);
    completableFuture2.get();
}
Also used : SendChatMessageResult(com.azure.android.communication.chat.models.SendChatMessageResult) SendChatMessageOptions(com.azure.android.communication.chat.models.SendChatMessageOptions) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with SendChatMessageOptions

use of com.azure.android.communication.chat.models.SendChatMessageOptions in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method canListMessagesWithOptions.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canListMessagesWithOptions(HttpClient httpClient) throws ExecutionException, InterruptedException {
    setupTest(httpClient);
    SendChatMessageOptions messageRequest = super.sendMessageOptions();
    ListChatMessagesOptions options = new ListChatMessagesOptions();
    options.setMaxPageSize(10);
    options.setStartTime(OffsetDateTime.parse("2020-09-08T01:02:14.387Z"));
    this.chatThreadClient.sendMessage(messageRequest).get();
    this.chatThreadClient.sendMessage(messageRequest).get();
    PagedAsyncStream<ChatMessage> messagePagedAsyncStream = this.chatThreadClient.listMessages(options, null);
    CountDownLatch latch = new CountDownLatch(1);
    List<ChatMessage> returnedMessages = new ArrayList<ChatMessage>();
    messagePagedAsyncStream.forEach(new AsyncStreamHandler<ChatMessage>() {

        @Override
        public void onNext(ChatMessage message) {
            if (message.getType().equals(ChatMessageType.TEXT)) {
                returnedMessages.add(message);
            }
        }

        @Override
        public void onError(Throwable throwable) {
            latch.countDown();
        }

        @Override
        public void onComplete() {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "canListMessagesWithOptions");
    assertTrue(returnedMessages.size() > 0);
}
Also used : ChatMessage(com.azure.android.communication.chat.models.ChatMessage) SendChatMessageOptions(com.azure.android.communication.chat.models.SendChatMessageOptions) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ListChatMessagesOptions(com.azure.android.communication.chat.models.ListChatMessagesOptions) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with SendChatMessageOptions

use of com.azure.android.communication.chat.models.SendChatMessageOptions in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method canUpdateExistingMessage.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canUpdateExistingMessage(HttpClient httpClient) throws ExecutionException, InterruptedException {
    setupTest(httpClient);
    SendChatMessageOptions messageRequest = super.sendMessageOptions();
    UpdateChatMessageOptions updateMessageRequest = super.updateMessageOptions();
    final String messageId = this.chatThreadClient.sendMessage(messageRequest).get().getId();
    this.chatThreadClient.updateMessage(messageId, updateMessageRequest).get();
    final ChatMessage message = chatThreadClient.getMessage(messageId).get();
    assertEquals(message.getContent().getMessage(), updateMessageRequest.getContent());
    assertEquals(message.getMetadata().containsKey("tags"), false);
    assertEquals(message.getMetadata().get("deliveryMode"), updateMessageRequest.getMetadata().get("deliveryMode"));
    assertEquals(message.getMetadata().get("onedriveReferences"), updateMessageRequest.getMetadata().get("onedriveReferences"));
    assertEquals(message.getMetadata().get("amsreferences"), messageRequest.getMetadata().get("amsreferences"));
    assertEquals(message.getMetadata().get("key"), messageRequest.getMetadata().get("key"));
}
Also used : UpdateChatMessageOptions(com.azure.android.communication.chat.models.UpdateChatMessageOptions) ChatMessage(com.azure.android.communication.chat.models.ChatMessage) SendChatMessageOptions(com.azure.android.communication.chat.models.SendChatMessageOptions) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with SendChatMessageOptions

use of com.azure.android.communication.chat.models.SendChatMessageOptions in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method canUpdateExistingMessageWithResponse.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canUpdateExistingMessageWithResponse(HttpClient httpClient) throws ExecutionException, InterruptedException {
    setupTest(httpClient);
    SendChatMessageOptions messageRequest = super.sendMessageOptions();
    UpdateChatMessageOptions updateMessageRequest = super.updateMessageOptions();
    CompletableFuture<Response<SendChatMessageResult>> completableFuture1 = this.chatThreadClient.sendMessageWithResponse(messageRequest, null);
    Response<SendChatMessageResult> sendResponse = completableFuture1.get();
    assertNotNull(sendResponse);
    final String messageId = sendResponse.getValue().getId();
    assertNotNull(messageId);
    CompletableFuture<Response<Void>> completableFuture2 = this.chatThreadClient.updateMessageWithResponse(messageId, updateMessageRequest, null);
    Response<Void> updateResponse = completableFuture2.get();
    assertNotNull(updateResponse);
    assertEquals(204, updateResponse.getStatusCode());
    CompletableFuture<Response<ChatMessage>> completableFuture3 = chatThreadClient.getMessageWithResponse(messageId, null);
    Response<ChatMessage> getResponse = completableFuture3.get();
    assertNotNull(getResponse);
    ChatMessage message = getResponse.getValue();
    assertEquals(message.getContent().getMessage(), updateMessageRequest.getContent());
    assertEquals(message.getMetadata().containsKey("tags"), false);
    assertEquals(message.getMetadata().get("deliveryMode"), updateMessageRequest.getMetadata().get("deliveryMode"));
    assertEquals(message.getMetadata().get("onedriveReferences"), updateMessageRequest.getMetadata().get("onedriveReferences"));
    assertEquals(message.getMetadata().get("amsreferences"), messageRequest.getMetadata().get("amsreferences"));
    assertEquals(message.getMetadata().get("key"), messageRequest.getMetadata().get("key"));
}
Also used : SendChatMessageResult(com.azure.android.communication.chat.models.SendChatMessageResult) UpdateChatMessageOptions(com.azure.android.communication.chat.models.UpdateChatMessageOptions) ChatMessage(com.azure.android.communication.chat.models.ChatMessage) SendChatMessageOptions(com.azure.android.communication.chat.models.SendChatMessageOptions) Response(com.azure.android.core.rest.Response) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

SendChatMessageOptions (com.azure.android.communication.chat.models.SendChatMessageOptions)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 MethodSource (org.junit.jupiter.params.provider.MethodSource)11 ChatMessage (com.azure.android.communication.chat.models.ChatMessage)7 Response (com.azure.android.core.rest.Response)4 SendChatMessageResult (com.azure.android.communication.chat.models.SendChatMessageResult)3 ListChatMessagesOptions (com.azure.android.communication.chat.models.ListChatMessagesOptions)2 UpdateChatMessageOptions (com.azure.android.communication.chat.models.UpdateChatMessageOptions)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Disabled (org.junit.jupiter.api.Disabled)2 ExecutionException (java.util.concurrent.ExecutionException)1