|
| 1 | +--- |
| 2 | +title: include file |
| 3 | +description: include file |
| 4 | +services: azure-communication-services |
| 5 | +author: dbasantes |
| 6 | + |
| 7 | +ms.service: azure-communication-services |
| 8 | +ms.subservice: azure-communication-services |
| 9 | +ms.date: 12/06/2024 |
| 10 | +ms.topic: include |
| 11 | +ms.custom: include file |
| 12 | +ms.author: dbasantes |
| 13 | +--- |
| 14 | + |
| 15 | +Get started with Azure Communication Services SMS Opt-out API by leveraging the following Javascript sample code. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 20 | +- Browser or Node.js Active LTS and Maintenance LTS versions (8.11.1 and 10.14.1 are recommended). |
| 21 | +- An active Communication Services resource and connection string. See [Create a Communication Services resource](https://learn.microsoft.com/azure/communication-services/quickstarts/create-communication-resource). |
| 22 | +- An SMS-enabled telephone number. See [Get a phone number](https://learn.microsoft.com/azure/communication-services/quickstarts/telephony/get-phone-number). |
| 23 | +- [CryptoJS](https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/crypto-js/CryptoJS%20v3.1.2.zip) is JavaScript implementations of standard and secure cryptographic algorithms. |
| 24 | + |
| 25 | +## Sample code to use Opt-Out API |
| 26 | + |
| 27 | +Below is a sample Javascript implementation demonstrating how to use the Opt-Out Management API to add, remove, or check opt-out entries programmatically. |
| 28 | + |
| 29 | +```html |
| 30 | +<script src="Scripts/CryptoJS/sha256-min.js" type="text/javascript"></script> |
| 31 | +<script src="Scripts/CryptoJS/hmac-sha256.js" type="text/javascript"></script> |
| 32 | +<script src="Scripts/CryptoJS/enc-base64-min.js" type="text/javascript"></script> |
| 33 | + |
| 34 | +``` |
| 35 | + |
| 36 | +```js |
| 37 | +const ConnectionString = "endpoint=https://[CONTOSO].communication.azure.com/;accesskey=******"; |
| 38 | + |
| 39 | +// Sample for Add action. Replace with Check or Remove as necessary. |
| 40 | +function sendOptOutAdd(acsResourceConnectionString, payload, apiVersion = "2024-12-10-preview") |
| 41 | +{ |
| 42 | + try |
| 43 | + { |
| 44 | + var acsRCS = acsResourceConnectionString |
| 45 | + .split(";") |
| 46 | + .map(i => |
| 47 | + { |
| 48 | + var p = i.indexOf("="); |
| 49 | + return [i.substr(0, p), i.substr(p + 1)]; |
| 50 | + }) |
| 51 | + .reduce((a, i) => ({ ...a, [i[0]]: i[1] }), {}); |
| 52 | + var uri = `${trimEnd(acsRCS.endpoint, "/")}/sms/optouts:add?api-version=${apiVersion}`; |
| 53 | + var url = new URL(uri); |
| 54 | + var method = "POST"; |
| 55 | + var utcNow = new Date().toUTCString(); |
| 56 | + var bodyJson = JSON.stringify(payload); |
| 57 | + var hashedBody = CryptoJS.SHA256(bodyJson).toString(CryptoJS.enc.Base64); |
| 58 | + var stringToSign = `${method}\n${url.pathname}${url.search}\n${utcNow};${url.host};${hashedBody}`; |
| 59 | + var signature = CryptoJS.HmacSHA256(stringToSign, CryptoJS.enc.Base64.parse(acsRCS.accesskey)).toString(CryptoJS.enc.Base64); |
| 60 | + |
| 61 | + fetch(uri, { |
| 62 | + method: method, |
| 63 | + headers: { |
| 64 | + "content-type": "application/json", |
| 65 | + "x-ms-date": utcNow, |
| 66 | + "x-ms-content-sha256": hashedBody, |
| 67 | + Authorization: `HMAC-SHA256 SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature=${signature}` |
| 68 | + }, |
| 69 | + body: bodyJson |
| 70 | + }) |
| 71 | + .then(response => response.json()) |
| 72 | + .then(console.warn) |
| 73 | + .catch(console.error); |
| 74 | + } |
| 75 | + catch (ex) |
| 76 | + { |
| 77 | + console.error(ex); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function trimEnd(s, c) |
| 82 | +{ |
| 83 | + while (s.slice(-1) == c) |
| 84 | + s = s.slice(0, -1); |
| 85 | + return s; |
| 86 | +} |
| 87 | + |
| 88 | +// Usage |
| 89 | + |
| 90 | +var payload = { |
| 91 | + from: "+15551234567", |
| 92 | + recipients: [ |
| 93 | + { to: "+15550112233" } |
| 94 | + ], |
| 95 | +}; |
| 96 | + |
| 97 | +sendOptOutAdd(ConnectionString, payload); |
| 98 | + |
| 99 | +``` |
1 | 100 |
|
0 commit comments