From 4b69153b48535aba8905518a0c7ad7b282ca27a0 Mon Sep 17 00:00:00 2001 From: Hunter Jackson Date: Wed, 20 Dec 2023 18:03:02 -0500 Subject: [PATCH] optionally get access token from fb message --- .../java/com/meta/cp4m/message/FBMessage.java | 17 +++++++++++++++-- .../com/meta/cp4m/message/FBMessageHandler.java | 6 ++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/meta/cp4m/message/FBMessage.java b/src/main/java/com/meta/cp4m/message/FBMessage.java index 8846130..618d7b4 100644 --- a/src/main/java/com/meta/cp4m/message/FBMessage.java +++ b/src/main/java/com/meta/cp4m/message/FBMessage.java @@ -10,6 +10,7 @@ import com.meta.cp4m.Identifier; import java.time.Instant; +import org.checkerframework.checker.nullness.qual.Nullable; public record FBMessage( Instant timestamp, @@ -17,5 +18,17 @@ public record FBMessage( Identifier senderId, Identifier recipientId, String message, - Role role) - implements Message {} + Role role, + @Nullable String accessToken) + implements Message { + + public FBMessage( + Instant timestamp, + Identifier instanceId, + Identifier senderId, + Identifier recipientId, + String message, + Role role) { + this(timestamp, instanceId, senderId, recipientId, message, role, null); + } +} diff --git a/src/main/java/com/meta/cp4m/message/FBMessageHandler.java b/src/main/java/com/meta/cp4m/message/FBMessageHandler.java index a84625b..6a4369c 100644 --- a/src/main/java/com/meta/cp4m/message/FBMessageHandler.java +++ b/src/main/java/com/meta/cp4m/message/FBMessageHandler.java @@ -158,12 +158,14 @@ private List postHandler(Context ctx, JsonNode body) { @Override public void respond(FBMessage message) throws IOException { List chunkedText = CHUNKER.chunks(message.message()).toList(); + String accessToken = message.accessToken() == null ? this.accessToken : message.accessToken(); for (String text : chunkedText) { - send(text, message.recipientId(), message.senderId()); + send(text, message.recipientId(), message.senderId(), accessToken); } } - private void send(String message, Identifier recipient, Identifier sender) throws IOException { + private void send(String message, Identifier recipient, Identifier sender, String accessToken) + throws IOException { URI url; ObjectNode body = MAPPER.createObjectNode(); body.put("messaging_type", "RESPONSE").putObject("recipient").put("id", recipient.toString());