-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendMessageWithPreviewExample.java
More file actions
75 lines (65 loc) · 2.73 KB
/
Copy pathSendMessageWithPreviewExample.java
File metadata and controls
75 lines (65 loc) · 2.73 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
package com.greenapi.client.examples;
import com.greenapi.client.pkg.api.GreenApi;
import com.greenapi.client.pkg.models.request.CustomPreview;
import com.greenapi.client.pkg.models.request.OutgoingMessage;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.client.RestTemplate;
import java.util.Objects;
@Log4j2
public class SendMessageWithPreviewExample {
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 SendMessageWithPreviewExample();
example.sendWithTypingExample(greenApi);
example.sendWithCustomPreviewExample(greenApi);
}
/**
* Sends a text message with a 4-second typing indicator shown to the recipient beforehand.
*/
private void sendWithTypingExample(GreenApi greenApi) {
var response = greenApi.sending.sendMessage(
OutgoingMessage.builder()
.chatId("11001234567@c.us")
.message("Hello! I was typing this for a while...")
.typingTime(4000)
.build());
if (response.getStatusCode().is2xxSuccessful()) {
log.info("Message sent with typing indicator, id: "
+ Objects.requireNonNull(response.getBody()).getIdMessage());
} else {
log.warn("Message sending failed, status: " + response.getStatusCode());
}
}
/**
* Sends a text message with a custom link preview (title, description, thumbnail).
* The preview overrides the default one auto-generated from the link.
*/
private void sendWithCustomPreviewExample(GreenApi greenApi) {
var customPreview = CustomPreview.builder()
.title("GREEN-API Documentation")
.description("Full WhatsApp API reference")
.link("green-api.com")
.urlFile("https://green-api.com/logo.png")
.build();
var response = greenApi.sending.sendMessage(
OutgoingMessage.builder()
.chatId("11001234567@c.us")
.message("Check out the docs: https://green-api.com/docs/api/")
.linkPreview(true)
.typePreview("large")
.customPreview(customPreview)
.build());
if (response.getStatusCode().is2xxSuccessful()) {
log.info("Message with custom preview sent, id: "
+ Objects.requireNonNull(response.getBody()).getIdMessage());
} else {
log.warn("Message sending failed, status: " + response.getStatusCode());
}
}
}