Skip to content

Commit

Permalink
Add samples for opt-out management and update readme (#47534)
Browse files Browse the repository at this point in the history
  • Loading branch information
phermanov-msft authored Dec 18, 2024
1 parent 6fe23e5 commit 01f732e
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 3 deletions.
44 changes: 42 additions & 2 deletions sdk/communication/Azure.Communication.Sms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
```

## Examples
### Send a 1:1 SMS Message
### SMS
#### Send a 1:1 SMS Message
To send a SMS message, call the `Send` or `SendAsync` function from the `SmsClient`.
```C# Snippet:Azure_Communication_Sms_Tests_SendAsync
SmsSendResult sendResult = await smsClient.SendAsync(
Expand All @@ -53,7 +54,7 @@ SmsSendResult sendResult = await smsClient.SendAsync(
message: "Hi");
Console.WriteLine($"Sms id: {sendResult.MessageId}");
```
### Send a 1:N SMS Message
#### Send a 1:N SMS Message
To send a SMS message to a list of recipients, call the `Send` or `SendAsync` function from the `SmsClient` with a list of recipient's phone numbers.
You may also add pass in an options object to specify whether the delivery report should be enabled and set custom tags.
```C# Snippet:Azure_Communication_SmsClient_Send_GroupSmsWithOptionsAsync
Expand All @@ -72,6 +73,45 @@ foreach (SmsSendResult result in response.Value)
Console.WriteLine($"Send Result Successful: {result.Successful}");
}
```
### Opt Out Management
#### Check if a list of recipients is in the Opt Out list
To check if the recipients are in the Opt Out list, call the function from the `SmsClient.OptOuts.Check` or `SmsClient.OptOuts.CheckAsync` with a list of recipient phone numbers.
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_CheckAsync
var optOutCheckResults = await smsClient.OptOuts.CheckAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutCheckResults.Value)
{
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
}
```
#### Add a list of recipients to Opt Out list
To add the list of recipients to Opt Out list, call the function from the `SmsClient.OptOuts.Add` or `SmsClient.OptOuts.AddAsync` with a list of recipient phone numbers.
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_AddAsync
var optOutAddResults = await smsClient.OptOuts.AddAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutAddResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
```

#### Remove a list of recipients from Opt Out list
To remove the list of recipients to Opt Out list, call the function from the `SmsClient.OptOuts.Remove` or `SmsClient.OptOuts.RemoveAsync` with a list of recipient phone numbers.
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_RemoveAsync
var optOutRemoveResults = await smsClient.OptOuts.RemoveAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutRemoveResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
```



## Troubleshooting
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.
Expand Down
2 changes: 1 addition & 1 deletion sdk/communication/Azure.Communication.Sms/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "net",
"TagPrefix": "net/communication/Azure.Communication.Sms",
"Tag": "net/communication/Azure.Communication.Sms_b96598d9ba"
"Tag": "net/communication/Azure.Communication.Sms_d05ab98bcb"
}
4 changes: 4 additions & 0 deletions sdk/communication/Azure.Communication.Sms/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ To get started you will need to have an Azure Subscription. Once you have this y
This client library allows to do following operations:
- Send SMS to one or more recipients
- Specify optional paramters while sending SMS
- Manage the Opt Out list

#### You can find samples for each of these functions below.
- Send SMS Messages [synchronously][sample_sms] or [asynchronously][sample_sms_async]
- Manage Opt Out list [synchronously][sample_optouts] or [asynchronously][sample_optouts_async]

<!-- LINKS -->
[sample_sms]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Sms/samples/Sample1_SendSms.md
[sample_sms_async]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Sms/samples/Sample1_SendSmsAsync.md
[sample_optouts]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Sms/samples/Sample2_OptOutsApi.md
[sample_optouts_async]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Sms/samples/Sample2_OptOutsApiAsync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Opt Out Management
This sample demonstrates how to check if customers phone numbers are in the Opt Out list, and add or remove entries to it.

To get started you'll need a Communication Service Resource. See [README][README] for prerequisites and instructions.

## Creating an `SmsClient`
SMS clients can be authenticated using the connection string acquired from an Azure Communication Resource in the Azure Portal. Alternatively, SMS clients can also be authenticated using a valid token credential.

```C# Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
string endpoint = "<endpoint_url>";
TokenCredential tokenCredential = new DefaultAzureCredential();
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
```

## Check if a list of recipients is in the Opt Out list
To check if the recipients are in the Opt Out list, call the function from the `SmsClient.OptOuts.Check` with a list of recipient phone numbers.
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Check
var optOutCheckResults = smsClient.OptOuts.Check(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutCheckResults.Value)
{
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
}
```
## Add a list of recipients to Opt Out list
To add the list of recipients to Opt Out list, call the function from the `SmsClient.OptOuts.Add` with a list of recipient phone numbers.
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Add
var optOutAddResults = smsClient.OptOuts.Add(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutAddResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
```

## Remove a list of recipients from Opt Out list
To remove the list of recipients to Opt Out list, call the function from the `SmsClient.OptOuts.Remove` with a list of recipient phone numbers.
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Remove
var optOutRemoveResults = smsClient.OptOuts.Remove(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutRemoveResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
```

[README]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/communication/Azure.Communication.Sms/README.md#getting-started
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Opt Out Management
This sample demonstrates how to check if customers phone numbers are in the Opt Out list, and add or remove entries to it.

To get started you'll need a Communication Service Resource. See [README][README] for prerequisites and instructions.

## Creating an `SmsClient`
SMS clients can be authenticated using the connection string acquired from an Azure Communication Resource in the Azure Portal. Alternatively, SMS clients can also be authenticated using a valid token credential.

```C# Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
string endpoint = "<endpoint_url>";
TokenCredential tokenCredential = new DefaultAzureCredential();
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
```

## Check if a list of recipients is in the Opt Out list
To check if the recipients are in the Opt Out list, call the function from the `SmsClient.OptOuts.CheckAsync` with a list of recipient phone numbers.
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_CheckAsync
var optOutCheckResults = await smsClient.OptOuts.CheckAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutCheckResults.Value)
{
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
}
```
## Add a list of recipients to Opt Out list
To add the list of recipients to Opt Out list, call the function from the `SmsClient.OptOuts.AddAsync` with a list of recipient phone numbers.
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_AddAsync
var optOutAddResults = await smsClient.OptOuts.AddAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutAddResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
```

## Remove a list of recipients from Opt Out list
To remove the list of recipients to Opt Out list, call the function from the `SmsClient.OptOuts.RemoveAsync` with a list of recipient phone numbers.
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_RemoveAsync
var optOutRemoveResults = await smsClient.OptOuts.RemoveAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutRemoveResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
```

[README]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/communication/Azure.Communication.Sms/README.md#getting-started
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<Compile Include="..\..\Shared\tests\CommunicationTestEnvironment.cs" LinkBase="Shared\Communication.Tests" />
</ItemGroup>
<ItemGroup>
<None Include="..\samples\Sample2_OptOutsApi.md" Link="samples\Sample2_OptOutsApi.md" />
<None Include="..\samples\Sample2_OptOutsApiAsync.md" Link="samples\Sample2_OptOutsApiAsync.md" />
<None Include="..\tests.yml" Link="\tests.yml" />
<None Include="..\samples\README.md" Link="samples\README.md" />
<None Include="..\samples\Sample1_SendSms.md" Link="samples\Sample1_SendSms.md" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using NUnit.Framework;

namespace Azure.Communication.Sms.Tests.samples
{
/// <summary>
/// Samples that are used in the README.md file.
/// </summary>
public partial class Sample2_OptOutsApi : SmsClientLiveTestBase
{
public Sample2_OptOutsApi(bool isAsync) : base(isAsync)
{
}

[Test]
[AsyncOnly]
public async Task CheckOptOutAsync()
{
SmsClient smsClient = CreateSmsClient();
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_CheckAsync
var optOutCheckResults = await smsClient.OptOuts.CheckAsync(
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
/*@@*/ from: TestEnvironment.FromPhoneNumber,
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
foreach (var result in optOutCheckResults.Value)
{
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
}
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_CheckAsync
}

[Test]
[AsyncOnly]
public async Task AddOptOutAsync()
{
SmsClient smsClient = CreateSmsClient();
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_AddAsync
var optOutAddResults = await smsClient.OptOuts.AddAsync(
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
/*@@*/ from: TestEnvironment.FromPhoneNumber,
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
foreach (var result in optOutAddResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_AddAsync
}

[Test]
[AsyncOnly]
public async Task RemoveOptOutAsync()
{
SmsClient smsClient = CreateSmsClient();
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_RemoveAsync
var optOutRemoveResults = await smsClient.OptOuts.RemoveAsync(
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
/*@@*/ from: TestEnvironment.FromPhoneNumber,
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });

foreach (var result in optOutRemoveResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_RemoveAsync
}

[Test]
[SyncOnly]
public void CheckOptOut()
{
SmsClient smsClient = CreateSmsClient();
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Check
var optOutCheckResults = smsClient.OptOuts.Check(
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
/*@@*/ from: TestEnvironment.FromPhoneNumber,
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
foreach (var result in optOutCheckResults.Value)
{
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
}
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Check
}

[Test]
[SyncOnly]
public void AddOptOut()
{
SmsClient smsClient = CreateSmsClient();
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Add
var optOutAddResults = smsClient.OptOuts.Add(
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
/*@@*/ from: TestEnvironment.FromPhoneNumber,
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
foreach (var result in optOutAddResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Add
}

[Test]
[SyncOnly]
public void RemoveOptOut()
{
SmsClient smsClient = CreateSmsClient();
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Remove
var optOutRemoveResults = smsClient.OptOuts.Remove(
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
/*@@*/ from: TestEnvironment.FromPhoneNumber,
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });

foreach (var result in optOutRemoveResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Remove
}
}
}

0 comments on commit 01f732e

Please sign in to comment.