|
2 | 2 | title: Include file
|
3 | 3 | description: Include file
|
4 | 4 | services: azure-communication-services
|
5 |
| -author: shamkh |
| 5 | +author: arifibrahim4 |
6 | 6 | ms.service: azure-communication-services
|
7 | 7 | ms.subservice: advanced-messaging
|
8 |
| -ms.date: 12/15/2024 |
| 8 | +ms.date: 1/24/2025 |
9 | 9 | ms.topic: include
|
10 | 10 | ms.custom: include file
|
11 |
| -ms.author: shamkh |
| 11 | +ms.author: armohamed |
12 | 12 | ---
|
13 | 13 |
|
14 |
| -## Prerequisites |
| 14 | +## Set up environment |
15 | 15 |
|
16 |
| -## Setting up |
| 16 | +To set up an environment for sending messages, complete the steps in the following sections. |
17 | 17 |
|
18 | 18 | [!INCLUDE [Setting up for JavaScript Application](../javascript-application-setup.md)]
|
| 19 | + |
| 20 | +## Code examples |
| 21 | + |
| 22 | +Follow these steps to add required code snippets to your `send-messages.js` file. |
| 23 | +- [Send an Interactive List options message to a WhatsApp user](#send-an-interactive-reply-button-message-to-a-whatsapp-user). |
| 24 | +- [Send an Interactive Reply Button message to a WhatsApp user](#send-an-interactive-reply-button-message-to-a-whatsapp-user). |
| 25 | +- [Send an Interactive Click-to-action Url based message to a WhatsApp user](#send-an-interactive-click-to-action-url-based-message-to-a-whatsapp-user) |
| 26 | + |
| 27 | + |
| 28 | +### Send an Interactive List options message to a WhatsApp user |
| 29 | + |
| 30 | +The Messages SDK enables Contoso to send interactive WhatsApp messages, when initiated by a WhatsApp users. To send interactive messages: |
| 31 | +- [WhatsApp Channel ID](#set-channel-registration-id). |
| 32 | +- [Recipient Phone Number in E16 format](#set-recipient-list). |
| 33 | +- Interactive message to be sent. |
| 34 | + |
| 35 | +> [!IMPORTANT] |
| 36 | +> To send a interactive message to a WhatsApp user, the WhatsApp user must first send a message to the WhatsApp Business Account. For more information, see [Start sending messages between business and WhatsApp user](#start-sending-messages-between-a-business-and-a-whatsapp-user). |
| 37 | +
|
| 38 | +In this example, the business sends an interactive shipping options message to the user. |
| 39 | + |
| 40 | +```javascript |
| 41 | +/** |
| 42 | + * @summary Send a interactive message |
| 43 | + */ |
| 44 | + |
| 45 | +const { AzureKeyCredential } = require("@azure/core-auth"); |
| 46 | +const NotificationClient = require("@azure-rest/communication-messages").default, |
| 47 | + { isUnexpected } = require("@azure-rest/communication-messages"); |
| 48 | +// Load the .env file if it exists |
| 49 | +require("dotenv").config(); |
| 50 | + |
| 51 | +async function main() { |
| 52 | + const credential = new AzureKeyCredential(process.env.ACS_ACCESS_KEY || ""); |
| 53 | + const endpoint = process.env.ACS_URL || ""; |
| 54 | + const client = NotificationClient(endpoint, credential); |
| 55 | + |
| 56 | + const interactiveMessage = { |
| 57 | + body: { |
| 58 | + kind: "text", |
| 59 | + text: "Do you want to proceed?", |
| 60 | + }, |
| 61 | + action: { |
| 62 | + kind: "whatsAppListAction", |
| 63 | + content: { |
| 64 | + kind: "group", |
| 65 | + title: "Shipping Options", |
| 66 | + groups: [ |
| 67 | + { |
| 68 | + title: "Express Delivery", |
| 69 | + items: [ |
| 70 | + { |
| 71 | + id: "priority_mail_express", |
| 72 | + title: "Priority Mail Express", |
| 73 | + description: "Delivered on same day!", |
| 74 | + }, |
| 75 | + { |
| 76 | + id: "priority_mail", |
| 77 | + title: "Priority Mail", |
| 78 | + description: "Delivered in 1-2 days", |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + { |
| 83 | + title: "Normal Delivery", |
| 84 | + items: [ |
| 85 | + { |
| 86 | + id: "usps_ground_advantage", |
| 87 | + title: "USPS Ground Advantage", |
| 88 | + description: "Delivered in 2-5 days", |
| 89 | + }, |
| 90 | + { |
| 91 | + id: "usps_mail", |
| 92 | + title: "Normal Mail", |
| 93 | + description: "Delivered in 5-8 days", |
| 94 | + }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + }, |
| 100 | + }; |
| 101 | + |
| 102 | + console.log("Sending message..."); |
| 103 | + const result = await client.path("/messages/notifications:send").post({ |
| 104 | + contentType: "application/json", |
| 105 | + body: { |
| 106 | + channelRegistrationId: process.env.CHANNEL_ID || "", |
| 107 | + to: [process.env.RECIPIENT_PHONE_NUMBER || ""], |
| 108 | + kind: "interactive", |
| 109 | + interactiveMessage: interactiveMessage, |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + console.log("Response: " + JSON.stringify(result, null, 2)); |
| 114 | + |
| 115 | + if (isUnexpected(result)) { |
| 116 | + throw new Error("Failed to send message"); |
| 117 | + } |
| 118 | + |
| 119 | + const response = result; |
| 120 | + response.body.receipts.forEach((receipt) => { |
| 121 | + console.log("Message sent to:" + receipt.to + " with message id:" + receipt.messageId); |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +main().catch((error) => { |
| 126 | + console.error("Encountered an error while sending message: ", error); |
| 127 | + throw error; |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +### Send an Interactive Reply Button message to a WhatsApp user |
| 132 | + |
| 133 | +The Messages SDK enables Contoso to send interactive WhatsApp messages, when initiated by a WhatsApp users. To send interactive messages: |
| 134 | +- [WhatsApp Channel ID](#set-channel-registration-id). |
| 135 | +- [Recipient Phone Number in E16 format](#set-recipient-list). |
| 136 | +- Interactive message to be sent. |
| 137 | + |
| 138 | +> [!IMPORTANT] |
| 139 | +> To send a interactive message to a WhatsApp user, the WhatsApp user must first send a message to the WhatsApp Business Account. For more information, see [Start sending messages between business and WhatsApp user](#start-sending-messages-between-a-business-and-a-whatsapp-user). |
| 140 | +
|
| 141 | +In this example, the business sends a reply button message to the user. |
| 142 | + |
| 143 | +```javascript |
| 144 | +/** |
| 145 | + * @summary Send a interactive message |
| 146 | + */ |
| 147 | + |
| 148 | +const { AzureKeyCredential } = require("@azure/core-auth"); |
| 149 | +const NotificationClient = require("@azure-rest/communication-messages").default, |
| 150 | + { isUnexpected } = require("@azure-rest/communication-messages"); |
| 151 | +// Load the .env file if it exists |
| 152 | +require("dotenv").config(); |
| 153 | + |
| 154 | +async function main() { |
| 155 | + const credential = new AzureKeyCredential(process.env.ACS_ACCESS_KEY || ""); |
| 156 | + const endpoint = process.env.ACS_URL || ""; |
| 157 | + const client = NotificationClient(endpoint, credential); |
| 158 | + |
| 159 | + const interactiveMessage = { |
| 160 | + body: { |
| 161 | + kind: "text", |
| 162 | + text: "Do you want to proceed?", |
| 163 | + }, |
| 164 | + action: { |
| 165 | + kind: "whatsAppButtonAction", |
| 166 | + content: { |
| 167 | + kind: "buttonSet", |
| 168 | + buttons: [ |
| 169 | + { |
| 170 | + id: "yes", |
| 171 | + title: "Yes", |
| 172 | + }, |
| 173 | + { |
| 174 | + id: "no", |
| 175 | + title: "No", |
| 176 | + }, |
| 177 | + ], |
| 178 | + }, |
| 179 | + }, |
| 180 | + }; |
| 181 | + |
| 182 | + console.log("Sending message..."); |
| 183 | + const result = await client.path("/messages/notifications:send").post({ |
| 184 | + contentType: "application/json", |
| 185 | + body: { |
| 186 | + channelRegistrationId: process.env.CHANNEL_ID || "", |
| 187 | + to: [process.env.RECIPIENT_PHONE_NUMBER || ""], |
| 188 | + kind: "interactive", |
| 189 | + interactiveMessage: interactiveMessage, |
| 190 | + }, |
| 191 | + }); |
| 192 | + |
| 193 | + console.log("Response: " + JSON.stringify(result, null, 2)); |
| 194 | + |
| 195 | + if (isUnexpected(result)) { |
| 196 | + throw new Error("Failed to send message"); |
| 197 | + } |
| 198 | + |
| 199 | + const response = result; |
| 200 | + response.body.receipts.forEach((receipt) => { |
| 201 | + console.log("Message sent to:" + receipt.to + " with message id:" + receipt.messageId); |
| 202 | + }); |
| 203 | +} |
| 204 | + |
| 205 | +main().catch((error) => { |
| 206 | + console.error("Encountered an error while sending message: ", error); |
| 207 | + throw error; |
| 208 | +}); |
| 209 | +``` |
| 210 | + |
| 211 | +### Send an Interactive Click-to-action Url based message to a WhatsApp user |
| 212 | + |
| 213 | +The Messages SDK enables Contoso to send interactive WhatsApp messages, when initiated by a WhatsApp users. To send interactive messages: |
| 214 | +- [WhatsApp Channel ID](#set-channel-registration-id). |
| 215 | +- [Recipient Phone Number in E16 format](#set-recipient-list). |
| 216 | +- Interactive message to be sent. |
| 217 | + |
| 218 | +> [!IMPORTANT] |
| 219 | +> To send a interactive message to a WhatsApp user, the WhatsApp user must first send a message to the WhatsApp Business Account. For more information, see [Start sending messages between business and WhatsApp user](#start-sending-messages-between-a-business-and-a-whatsapp-user). |
| 220 | +
|
| 221 | +In this example, the business sends a click to a link message to the user. |
| 222 | + |
| 223 | +```javascript |
| 224 | +/** |
| 225 | + * @summary Send a interactive message |
| 226 | + */ |
| 227 | + |
| 228 | +const { AzureKeyCredential } = require("@azure/core-auth"); |
| 229 | +const NotificationClient = require("@azure-rest/communication-messages").default, |
| 230 | + { isUnexpected } = require("@azure-rest/communication-messages"); |
| 231 | +// Load the .env file if it exists |
| 232 | +require("dotenv").config(); |
| 233 | + |
| 234 | +async function main() { |
| 235 | + const credential = new AzureKeyCredential(process.env.ACS_ACCESS_KEY || ""); |
| 236 | + const endpoint = process.env.ACS_URL || ""; |
| 237 | + const client = NotificationClient(endpoint, credential); |
| 238 | + |
| 239 | + const interactiveMessage = { |
| 240 | + body: { |
| 241 | + kind: "text", |
| 242 | + text: "The best Guardian of Galaxy", |
| 243 | + }, |
| 244 | + action: { |
| 245 | + kind: "whatsAppUrlAction", |
| 246 | + content: { |
| 247 | + kind: "url", |
| 248 | + title: "Rocket is the best!", |
| 249 | + url: "https://wallpapercave.com/wp/wp2163723.jpg", |
| 250 | + }, |
| 251 | + }, |
| 252 | + footer: { |
| 253 | + kind: "text", |
| 254 | + text: "Intergalactic News Ltd", |
| 255 | + }, |
| 256 | + }; |
| 257 | + |
| 258 | + console.log("Sending message..."); |
| 259 | + const result = await client.path("/messages/notifications:send").post({ |
| 260 | + contentType: "application/json", |
| 261 | + body: { |
| 262 | + channelRegistrationId: process.env.CHANNEL_ID || "", |
| 263 | + to: [process.env.RECIPIENT_PHONE_NUMBER || ""], |
| 264 | + kind: "interactive", |
| 265 | + interactiveMessage: interactiveMessage, |
| 266 | + }, |
| 267 | + }); |
| 268 | + |
| 269 | + console.log("Response: " + JSON.stringify(result, null, 2)); |
| 270 | + |
| 271 | + if (isUnexpected(result)) { |
| 272 | + throw new Error("Failed to send message"); |
| 273 | + } |
| 274 | + |
| 275 | + const response = result; |
| 276 | + response.body.receipts.forEach((receipt) => { |
| 277 | + console.log("Message sent to:" + receipt.to + " with message id:" + receipt.messageId); |
| 278 | + }); |
| 279 | +} |
| 280 | + |
| 281 | +main().catch((error) => { |
| 282 | + console.error("Encountered an error while sending message: ", error); |
| 283 | + throw error; |
| 284 | +}); |
| 285 | +``` |
| 286 | + |
| 287 | +## Run the code |
| 288 | +Use the node command to run the code you added to the send-messages.js file. |
| 289 | + |
| 290 | +```console |
| 291 | +node ./send-messages.js |
| 292 | +``` |
| 293 | + |
| 294 | +## Full sample code |
| 295 | + |
| 296 | +Find the finalized code for this quickstart on [GitHub](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/communication/communication-messages-rest/samples). |
0 commit comments