-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendInteractiveButtonsExample.java
More file actions
97 lines (86 loc) · 3.38 KB
/
Copy pathSendInteractiveButtonsExample.java
File metadata and controls
97 lines (86 loc) · 3.38 KB
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
package com.greenapi.client.examples;
import com.greenapi.client.pkg.api.GreenApi;
import com.greenapi.client.pkg.models.request.InteractiveButton;
import com.greenapi.client.pkg.models.request.InteractiveReplyButton;
import com.greenapi.client.pkg.models.request.OutgoingInteractiveButtons;
import com.greenapi.client.pkg.models.request.OutgoingInteractiveButtonsReply;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Objects;
@Log4j2
public class SendInteractiveButtonsExample {
public static void main(String[] args) {
var restTemplate = new RestTemplate();
var greenApi = new GreenApi(
restTemplate,
"https://media.green-api.com",
"https://api.green-api.com",
"YOUR_INSTANCE_ID",
"YOUR_TOKEN");
var example = new SendInteractiveButtonsExample();
example.sendInteractiveButtonsExample(greenApi);
example.sendInteractiveButtonsReplyExample(greenApi);
}
/**
* Sends a message with action buttons (url, call, copy).
* Maximum 3 buttons per message.
*/
private void sendInteractiveButtonsExample(GreenApi greenApi) {
var buttons = List.of(
InteractiveButton.builder()
.type("url")
.buttonId("1")
.buttonText("Visit site")
.url("https://green-api.com")
.build(),
InteractiveButton.builder()
.type("call")
.buttonId("2")
.buttonText("Call us")
.phoneNumber("11001234567")
.build()
);
var response = greenApi.sending.sendInteractiveButtons(
OutgoingInteractiveButtons.builder()
.chatId("11001234567@c.us")
.header("Support")
.body("Choose an action")
.footer("GREEN-API")
.buttons(buttons)
.build());
if (response.getStatusCode().is2xxSuccessful()) {
log.info("Interactive buttons sent, id: " + Objects.requireNonNull(response.getBody()).getIdMessage());
} else {
log.warn("Sending failed, status: " + response.getStatusCode());
}
}
/**
* Sends a message with reply buttons that send their text back into the chat when clicked.
* Maximum 3 buttons per message. Beta feature.
*/
private void sendInteractiveButtonsReplyExample(GreenApi greenApi) {
var buttons = List.of(
InteractiveReplyButton.builder()
.buttonId("1")
.buttonText("Yes")
.build(),
InteractiveReplyButton.builder()
.buttonId("2")
.buttonText("No")
.build()
);
var response = greenApi.sending.sendInteractiveButtonsReply(
OutgoingInteractiveButtonsReply.builder()
.chatId("11001234567@c.us")
.body("Confirm your order?")
.footer("Order #12345")
.buttons(buttons)
.build());
if (response.getStatusCode().is2xxSuccessful()) {
log.info("Reply buttons sent, id: " + Objects.requireNonNull(response.getBody()).getIdMessage());
} else {
log.warn("Sending failed, status: " + response.getStatusCode());
}
}
}