Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions sdk/communication/azure-communication-sms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L50-L60 -->
<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L70-L80 -->
```java
// You can find your endpoint and access key from your resource in the Azure Portal
String endpoint = "https://<RESOURCE_NAME>.communication.azure.com";
Expand All @@ -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.

<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L18-L29 -->
<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L21-L32 -->
```java
// You can find your endpoint and access key from your resource in the Azure Portal
String endpoint = "https://<resource-name>.communication.azure.com";
Expand All @@ -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.
<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L35-L44 -->
<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L55-L64 -->
```java
// You can find your connection string from your resource in the Azure Portal
String connectionString = "https://<resource-name>.communication.azure.com/;<access-key>";
Expand All @@ -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.

<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L68-L75 -->
<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L103-L110 -->
```java
SmsSendResult sendResult = smsClient.send(
"<from-phone-number>",
"<to-phone-number>",
"Hi");
"Weekly Promotion");

System.out.println("Message Id: " + sendResult.getMessageId());
System.out.println("Recipient Number: " + sendResult.getTo());
Expand All @@ -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.

<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L81-L96 -->
<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L116-L131 -->
```java
SmsSendOptions options = new SmsSendOptions();
options.setDeliveryReportEnabled(true);
options.setTag("Tag");
options.setTag("Marketing");

Iterable<SmsSendResult> sendResults = smsClient.sendWithResponse(
"<from-phone-number>",
Arrays.asList("<to-phone-number1>", "<to-phone-number2>"),
"Hi",
"Weekly Promotion",
options /* Optional */,
Context.NONE).getValue();

Expand All @@ -128,15 +128,32 @@ for (SmsSendResult result : sendResults) {

## Troubleshooting

All SMS service operations will throw an exception on failure.
<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L104-L112 -->
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.
<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L153-L176 -->
```java
try {
SmsSendResult sendResult = smsClient.send(
SmsSendOptions options = new SmsSendOptions();
options.setDeliveryReportEnabled(true);
options.setTag("Marketing");

Response<Iterable<SmsSendResult>> sendResults = smsClient.sendWithResponse(
"<from-phone-number>",
"<to-phone-number>",
"Hi"
);
Arrays.asList("<to-phone-number1>", "<to-phone-number2>"),
"Weekly Promotion",
options /* Optional */,
Context.NONE);

Iterable<SmsSendResult> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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://<resource-name>.communication.azure.com";
AzureKeyCredential azureKeyCredential = new AzureKeyCredential("<access-key>");

// 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://<resource-name>.communication.azure.com/;<access-key>";
Expand Down Expand Up @@ -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://<RESOURCE_NAME>.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(
"<from-phone-number>",
"<to-phone-number>",
"Hi");
"Weekly Promotion");

System.out.println("Message Id: " + sendResult.getMessageId());
System.out.println("Recipient Number: " + sendResult.getTo());
Expand All @@ -80,12 +115,12 @@ public void sendMessageToGroupWithOptions() {

SmsSendOptions options = new SmsSendOptions();
options.setDeliveryReportEnabled(true);
options.setTag("Tag");
options.setTag("Marketing");

Iterable<SmsSendResult> sendResults = smsClient.sendWithResponse(
"<from-phone-number>",
Arrays.asList("<to-phone-number1>", "<to-phone-number2>"),
"Hi",
"Weekly Promotion",
options /* Optional */,
Context.NONE).getValue();

Expand All @@ -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(
"<from-phone-number>",
"<to-phone-number>",
"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<Iterable<SmsSendResult>> sendResults = smsClient.sendWithResponse(
"<from-phone-number>",
Arrays.asList("<to-phone-number1>", "<to-phone-number2>"),
"Weekly Promotion",
options /* Optional */,
Context.NONE);

Iterable<SmsSendResult> 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());
}
}
}