Skip to content

Commit 01f732e

Browse files
Add samples for opt-out management and update readme (Azure#47534)
1 parent 6fe23e5 commit 01f732e

File tree

7 files changed

+281
-3
lines changed

7 files changed

+281
-3
lines changed

sdk/communication/Azure.Communication.Sms/README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
4444
```
4545

4646
## Examples
47-
### Send a 1:1 SMS Message
47+
### SMS
48+
#### Send a 1:1 SMS Message
4849
To send a SMS message, call the `Send` or `SendAsync` function from the `SmsClient`.
4950
```C# Snippet:Azure_Communication_Sms_Tests_SendAsync
5051
SmsSendResult sendResult = await smsClient.SendAsync(
@@ -53,7 +54,7 @@ SmsSendResult sendResult = await smsClient.SendAsync(
5354
message: "Hi");
5455
Console.WriteLine($"Sms id: {sendResult.MessageId}");
5556
```
56-
### Send a 1:N SMS Message
57+
#### Send a 1:N SMS Message
5758
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.
5859
You may also add pass in an options object to specify whether the delivery report should be enabled and set custom tags.
5960
```C# Snippet:Azure_Communication_SmsClient_Send_GroupSmsWithOptionsAsync
@@ -72,6 +73,45 @@ foreach (SmsSendResult result in response.Value)
7273
Console.WriteLine($"Send Result Successful: {result.Successful}");
7374
}
7475
```
76+
### Opt Out Management
77+
#### Check if a list of recipients is in the Opt Out list
78+
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.
79+
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_CheckAsync
80+
var optOutCheckResults = await smsClient.OptOuts.CheckAsync(
81+
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
82+
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
83+
foreach (var result in optOutCheckResults.Value)
84+
{
85+
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
86+
}
87+
```
88+
#### Add a list of recipients to Opt Out list
89+
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.
90+
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_AddAsync
91+
var optOutAddResults = await smsClient.OptOuts.AddAsync(
92+
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
93+
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
94+
foreach (var result in optOutAddResults.Value)
95+
{
96+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
97+
}
98+
```
99+
100+
#### Remove a list of recipients from Opt Out list
101+
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.
102+
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_RemoveAsync
103+
var optOutRemoveResults = await smsClient.OptOuts.RemoveAsync(
104+
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
105+
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
106+
107+
foreach (var result in optOutRemoveResults.Value)
108+
{
109+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
110+
}
111+
```
112+
113+
114+
75115
## Troubleshooting
76116
SMS operations will throw an exception if the request to the server fails.
77117
Exceptions will not be thrown if the error is caused by an individual message, only if something fails with the overall request.

sdk/communication/Azure.Communication.Sms/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "net",
44
"TagPrefix": "net/communication/Azure.Communication.Sms",
5-
"Tag": "net/communication/Azure.Communication.Sms_b96598d9ba"
5+
"Tag": "net/communication/Azure.Communication.Sms_d05ab98bcb"
66
}

sdk/communication/Azure.Communication.Sms/samples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ To get started you will need to have an Azure Subscription. Once you have this y
1717
This client library allows to do following operations:
1818
- Send SMS to one or more recipients
1919
- Specify optional paramters while sending SMS
20+
- Manage the Opt Out list
2021

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

2426
<!-- LINKS -->
2527
[sample_sms]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Sms/samples/Sample1_SendSms.md
2628
[sample_sms_async]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Sms/samples/Sample1_SendSmsAsync.md
29+
[sample_optouts]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Sms/samples/Sample2_OptOutsApi.md
30+
[sample_optouts_async]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Sms/samples/Sample2_OptOutsApiAsync.md
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Opt Out Management
2+
This sample demonstrates how to check if customers phone numbers are in the Opt Out list, and add or remove entries to it.
3+
4+
To get started you'll need a Communication Service Resource. See [README][README] for prerequisites and instructions.
5+
6+
## Creating an `SmsClient`
7+
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.
8+
9+
```C# Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
10+
string endpoint = "<endpoint_url>";
11+
TokenCredential tokenCredential = new DefaultAzureCredential();
12+
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
13+
```
14+
15+
## Check if a list of recipients is in the Opt Out list
16+
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.
17+
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Check
18+
var optOutCheckResults = smsClient.OptOuts.Check(
19+
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
20+
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
21+
foreach (var result in optOutCheckResults.Value)
22+
{
23+
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
24+
}
25+
```
26+
## Add a list of recipients to Opt Out list
27+
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.
28+
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Add
29+
var optOutAddResults = smsClient.OptOuts.Add(
30+
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
31+
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
32+
foreach (var result in optOutAddResults.Value)
33+
{
34+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
35+
}
36+
```
37+
38+
## Remove a list of recipients from Opt Out list
39+
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.
40+
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Remove
41+
var optOutRemoveResults = smsClient.OptOuts.Remove(
42+
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
43+
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
44+
45+
foreach (var result in optOutRemoveResults.Value)
46+
{
47+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
48+
}
49+
```
50+
51+
[README]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/communication/Azure.Communication.Sms/README.md#getting-started
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Opt Out Management
2+
This sample demonstrates how to check if customers phone numbers are in the Opt Out list, and add or remove entries to it.
3+
4+
To get started you'll need a Communication Service Resource. See [README][README] for prerequisites and instructions.
5+
6+
## Creating an `SmsClient`
7+
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.
8+
9+
```C# Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
10+
string endpoint = "<endpoint_url>";
11+
TokenCredential tokenCredential = new DefaultAzureCredential();
12+
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
13+
```
14+
15+
## Check if a list of recipients is in the Opt Out list
16+
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.
17+
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_CheckAsync
18+
var optOutCheckResults = await smsClient.OptOuts.CheckAsync(
19+
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
20+
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
21+
foreach (var result in optOutCheckResults.Value)
22+
{
23+
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
24+
}
25+
```
26+
## Add a list of recipients to Opt Out list
27+
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.
28+
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_AddAsync
29+
var optOutAddResults = await smsClient.OptOuts.AddAsync(
30+
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
31+
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
32+
foreach (var result in optOutAddResults.Value)
33+
{
34+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
35+
}
36+
```
37+
38+
## Remove a list of recipients from Opt Out list
39+
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.
40+
```C# Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_RemoveAsync
41+
var optOutRemoveResults = await smsClient.OptOuts.RemoveAsync(
42+
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
43+
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
44+
45+
foreach (var result in optOutRemoveResults.Value)
46+
{
47+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
48+
}
49+
```
50+
51+
[README]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/communication/Azure.Communication.Sms/README.md#getting-started

sdk/communication/Azure.Communication.Sms/tests/Azure.Communication.Sms.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<Compile Include="..\..\Shared\tests\CommunicationTestEnvironment.cs" LinkBase="Shared\Communication.Tests" />
1717
</ItemGroup>
1818
<ItemGroup>
19+
<None Include="..\samples\Sample2_OptOutsApi.md" Link="samples\Sample2_OptOutsApi.md" />
20+
<None Include="..\samples\Sample2_OptOutsApiAsync.md" Link="samples\Sample2_OptOutsApiAsync.md" />
1921
<None Include="..\tests.yml" Link="\tests.yml" />
2022
<None Include="..\samples\README.md" Link="samples\README.md" />
2123
<None Include="..\samples\Sample1_SendSms.md" Link="samples\Sample1_SendSms.md" />
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Azure.Core.TestFramework;
7+
using NUnit.Framework;
8+
9+
namespace Azure.Communication.Sms.Tests.samples
10+
{
11+
/// <summary>
12+
/// Samples that are used in the README.md file.
13+
/// </summary>
14+
public partial class Sample2_OptOutsApi : SmsClientLiveTestBase
15+
{
16+
public Sample2_OptOutsApi(bool isAsync) : base(isAsync)
17+
{
18+
}
19+
20+
[Test]
21+
[AsyncOnly]
22+
public async Task CheckOptOutAsync()
23+
{
24+
SmsClient smsClient = CreateSmsClient();
25+
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_CheckAsync
26+
var optOutCheckResults = await smsClient.OptOuts.CheckAsync(
27+
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
28+
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
29+
/*@@*/ from: TestEnvironment.FromPhoneNumber,
30+
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
31+
foreach (var result in optOutCheckResults.Value)
32+
{
33+
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
34+
}
35+
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_CheckAsync
36+
}
37+
38+
[Test]
39+
[AsyncOnly]
40+
public async Task AddOptOutAsync()
41+
{
42+
SmsClient smsClient = CreateSmsClient();
43+
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_AddAsync
44+
var optOutAddResults = await smsClient.OptOuts.AddAsync(
45+
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
46+
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
47+
/*@@*/ from: TestEnvironment.FromPhoneNumber,
48+
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
49+
foreach (var result in optOutAddResults.Value)
50+
{
51+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
52+
}
53+
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_AddAsync
54+
}
55+
56+
[Test]
57+
[AsyncOnly]
58+
public async Task RemoveOptOutAsync()
59+
{
60+
SmsClient smsClient = CreateSmsClient();
61+
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_RemoveAsync
62+
var optOutRemoveResults = await smsClient.OptOuts.RemoveAsync(
63+
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
64+
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
65+
/*@@*/ from: TestEnvironment.FromPhoneNumber,
66+
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
67+
68+
foreach (var result in optOutRemoveResults.Value)
69+
{
70+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
71+
}
72+
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_RemoveAsync
73+
}
74+
75+
[Test]
76+
[SyncOnly]
77+
public void CheckOptOut()
78+
{
79+
SmsClient smsClient = CreateSmsClient();
80+
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Check
81+
var optOutCheckResults = smsClient.OptOuts.Check(
82+
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
83+
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
84+
/*@@*/ from: TestEnvironment.FromPhoneNumber,
85+
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
86+
foreach (var result in optOutCheckResults.Value)
87+
{
88+
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
89+
}
90+
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Check
91+
}
92+
93+
[Test]
94+
[SyncOnly]
95+
public void AddOptOut()
96+
{
97+
SmsClient smsClient = CreateSmsClient();
98+
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Add
99+
var optOutAddResults = smsClient.OptOuts.Add(
100+
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
101+
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
102+
/*@@*/ from: TestEnvironment.FromPhoneNumber,
103+
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
104+
foreach (var result in optOutAddResults.Value)
105+
{
106+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
107+
}
108+
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Add
109+
}
110+
111+
[Test]
112+
[SyncOnly]
113+
public void RemoveOptOut()
114+
{
115+
SmsClient smsClient = CreateSmsClient();
116+
#region Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Remove
117+
var optOutRemoveResults = smsClient.OptOuts.Remove(
118+
//@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
119+
//@@ to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
120+
/*@@*/ from: TestEnvironment.FromPhoneNumber,
121+
/*@@*/ to: new string[] { TestEnvironment.ToPhoneNumber, TestEnvironment.ToPhoneNumber });
122+
123+
foreach (var result in optOutRemoveResults.Value)
124+
{
125+
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
126+
}
127+
#endregion Snippet:Azure_Communication_Sms_OptOuts_Tests_Samples_Remove
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)