Skip to content

Commit

Permalink
Replacing boolean check and me/messages with owning page ID and ownin…
Browse files Browse the repository at this point in the history
…g_page_id/messages
  • Loading branch information
colinmccloskey committed Nov 28, 2023
1 parent 3331910 commit 9f84d6e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
17 changes: 12 additions & 5 deletions src/main/java/com/meta/cp4m/message/FBMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class FBMessageHandler implements MessageHandler<FBMessage> {
private final String appSecret;

private final String accessToken;
private final boolean isInstagram;
private final String owningPageID;

private final Deduplicator<Identifier> messageDeduplicator = new Deduplicator<>(10_000);
private Function<Identifier, URI> baseURLFactory =
Expand All @@ -63,18 +63,25 @@ public class FBMessageHandler implements MessageHandler<FBMessage> {
}
};

public FBMessageHandler(String verifyToken, String pageAccessToken, String appSecret, boolean isInstagram) {
public FBMessageHandler(String verifyToken, String pageAccessToken, String appSecret, String owningPageID) {
this.verifyToken = verifyToken;
this.appSecret = appSecret;
this.accessToken = pageAccessToken;
this.isInstagram = isInstagram;
this.owningPageID = owningPageID;
}

public FBMessageHandler(String verifyToken, String pageAccessToken, String appSecret) {
this.verifyToken = verifyToken;
this.appSecret = appSecret;
this.accessToken = pageAccessToken;
this.owningPageID = "-1";
}

FBMessageHandler(FBMessengerConfig config) {
this.verifyToken = config.verifyToken();
this.appSecret = config.appSecret();
this.accessToken = config.pageAccessToken();
this.isInstagram = config.isInstagram();
this.owningPageID = config.owningPageID();
}

@Override
Expand Down Expand Up @@ -213,7 +220,7 @@ private void send(String message, Identifier recipient, Identifier sender) throw
try {
bodyString = MAPPER.writeValueAsString(body);
url =
new URIBuilder(baseURLFactory.apply(isInstagram ? Identifier.from("me") : sender))
new URIBuilder(baseURLFactory.apply(owningPageID.equals("-1") ? sender : Identifier.from(owningPageID)))
.addParameter("access_token", accessToken)
.build();
} catch (JsonProcessingException | URISyntaxException e) {
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/meta/cp4m/message/FBMessengerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;

import java.util.Optional;
import java.util.UUID;

public class FBMessengerConfig implements HandlerConfig {
Expand All @@ -19,14 +20,14 @@ public class FBMessengerConfig implements HandlerConfig {
private final String verifyToken;
private final String appSecret;
private final String pageAccessToken;
private final boolean isInstagram;
private final String owningPageID;

private FBMessengerConfig(
@JsonProperty("name") String name,
@JsonProperty("verify_token") String verifyToken,
@JsonProperty("app_secret") String appSecret,
@JsonProperty("page_access_token") String pageAccessToken,
@JsonProperty("is_instagram") Boolean isInstagram) {
@JsonProperty("owning_page_id") String owningPageID) {

Preconditions.checkArgument(name != null && !name.isBlank(), "name cannot be blank");
Preconditions.checkArgument(
Expand All @@ -35,18 +36,26 @@ private FBMessengerConfig(
appSecret != null && !appSecret.isBlank(), "app_secret cannot be blank");
Preconditions.checkArgument(
pageAccessToken != null && !pageAccessToken.isBlank(), "page_access_token cannot be blank");
Preconditions.checkArgument(
owningPageID != null && !owningPageID.isBlank(), "owning_page_id cannot be blank");

this.name = name;
this.verifyToken = verifyToken;
this.appSecret = appSecret;
this.pageAccessToken = pageAccessToken;
this.isInstagram = isInstagram != null ? isInstagram : false;
this.owningPageID = owningPageID;
}

public static FBMessengerConfig of(String verifyToken, String appSecret, String pageAccessToken, String owningPageID) {
// human readability of the name only matters when it's coming from a config
return new FBMessengerConfig(
UUID.randomUUID().toString(), verifyToken, appSecret, pageAccessToken, owningPageID);
}

public static FBMessengerConfig of(String verifyToken, String appSecret, String pageAccessToken, boolean isInstagram) {
public static FBMessengerConfig of(String verifyToken, String appSecret, String pageAccessToken) {
// human readability of the name only matters when it's coming from a config
return new FBMessengerConfig(
UUID.randomUUID().toString(), verifyToken, appSecret, pageAccessToken, isInstagram);
UUID.randomUUID().toString(), verifyToken, appSecret, pageAccessToken, "-1");
}

@Override
Expand All @@ -71,7 +80,7 @@ public String pageAccessToken() {
return pageAccessToken;
}

public boolean isInstagram() {
return isInstagram;
public String owningPageID() {
return owningPageID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void inPipeline() throws IOException, URISyntaxException, InterruptedException {
metaRequests.put(
new OutboundRequest(ctx.body(), ctx.headerMap(), ctx.queryParamMap())));
FBMessageHandler handler =
new FBMessageHandler(verifyToken, accessToken, appSecret, false)
new FBMessageHandler(verifyToken, accessToken, appSecret)
.baseURLFactory(ignored -> messageReceiver);

String apiKey = "api key";
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/meta/cp4m/llm/OpenAIPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void inPipeline() throws IOException, URISyntaxException, InterruptedException {
metaRequests.put(
new OutboundRequest(ctx.body(), ctx.headerMap(), ctx.queryParamMap())));
FBMessageHandler handler =
new FBMessageHandler(verifyToken, accessToken, appSecret, false)
new FBMessageHandler(verifyToken, accessToken, appSecret)
.baseURLFactory(ignored -> messageReceiver);

String apiKey = "api key";
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/meta/cp4m/message/FBMessageHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ void validation() throws IOException, URISyntaxException {
Service<FBMessage> service =
new Service<>(
MemoryStoreConfig.of(1, 1).toStore(),
new FBMessageHandler("0", token, "dummy", false),
new FBMessageHandler("0", token, "dummy"),
new DummyLLMPlugin("this is a dummy message"),
"/testfbmessage");
final ServicesRunner runner = ServicesRunner.newInstance().service(service).port(0);
Expand Down Expand Up @@ -308,7 +308,7 @@ void invalidMessage(
Identifier pageId = Identifier.from(106195825075770L);
String token = "243af3c6-9994-4869-ae13-ad61a38323f5"; // this is fake don't worry
String secret = "f74a638462f975e9eadfcbb84e4aa06b"; // it's been rolled don't worry
FBMessageHandler messageHandler = new FBMessageHandler("0", token, secret, false);
FBMessageHandler messageHandler = new FBMessageHandler("0", token, secret);
DummyLLMPlugin llmHandler = new DummyLLMPlugin("this is a dummy message");
MemoryStore<FBMessage> memoryStore = MemoryStoreConfig.of(1, 1).toStore();
Service<FBMessage> service = new Service<>(memoryStore, messageHandler, llmHandler, path);
Expand Down Expand Up @@ -368,7 +368,7 @@ void chunkingHappens() throws IOException {
String token = "243af3c6-9994-4869-ae13-ad61a38323f5"; // this is fake don't worry
String secret = "f74a638462f975e9eadfcbb84e4aa06b"; // it's been rolled don't worry
FBMessageHandler messageHandler =
new FBMessageHandler("0", token, secret, false).baseURLFactory(testURLFactoryFactory(pageId));
new FBMessageHandler("0", token, secret).baseURLFactory(testURLFactoryFactory(pageId));

String bigText =
Stream.generate(() -> "0123456789.").limit(300).collect(Collectors.joining(" "));
Expand Down

0 comments on commit 9f84d6e

Please sign in to comment.