diff --git a/sdk/communication/azure-communication-sms/README.md b/sdk/communication/azure-communication-sms/README.md index dd80c04ee861..df3a58669529 100644 --- a/sdk/communication/azure-communication-sms/README.md +++ b/sdk/communication/azure-communication-sms/README.md @@ -33,7 +33,7 @@ A `DefaultAzureCredential` object must be passed to the `SmsClientBuilder` via t `AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID` and `AZURE_TENANT_ID` environment variables are needed to create a DefaultAzureCredential object. - + ```java // You can find your endpoint and access key from your resource in the Azure Portal String endpoint = "https://.communication.azure.com"; @@ -52,7 +52,7 @@ SmsClient smsClient = new SmsClientBuilder() SMS uses HMAC authentication with the resource access key. The access key must be provided to the `SmsClientBuilder` via the credential() function. Endpoint and httpClient must also be set via the endpoint() and httpClient() functions respectively. - + ```java // You can find your endpoint and access key from your resource in the Azure Portal String endpoint = "https://.communication.azure.com"; @@ -69,7 +69,7 @@ SmsClient smsClient = new SmsClientBuilder() ``` Alternatively, you can provide the entire connection string using the connectionString() function instead of providing the endpoint and access key. - + ```java // You can find your connection string from your resource in the Azure Portal String connectionString = "https://.communication.azure.com/;"; @@ -92,12 +92,12 @@ There are two different forms of authentication to use the Azure Communication S ### Send a 1:1 SMS Message Use the `send` or `sendWithResponse` function to send a SMS message to a single phone number. - + ```java SmsSendResult sendResult = smsClient.send( "", "", - "Hi"); + "Weekly Promotion"); System.out.println("Message Id: " + sendResult.getMessageId()); System.out.println("Recipient Number: " + sendResult.getTo()); @@ -106,16 +106,16 @@ System.out.println("Send Result Successful:" + sendResult.isSuccessful()); ### Send a 1:N SMS Message To send a SMS message to a list of recipients, call the `send` or `sendWithResponse` function with a list of recipient phone numbers. You may also add pass in an options object to specify whether the delivery report should be enabled and set custom tags. - + ```java SmsSendOptions options = new SmsSendOptions(); options.setDeliveryReportEnabled(true); -options.setTag("Tag"); +options.setTag("Marketing"); Iterable sendResults = smsClient.sendWithResponse( "", Arrays.asList("", ""), - "Hi", + "Weekly Promotion", options /* Optional */, Context.NONE).getValue(); @@ -128,15 +128,32 @@ for (SmsSendResult result : sendResults) { ## Troubleshooting -All SMS service operations will throw an exception on failure. - +SMS operations will throw an exception if the request to the server fails. +Exceptions will not be thrown if the error is caused by an individual message, only if something fails with the overall request. +Please use the `isSuccessful()` flag to validate each individual result to verify if the message was sent. + ```java try { - SmsSendResult sendResult = smsClient.send( + SmsSendOptions options = new SmsSendOptions(); + options.setDeliveryReportEnabled(true); + options.setTag("Marketing"); + + Response> sendResults = smsClient.sendWithResponse( "", - "", - "Hi" - ); + Arrays.asList("", ""), + "Weekly Promotion", + options /* Optional */, + Context.NONE); + + Iterable smsSendResults = sendResults.getValue(); + for (SmsSendResult result : smsSendResults) { + if (result.isSuccessful()) { + System.out.println("Successfully sent this message: " + result.getMessageId() + " to " + result.getTo()); + } else { + System.out.println("Something went wrong when trying to send this message " + result.getMessageId() + " to " + result.getTo()); + System.out.println("Status code " + result.getHttpStatusCode() + " and error message " + result.getErrorMessage()); + } + } } catch (RuntimeException ex) { System.out.println(ex.getMessage()); } diff --git a/sdk/communication/azure-communication-sms/src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java b/sdk/communication/azure-communication-sms/src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java index f151036d6855..1856c933f106 100644 --- a/sdk/communication/azure-communication-sms/src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java +++ b/sdk/communication/azure-communication-sms/src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java @@ -3,13 +3,16 @@ package com.azure.communication.sms.samples.quickstart; import java.util.Arrays; +import com.azure.communication.sms.SmsAsyncClient; import com.azure.communication.sms.SmsClient; import com.azure.communication.sms.SmsClientBuilder; import com.azure.communication.sms.models.SmsSendOptions; import com.azure.communication.sms.SmsSendResult; import com.azure.core.credential.AzureKeyCredential; +import com.azure.core.credential.TokenCredential; import com.azure.core.http.HttpClient; import com.azure.core.http.netty.NettyAsyncHttpClientBuilder; +import com.azure.core.http.rest.Response; import com.azure.core.util.Context; import com.azure.identity.DefaultAzureCredentialBuilder; @@ -31,6 +34,23 @@ public SmsClient createSmsClientUsingAzureKeyCredential() { return smsClient; } + public SmsAsyncClient createSmsAsyncClientUsingAzureKeyCredential() { + // You can find your endpoint and access key from your resource in the Azure Portal + String endpoint = "https://.communication.azure.com"; + AzureKeyCredential azureKeyCredential = new AzureKeyCredential(""); + + // Create an HttpClient builder of your choice and customize it + HttpClient httpClient = new NettyAsyncHttpClientBuilder().build(); + + SmsAsyncClient smsClient = new SmsClientBuilder() + .endpoint(endpoint) + .credential(azureKeyCredential) + .httpClient(httpClient) + .buildAsyncClient(); + + return smsClient; + } + public SmsClient createSmsClientWithConnectionString() { // You can find your connection string from your resource in the Azure Portal String connectionString = "https://.communication.azure.com/;"; @@ -62,13 +82,28 @@ public SmsClient createSmsClientWithAAD() { return smsClient; } + public SmsClient createSyncClientUsingTokenCredential() { + TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build(); + // You can find your endpoint and access key from your resource in the Azure Portal + String endpoint = "https://.communication.azure.com"; + + // Create an HttpClient builder of your choice and customize it + HttpClient httpClient = new NettyAsyncHttpClientBuilder().build(); + SmsClient smsClient = new SmsClientBuilder() + .endpoint(endpoint) + .credential(tokenCredential) + .httpClient(httpClient) + .buildClient(); + return smsClient; + } + public void sendMessageToOneRecipient() { SmsClient smsClient = createSmsClientUsingAzureKeyCredential(); SmsSendResult sendResult = smsClient.send( "", "", - "Hi"); + "Weekly Promotion"); System.out.println("Message Id: " + sendResult.getMessageId()); System.out.println("Recipient Number: " + sendResult.getTo()); @@ -80,12 +115,12 @@ public void sendMessageToGroupWithOptions() { SmsSendOptions options = new SmsSendOptions(); options.setDeliveryReportEnabled(true); - options.setTag("Tag"); + options.setTag("Marketing"); Iterable sendResults = smsClient.sendWithResponse( "", Arrays.asList("", ""), - "Hi", + "Weekly Promotion", options /* Optional */, Context.NONE).getValue(); @@ -99,16 +134,45 @@ public void sendMessageToGroupWithOptions() { /** * Sample code for troubleshooting */ - public void sendSMSTroubleshooting() { + public void catchHttpErrorOnRequest() { SmsClient smsClient = createSmsClientUsingAzureKeyCredential(); try { SmsSendResult sendResult = smsClient.send( "", "", - "Hi" + "Weekly Promotion" ); } catch (RuntimeException ex) { System.out.println(ex.getMessage()); } } + + public void sendMessageTroubleShooting() { + SmsClient smsClient = createSmsClientUsingAzureKeyCredential(); + + try { + SmsSendOptions options = new SmsSendOptions(); + options.setDeliveryReportEnabled(true); + options.setTag("Marketing"); + + Response> sendResults = smsClient.sendWithResponse( + "", + Arrays.asList("", ""), + "Weekly Promotion", + options /* Optional */, + Context.NONE); + + Iterable smsSendResults = sendResults.getValue(); + for (SmsSendResult result : smsSendResults) { + if (result.isSuccessful()) { + System.out.println("Successfully sent this message: " + result.getMessageId() + " to " + result.getTo()); + } else { + System.out.println("Something went wrong when trying to send this message " + result.getMessageId() + " to " + result.getTo()); + System.out.println("Status code " + result.getHttpStatusCode() + " and error message " + result.getErrorMessage()); + } + } + } catch (RuntimeException ex) { + System.out.println(ex.getMessage()); + } + } }