|
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: 01/28/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 | 14 | ## Prerequisites
|
15 | 15 |
|
16 |
| -## Setting up |
| 16 | +- [WhatsApp Business Account registered with your Azure Communication Services resource](../../connect-whatsapp-business-account.md). |
| 17 | +- Active WhatsApp phone number to receive messages. |
| 18 | +- [Java Development Kit (JDK)](/java/azure/jdk/) version 8 or later. |
| 19 | +- [Apache Maven](https://maven.apache.org/download.cgi). |
| 20 | + |
| 21 | +## Set up environment |
| 22 | + |
| 23 | +To set up an environment for sending messages, complete the steps in the following sections. |
17 | 24 |
|
18 | 25 | [!INCLUDE [Setting up for Java Application](../java-application-setup.md)]
|
| 26 | + |
| 27 | +## Code examples |
| 28 | + |
| 29 | +Follow these steps to add required code snippets to the main function of your `App.java` file. |
| 30 | +- [Start sending messages between a business and a WhatsApp user](#start-sending-messages-between-a-business-and-a-whatsapp-user). |
| 31 | +- [Authenticate the client](#authenticate-the-client). |
| 32 | +- [Set channel registration ID](#set-channel-registration-id). |
| 33 | +- [Set recipient list](#set-recipient-list). |
| 34 | +- [Send an Interactive List options message to a WhatsApp user](#send-an-interactive-reply-button-message-to-a-whatsapp-user). |
| 35 | +- [Send an Interactive Reply Button message to a WhatsApp user](#send-an-interactive-reply-button-message-to-a-whatsapp-user). |
| 36 | +- [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) |
| 37 | + |
| 38 | +[!INCLUDE [Common setting for using Advanced Messages SDK](../common-setting-java.md)] |
| 39 | + |
| 40 | +### Send an Interactive List options message to a WhatsApp user |
| 41 | + |
| 42 | +The Messages SDK enables Contoso to send interactive WhatsApp messages, when initiated by a WhatsApp users. To send interactive messages: |
| 43 | +- [WhatsApp Channel ID](#set-channel-registration-id). |
| 44 | +- [Recipient Phone Number in E16 format](#set-recipient-list). |
| 45 | +- Interactive message to be sent. |
| 46 | + |
| 47 | +> [!IMPORTANT] |
| 48 | +> 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). |
| 49 | +
|
| 50 | +In this example, the business sends an interactive shipping options message to the user. |
| 51 | + |
| 52 | +```java |
| 53 | + |
| 54 | +// Create Express Shipping options group |
| 55 | +List<ActionGroupItem> group1 = new ArrayList<>(); |
| 56 | +group1.add(new ActionGroupItem("priority_express", "Priority Mail Express", "Delivered on same day!")); |
| 57 | +group1.add(new ActionGroupItem("priority_mail", "Priority Mail", "Delivered in 1-2 days")); |
| 58 | + |
| 59 | +// Create Normal Shipping options group |
| 60 | +List<ActionGroupItem> group2 = new ArrayList<>(); |
| 61 | +group2.add(new ActionGroupItem("usps_ground_advantage", "USPS Ground Advantage", "Delivered in 2-5 days")); |
| 62 | +group2.add(new ActionGroupItem("normal_mail", "Normal Mail", "Delivered in 5-8 days")); |
| 63 | + |
| 64 | +// Add Shipping options |
| 65 | +List<ActionGroup> options = new ArrayList<>(); |
| 66 | +options.add(new ActionGroup("Express Delivery", group1)); |
| 67 | +options.add(new ActionGroup("Normal Delivery", group2)); |
| 68 | +ActionGroupContent actionGroupContent = new ActionGroupContent("Shipping Options", options); |
| 69 | + |
| 70 | +// Build interactive message with body, header (optional), footer (optional) |
| 71 | +InteractiveMessage interactiveMessage = new InteractiveMessage( |
| 72 | + new TextMessageContent("Which shipping option do you want?"), new WhatsAppListActionBindings(actionGroupContent)); |
| 73 | +interactiveMessage.setFooter(new TextMessageContent("Logistic Ltd")); |
| 74 | +interactiveMessage.setHeader(new TextMessageContent("Shipping Options")); |
| 75 | + |
| 76 | +InteractiveNotificationContent interactiveMessageContent new InteractiveNotificationContent("<CHANNEL_ID>", recipients, interactiveMessage); |
| 77 | + |
| 78 | +// Send a interactive message |
| 79 | +SendMessageResult textMessageResult = notificationClient.send(interactiveMessageContent); |
| 80 | + |
| 81 | +// Process result |
| 82 | +for (MessageReceipt messageReceipt : textMessageResult.getReceipts()) { |
| 83 | + System.out.println("Message sent to:" + messageReceipt.getTo() + " and message id:" + messageReceipt.getMessageId()); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Send an Interactive Reply Button message to a WhatsApp user |
| 88 | + |
| 89 | +The Messages SDK enables Contoso to send interactive WhatsApp messages, when initiated by a WhatsApp users. To send interactive messages: |
| 90 | +- [WhatsApp Channel ID](#set-channel-registration-id). |
| 91 | +- [Recipient Phone Number in E16 format](#set-recipient-list). |
| 92 | +- Interactive message to be sent. |
| 93 | + |
| 94 | +> [!IMPORTANT] |
| 95 | +> 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). |
| 96 | +
|
| 97 | +In this example, the business sends a interactive reply button message to the user. |
| 98 | + |
| 99 | +```java |
| 100 | +// Assemble interactive reply button |
| 101 | +List<ButtonContent> buttonActions = new ArrayList<>(); |
| 102 | +buttonActions.add(new ButtonContent("no", "No")); |
| 103 | +buttonActions.add(new ButtonContent("yes", "Yes")); |
| 104 | +ButtonSetContent buttonSet = new ButtonSetContent(buttonActions); |
| 105 | +InteractiveMessage interactiveMessage = new InteractiveMessage(new TextMessageContent("Do you want to proceed?"), new WhatsAppButtonActionBindings(buttonSet)); |
| 106 | + |
| 107 | +InteractiveNotificationContent interactiveMessageContent new InteractiveNotificationContent("<CHANNEL_ID>", recipients, interactiveMessage); |
| 108 | + |
| 109 | +// Send a interactive message |
| 110 | +SendMessageResult textMessageResult = notificationClient.send(interactiveMessageContent); |
| 111 | + |
| 112 | +// Process result |
| 113 | +for (MessageReceipt messageReceipt : textMessageResult.getReceipts()) { |
| 114 | + System.out.println("Message sent to:" + messageReceipt.getTo() + " and message id:" + messageReceipt.getMessageId()); |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### Send an Interactive Click-to-action Url based message to a WhatsApp user |
| 119 | + |
| 120 | +The Messages SDK enables Contoso to send interactive WhatsApp messages, when initiated by a WhatsApp users. To send interactive messages: |
| 121 | +- [WhatsApp Channel ID](#set-channel-registration-id). |
| 122 | +- [Recipient Phone Number in E16 format](#set-recipient-list). |
| 123 | +- Interactive message to be sent. |
| 124 | + |
| 125 | +> [!IMPORTANT] |
| 126 | +> 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). |
| 127 | +
|
| 128 | +In this example, the business sends a click to a link message to the user. |
| 129 | + |
| 130 | +```java |
| 131 | +LinkContent urlAction = new LinkContent("Click here to find out", "https://wallpapercave.com/wp/wp2163723.jpg"); |
| 132 | +InteractiveMessage interactiveMessage = new InteractiveMessage( |
| 133 | + new TextMessageContent("The best Guardian of Galaxy"), new WhatsAppUrlActionBindings(urlAction)); |
| 134 | +interactiveMessage.setFooter(new TextMessageContent("Intergalactic New Ltd")); |
| 135 | + |
| 136 | +InteractiveNotificationContent interactiveMessageContent new InteractiveNotificationContent("<CHANNEL_ID>", recipients, interactiveMessage); |
| 137 | + |
| 138 | +// Send a interactive message |
| 139 | +SendMessageResult textMessageResult = notificationClient.send(interactiveMessageContent); |
| 140 | + |
| 141 | +// Process result |
| 142 | +for (MessageReceipt messageReceipt : textMessageResult.getReceipts()) { |
| 143 | + System.out.println("Message sent to:" + messageReceipt.getTo() + " and message id:" + messageReceipt.getMessageId()); |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | + |
| 148 | +### Run the code |
| 149 | + |
| 150 | +1. Open to the directory that contains the `pom.xml` file and compile the project using the `mvn` command. |
| 151 | + |
| 152 | + ```console |
| 153 | + mvn compile |
| 154 | + ``` |
| 155 | + |
| 156 | +1. Run the app by executing the following `mvn` command. |
| 157 | + |
| 158 | + ```console |
| 159 | + mvn exec:java -D"exec.mainClass"="com.communication.quickstart.App" -D"exec.cleanupDaemonThreads"="false" |
| 160 | + ``` |
| 161 | + |
| 162 | +## Full sample code |
| 163 | + |
| 164 | +Find the finalized code for this quickstart on [GitHub](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/communication/azure-communication-messages/src/samples/java/com/azure/communication/messages). |
0 commit comments