Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 34 additions & 11 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#L71-L81 -->
```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#L22-L33 -->
```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#L56-L65 -->
```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,8 +92,10 @@ 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#L119-L128 -->
```java
SmsClient smsClient = createSmsClientUsingAzureKeyCredential();
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
Comment thread
paolamvhz marked this conversation as resolved.
Outdated

SmsSendResult sendResult = smsClient.send(
"<from-phone-number>",
"<to-phone-number>",
Expand All @@ -106,8 +108,10 @@ 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#L132-L149 -->
```java
SmsClient smsClient = createSmsClientUsingAzureKeyCredential();
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
Comment thread
paolamvhz marked this conversation as resolved.
Outdated

SmsSendOptions options = new SmsSendOptions();
options.setDeliveryReportEnabled(true);
options.setTag("Tag");
Expand All @@ -128,15 +132,34 @@ 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.
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
- Please use the `Successful` flag to validate each individual result to verify if the message was sent.
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
<!-- embedme src/samples/java/com/azure/communication/sms/samples/quickstart/ReadmeSamples.java#L248-L273 -->
```java
SmsClient smsClient = createSmsClientUsingAzureKeyCredential();
Comment thread
paolamvhz marked this conversation as resolved.
Outdated

SmsSendOptions options = new SmsSendOptions();
options.setDeliveryReportEnabled(true);
options.setTag("Tag");
Comment thread
paolamvhz marked this conversation as resolved.
Outdated

try {
SmsSendResult sendResult = smsClient.send(
Response<Iterable<SmsSendResult>> sendResults = smsClient.sendWithResponse(
"<from-phone-number>",
"<to-phone-number>",
"Hi"
);
Arrays.asList("<to-phone-number1>", "<to-phone-number2>"),
"Hi",
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
options /* Optional */,
Context.NONE);

Iterable<SmsSendResult> resultOfEachMessage = sendResults.getValue();
for (SmsSendResult result : resultOfEachMessage) {
if (!result.isSuccessful()) {
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
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());
Comment thread
paolamvhz marked this conversation as resolved.
}
}

Comment thread
paolamvhz marked this conversation as resolved.
Outdated
} catch (RuntimeException ex) {
System.out.println(ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
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;
import reactor.core.publisher.Mono;

public class ReadmeSamples {
public SmsClient createSmsClientUsingAzureKeyCredential() {
Expand All @@ -31,6 +35,23 @@ public SmsClient createSmsClientUsingAzureKeyCredential() {
return smsClient;
}

public SmsAsyncClient createSmsAsyncClientUsingAzureKeyCredential() {
Comment thread
paolamvhz marked this conversation as resolved.
// 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,6 +83,38 @@ public SmsClient createSmsClientWithAAD() {
return smsClient;
}

public SmsAsyncClient createAsyncClientUsingTokenCredential() {
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();
SmsAsyncClient smsClient = new SmsClientBuilder().
endpoint(endpoint)
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
.credential(tokenCredential)
.httpClient(httpClient)
.buildAsyncClient();
return smsClient;

}

public SmsClient createSyncClientUsingTokenCredential() {
Comment thread
paolamvhz marked this conversation as resolved.
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)
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
.credential(tokenCredential)
.httpClient(httpClient)
.buildClient();
return smsClient;

Comment thread
paolamvhz marked this conversation as resolved.
Outdated
}

public void sendMessageToOneRecipient() {
SmsClient smsClient = createSmsClientUsingAzureKeyCredential();

Expand Down Expand Up @@ -96,10 +149,49 @@ public void sendMessageToGroupWithOptions() {
}
}

public void sendMessageAsyncToOneRecipient() {
SmsAsyncClient smsClient = createSmsAsyncClientUsingAzureKeyCredential();

Mono<SmsSendResult> sendResult = smsClient.send(
"<from-phone-number>",
"<to-phone-number>",
"Hi");
Mono<Boolean> isSuccessful = sendResult.flatMap(result -> {
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
System.out.println("Message Id: " + result.getMessageId());
System.out.println("Recipient Number: " + result.getTo());
System.out.println("Send Result Successful:" + result.isSuccessful());
return Mono.just(result.isSuccessful());
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
});
}

public void sendMessageAsyncClientToGroup() {
SmsAsyncClient smsClient = createSmsAsyncClientUsingAzureKeyCredential();

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

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

Mono <Iterable<SmsSendResult>> resultOfEachMessage = sendResults.flatMap(response -> {
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
Iterable<SmsSendResult> iterableResults = response.getValue();
for (SmsSendResult result : iterableResults) {
System.out.println("Message Id: " + result.getMessageId());
System.out.println("Recipient Number: " + result.getTo());
System.out.println("Send Result Successful:" + result.isSuccessful());
}
return Mono.just(iterableResults);
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
});
}

/**
* Sample code for troubleshooting
*/
public void sendSMSTroubleshooting() {
public void catchHttpErrorOnRequestSync() {
SmsClient smsClient = createSmsClientUsingAzureKeyCredential();
try {
SmsSendResult sendResult = smsClient.send(
Expand All @@ -111,4 +203,74 @@ public void sendSMSTroubleshooting() {
System.out.println(ex.getMessage());
}
}

public void catchHttpErrorOnRequestAsync() {
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
SmsAsyncClient smsClient = createSmsAsyncClientUsingAzureKeyCredential();
try {
Mono<SmsSendResult> sendResult = smsClient.send(
"<from-phone-number>",
"<to-phone-number>",
"Hi"
);
} catch (RuntimeException ex) {
System.out.println(ex.getMessage());
}
}

public void failedMessagesAsync() {
SmsAsyncClient smsClient = createSmsAsyncClientUsingAzureKeyCredential();

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

Comment thread
paolamvhz marked this conversation as resolved.
Outdated
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
Mono<Response<Iterable<SmsSendResult>>> sendResults = smsClient.sendWithResponse(
"<from-phone-number>",
Arrays.asList("<to-phone-number1>", "<to-phone-number2>"),
"Hi",
options /* Optional */);

Mono <Iterable<SmsSendResult>> resultOfEachMessage = sendResults.flatMap(response -> {
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
Iterable<SmsSendResult> iterableResults = response.getValue();
for (SmsSendResult result : iterableResults) {
if (!result.isSuccessful()) {
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
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());
}
}
return Mono.just(iterableResults);
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
});

}

public void failedMessagesSync() {
SmsClient smsClient = createSmsClientUsingAzureKeyCredential();

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

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

Iterable<SmsSendResult> resultOfEachMessage = sendResults.getValue();
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
for (SmsSendResult result : resultOfEachMessage) {
if (!result.isSuccessful()) {
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
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());
}
}

Comment thread
paolamvhz marked this conversation as resolved.
Outdated
Comment thread
paolamvhz marked this conversation as resolved.
Outdated
} catch (RuntimeException ex) {
System.out.println(ex.getMessage());
}
}

Comment thread
paolamvhz marked this conversation as resolved.
Outdated
}