-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookHandlerExample.java
More file actions
142 lines (110 loc) · 7.1 KB
/
Copy pathWebhookHandlerExample.java
File metadata and controls
142 lines (110 loc) · 7.1 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
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
package com.greenapi.server.examples;
import com.greenapi.client.pkg.api.webhook.WebhookHandler;
import com.greenapi.client.pkg.models.notifications.*;
import org.springframework.stereotype.Component;
import com.greenapi.client.pkg.models.notifications.messages.EditedMessage;
import com.greenapi.client.pkg.models.notifications.messages.InteractiveButtonsMessage;
import com.greenapi.client.pkg.models.notifications.messages.InteractiveButtonsReplyMessage;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
@Component(value = "whatsappWebhookHandler")
@Log4j2
class WebhookHandlerExample implements WebhookHandler {
@SneakyThrows
@Override
public void handle(Notification notification) {
var body = notification.getBody();
if (body instanceof TextMessageWebhook textMessage) {
var text = textMessage.getMessageData().getTextMessageData().getTextMessage();
log.info("Incoming text message from {}: {}", textMessage.getSenderData().getSender(), text);
} else if (body instanceof ExtendedTextMessageWebhook extMessage) {
var text = extMessage.getMessageData().getExtendedTextMessageData().getText();
log.info("Incoming extended text message: {}", text);
} else if (body instanceof FileMessageWebhook fileMessage) {
var fileData = fileMessage.getMessageData().getFileMessageData();
log.info("Incoming file: {} ({})", fileData.getFileName(), fileData.getMimeType());
} else if (body instanceof LocationMessageWebhook locationMessage) {
var loc = locationMessage.getMessageData().getLocationMessageData();
log.info("Incoming location: lat={}, lon={}", loc.getLatitude(), loc.getLongitude());
} else if (body instanceof ContactMessageWebhook contactMessage) {
var contact = contactMessage.getMessageData().getContactMessageData();
log.info("Incoming contact: {}", contact.getDisplayName());
} else if (body instanceof ContactsArrayMessageWebhook contactsArray) {
var contacts = contactsArray.getMessageData().getMessageData().getContacts();
log.info("Incoming contacts array, count: {}", contacts.size());
} else if (body instanceof ReactionMessageWebhook reactionMessage) {
var reaction = reactionMessage.getMessageData().getExtendedTextMessageData().getText();
log.info("Incoming reaction: {}", reaction);
} else if (body instanceof StickerMessageWebhook stickerMessage) {
var sticker = stickerMessage.getMessageData().getFileMessageData();
log.info("Incoming sticker: animated={}", sticker.getIsAnimated());
} else if (body instanceof PollMessageWebhook pollMessage) {
var poll = pollMessage.getMessageData().getPollMessageData();
log.info("Incoming poll '{}' with {} options", poll.getName(), poll.getOptions().size());
} else if (body instanceof PollUpdateMessageWebhook pollUpdate) {
var update = pollUpdate.getMessageData().getPollMessageData();
log.info("Poll update '{}': {} vote options", update.getName(), update.getVotes().size());
} else if (body instanceof GroupInviteMessageWebhook groupInvite) {
var invite = groupInvite.getMessageData();
log.info("Incoming group invite to: {}", invite.getGroupName());
} else if (body instanceof EditedMessageWebhook editedMessage) {
EditedMessage data = editedMessage.getMessageData();
log.info("Message edited, new text: '{}', original id: {}",
data.getEditedMessageData().getTextMessage(),
data.getEditedMessageData().getStanzaId());
} else if (body instanceof DeletedMessageWebhook deletedMessage) {
var deletedId = deletedMessage.getMessageData().getDeletedMessageData().getStanzaId();
log.info("Message deleted, original id: {}", deletedId);
} else if (body instanceof InteractiveButtonsMessageWebhook interactiveButtons) {
InteractiveButtonsMessage data = interactiveButtons.getMessageData();
var buttons = data.getInteractiveButtons();
log.info("Incoming interactive buttons message: '{}', buttons count: {}",
buttons.getContentText(),
buttons.getButtons().size());
} else if (body instanceof InteractiveButtonsReplyMessageWebhook interactiveReply) {
InteractiveButtonsReplyMessage data = interactiveReply.getMessageData();
var reply = data.getTemplateButtonReplyMessage();
log.info("Interactive button selected: id='{}', text='{}'",
reply.getSelectedId(),
reply.getSelectedDisplayText());
} else if (body instanceof ButtonsMessageWebhook buttonsMessage) {
log.info("Incoming buttons message");
} else if (body instanceof ButtonsResponseMessageWebhook buttonsResponse) {
log.info("Buttons response received from: {}", buttonsResponse.getSenderData().getSender());
} else if (body instanceof ListMessageWebhook listMessage) {
log.info("Incoming list message");
} else if (body instanceof ListResponseMessageWebhook listResponse) {
log.info("List item selected");
} else if (body instanceof TemplateMessageWebhook templateMessage) {
log.info("Incoming template message");
} else if (body instanceof TemplateButtonsReplyMessageWebhook templateReply) {
log.info("Template button selected: {}", templateReply.getMessageData().getSelectedDisplayText());
} else if (body instanceof QuotedMessageWebhook quotedMessage) {
log.info("Incoming quoted message");
} else if (body instanceof OutgoingMessageStatus outgoingStatus) {
log.info("Outgoing message status: {} -> {}", outgoingStatus.getIdMessage(), outgoingStatus.getStatus());
} else if (body instanceof IncomingCall incomingCall) {
log.info("Incoming call from: {}, status: {}, isVideo: {}",
incomingCall.getFrom(),
incomingCall.getStatus(),
incomingCall.getIsVideo());
} else if (body instanceof OutgoingCall outgoingCall) {
log.info("Outgoing call to: {}, status: {}, duration: {}s",
outgoingCall.getFrom(),
outgoingCall.getStatus(),
outgoingCall.getDuration());
} else if (body instanceof QuotaExceeded quotaExceeded) {
var quota = quotaExceeded.getQuotaData();
log.warn("Quota exceeded! Used: {}/{}, status: {}",
quota.getUsed(),
quota.getTotal(),
quota.getStatus());
} else if (body instanceof StateInstanceChanged stateChanged) {
log.info("Instance status changed: {}", stateChanged.getStatusInstance());
} else if (body instanceof StatusInstanceChanged statusChanged) {
log.info("Instance state changed: {}", statusChanged.getStateInstance());
} else {
log.warn("Unhandled notification type: {}", body != null ? body.getTypeWebhook() : "null");
}
}
}