-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
SendInteractiveMessageExample.java
277 lines (239 loc) · 15.4 KB
/
SendInteractiveMessageExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.whatsapp.api.examples;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.whatsapp.api.TestConstants;
import com.whatsapp.api.WhatsappApiFactory;
import com.whatsapp.api.domain.messages.Action;
import com.whatsapp.api.domain.messages.Body;
import com.whatsapp.api.domain.messages.Button;
import com.whatsapp.api.domain.messages.Document;
import com.whatsapp.api.domain.messages.Footer;
import com.whatsapp.api.domain.messages.Header;
import com.whatsapp.api.domain.messages.Image;
import com.whatsapp.api.domain.messages.InteractiveMessage;
import com.whatsapp.api.domain.messages.Message.MessageBuilder;
import com.whatsapp.api.domain.messages.Product;
import com.whatsapp.api.domain.messages.Reply;
import com.whatsapp.api.domain.messages.Row;
import com.whatsapp.api.domain.messages.Section;
import com.whatsapp.api.domain.messages.Video;
import com.whatsapp.api.domain.messages.response.MessageResponse;
import com.whatsapp.api.domain.messages.type.ButtonType;
import com.whatsapp.api.domain.messages.type.HeaderType;
import com.whatsapp.api.domain.messages.type.InteractiveMessageType;
import com.whatsapp.api.impl.WhatsappBusinessCloudApi;
import static com.whatsapp.api.TestConstants.PHONE_NUMBER_1;
import static com.whatsapp.api.TestConstants.PHONE_NUMBER_ID;
public class SendInteractiveMessageExample {
public static void main(String[] args) throws JsonProcessingException {
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN);
WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
productMessage(whatsappBusinessCloudApi);
multiProductMessage(whatsappBusinessCloudApi);
buttonMessage(whatsappBusinessCloudApi);
listMessage(whatsappBusinessCloudApi);
withImageHeader(whatsappBusinessCloudApi);
withVideoHeader(whatsappBusinessCloudApi);
withDocumentHeader(whatsappBusinessCloudApi);
}
private static void multiProductMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) {
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildInteractiveMessage(InteractiveMessage.build() //
.setAction(new Action() //
.setCatalogId("6019053994849450") //
.addSection(new Section() //
.setTitle("Title 1") //
.addProductItem(new Product() //
.setProductRetailerId("product-SKU1-in-catalog")) //
.addProductItem(new Product() //
.setProductRetailerId("product-SKU2-in-catalog")) //
.addProductItem(new Product() //
.setProductRetailerId("product-SKU3-in-catalog")) //
) //
.addSection(new Section() //
.setTitle("Title 2") //
.addProductItem(new Product() //
.setProductRetailerId("product-SKU5-in-catalog"))) //
) //
.setType(InteractiveMessageType.PRODUCT_LIST) //
.setHeader(new Header() //
.setType(HeaderType.TEXT) //
.setText("Header Text")) //
.setBody(new Body() //
.setText("Body message")) //
);
MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
System.out.println(messageResponse);
}
private static void productMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) throws JsonProcessingException {
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildInteractiveMessage(InteractiveMessage.build() //
.setAction(new Action() //
.setCatalogId("600136185327014") //
.setProductRetailerId("r2d5xse158")) //
.setType(InteractiveMessageType.PRODUCT) //
.setBody(new Body() //
.setText("Body message")) //
);
System.out.println(new ObjectMapper().writeValueAsString(message));
MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
System.out.println(messageResponse);
}
private static void buttonMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) throws JsonProcessingException {
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildInteractiveMessage(InteractiveMessage.build() //
.setAction(new Action() //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278454") //
.setTitle("YES"))) //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278455") //
.setTitle("NO"))) //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278456") //
.setTitle("CHANGE")))) //
.setType(InteractiveMessageType.BUTTON) //
.setHeader(new Header()//
.setType(HeaderType.TEXT)//
.setText("Appointment confirmation.")//
).setBody(new Body() //
.setText("Would you like to confirm your appointment for tomorrow?")) //
.setFooter(new Footer().setText("Choose an option:")));
System.out.println(new ObjectMapper().writeValueAsString(message));
MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
System.out.println(messageResponse);
}
private static void withImageHeader(WhatsappBusinessCloudApi whatsappBusinessCloudApi) throws JsonProcessingException {
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildInteractiveMessage(InteractiveMessage.build() //
.setAction(new Action() //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278454") //
.setTitle("YES"))) //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278455") //
.setTitle("NO"))) //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278456") //
.setTitle("CHANGE")))) //
.setType(InteractiveMessageType.BUTTON) //
.setHeader(new Header()//
.setType(HeaderType.IMAGE)//
.setImage(new Image().setLink("https://upload.wikimedia.org/wikipedia/commons/8/87/Tamarama_Beach.jpg"))).setBody(new Body() //
.setText("Would you like to confirm your appointment for tomorrow?")) //
.setFooter(new Footer().setText("Choose an option:")));
System.out.println(new ObjectMapper().writeValueAsString(message));
MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
System.out.println(messageResponse);
}
private static void withDocumentHeader(WhatsappBusinessCloudApi whatsappBusinessCloudApi) throws JsonProcessingException {
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildInteractiveMessage(InteractiveMessage.build() //
.setAction(new Action() //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278454") //
.setTitle("YES"))) //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278455") //
.setTitle("NO"))) //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278456") //
.setTitle("CHANGE")))) //
.setType(InteractiveMessageType.BUTTON) //
.setHeader(new Header()//
.setType(HeaderType.DOCUMENT)//
.setDocument(new Document().setFileName("schedule.pdf").setLink("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"))).setBody(new Body() //
.setText("Would you like to confirm your appointment for tomorrow?")) //
.setFooter(new Footer().setText("Choose an option:")));
System.out.println(new ObjectMapper().writeValueAsString(message));
MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
System.out.println(messageResponse);
}
private static void withVideoHeader(WhatsappBusinessCloudApi whatsappBusinessCloudApi) throws JsonProcessingException {
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildInteractiveMessage(InteractiveMessage.build() //
.setAction(new Action() //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278454") //
.setTitle("YES"))) //
.addButton(new Button() //
.setType(ButtonType.REPLY).setReply(new Reply() //
.setId("1278455") //
.setTitle("NO"))) //
) //
.setType(InteractiveMessageType.BUTTON) //
.setHeader(new Header()//
.setType(HeaderType.VIDEO)//
.setVideo(new Video().setLink("https://joy1.videvo.net/videvo_files/video/free/2014-04/large_watermarked/Lonely_tree_at_Sunset_slow_motion_CCBY_NatureClip_preview.mp4"))).setBody(new Body() //
.setText("Do you like this video?")) //
.setFooter(new Footer().setText("Choose an option:")));
System.out.println(new ObjectMapper().writeValueAsString(message));
MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
System.out.println(messageResponse);
}
private static void listMessage(WhatsappBusinessCloudApi whatsappBusinessCloudApi) throws JsonProcessingException {
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildInteractiveMessage(InteractiveMessage.build() //
.setAction(new Action() //
.setButtonText("choose an option") //
.addSection(new Section() //
.setTitle("Section 1") //
.addRow(new Row() //
.setId("SECTION_1_ROW_1_ID") //
.setTitle("Title 1") //
.setDescription("SECTION_1_ROW_1_DESCRIPTION")) //
.addRow(new Row() //
.setId("SECTION_1_ROW_2_ID") //
.setTitle("Title 2") //
.setDescription("SECTION_1_ROW_2_DESCRIPTION")) //
.addRow(new Row() //
.setId("SECTION_1_ROW_3_ID") //
.setTitle("Title 3") //
.setDescription("SECTION_1_ROW_3_DESCRIPTION")) //
) //
.addSection(new Section() //
.setTitle("Section 2") //
.addRow(new Row() //
.setId("SECTION_2_ROW_1_ID") //
.setTitle("Title 1") //
.setDescription("SECTION_2_ROW_1_DESCRIPTION")) //
.addRow(new Row() //
.setId("SECTION_2_ROW_2_ID") //
.setTitle("Title 2") //
.setDescription("SECTION_2_ROW_2_DESCRIPTION")) //
.addRow(new Row() //
.setId("SECTION_2_ROW_3_ID") //
.setTitle("Title 3") //
.setDescription("SECTION_2_ROW_3_DESCRIPTION")) //
)) //
.setType(InteractiveMessageType.LIST) //
.setHeader(new Header() //
.setType(HeaderType.TEXT) //
.setText("Header Text")) //
.setBody(new Body() //
.setText("Body message")) //
.setFooter(new Footer() //
.setText("Footer Text")) //
);
System.out.println(new ObjectMapper().writeValueAsString(message));
MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
System.out.println(messageResponse);
}
}