Skip to content

Commit

Permalink
more detailed parsing and logging of media get
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterjackson committed Jul 16, 2024
1 parent c96e9b0 commit ce7e04e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/main/java/com/meta/cp4m/message/WAMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package com.meta.cp4m.message;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.meta.cp4m.Identifier;
Expand Down Expand Up @@ -82,8 +81,8 @@ private List<ThreadState<WAMessage>> post(Context ctx, WebhookPayload payload) {
case TextWebhookMessage m -> payloadValue = new Payload.Text(m.text().body());
case ImageWebhookMessage m -> {
try {
URI url = this.getUrlFromID(m.image().id());
byte[] media = this.getMediaFromUrl(url);
GetMediaIdBody mediaDetails = this.mediaDetails(m.image().id());
byte[] media = this.getMediaFromUrl(mediaDetails.url());
payloadValue = new Payload.Image(media, m.image().mimeType());
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
Expand All @@ -92,8 +91,8 @@ private List<ThreadState<WAMessage>> post(Context ctx, WebhookPayload payload) {

case DocumentWebhookMessage m -> {
try {
URI url = this.getUrlFromID(m.document().id());
byte[] media = this.getMediaFromUrl(url);
GetMediaIdBody mediaDetails = this.mediaDetails(m.document().id());
byte[] media = this.getMediaFromUrl(mediaDetails.url());
payloadValue = new Payload.Document(media, m.document().mimeType());
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -272,22 +271,29 @@ private void markRead(Identifier phoneNumberId, String messageId) {
}
}

private URI getUrlFromID(String mediaID) throws IOException, URISyntaxException {
return Request.get(new URIBuilder(this.baseURL).appendPath(mediaID).build())
GetMediaIdBody mediaDetails(String mediaID) throws IOException, URISyntaxException {
URI getUrl = new URIBuilder(this.baseURL).appendPath(mediaID).build();
return Request.get(getUrl)
.setHeader("Authorization", "Bearer " + accessToken)
.setHeader("appsecret_proof", appSecretProof)
.execute()
.handleResponse(
response -> {
String jsonResponse = EntityUtils.toString(response.getEntity());
GetMediaIdBody parsedResponse;
try {
String jsonResponse = EntityUtils.toString(response.getEntity());
JsonNode jsonNode = MAPPER.readTree(jsonResponse);
return new URIBuilder(jsonNode.get("url").asText());
} catch (URISyntaxException e) {
parsedResponse = MAPPER.readValue(jsonResponse, GetMediaIdBody.class);
} catch (Exception e) {
LOGGER
.atError()
.addKeyValue("url", getUrl)
.addKeyValue("response", jsonResponse)
.setCause(e)
.log("Unable to parse response from media get request");
throw new RuntimeException(e);
}
})
.build();
return parsedResponse;
});
}

private byte[] getMediaFromUrl(URI url) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.meta.cp4m.message.webhook.whatsapp;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.net.URI;

public record GetMediaIdBody(
@JsonProperty("messaging_product") String messagingProduct,
@JsonProperty("mime_type") String mimeType,
@JsonProperty("url") URI url,
@JsonProperty("sha256") String sha256,
@JsonProperty("file_size") String fileSize,
@JsonProperty("id") String id) {}
23 changes: 23 additions & 0 deletions src/test/java/com/meta/cp4m/message/WAMessageHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
import com.google.common.base.Stopwatch;
import com.meta.cp4m.DummyWebServer.ReceivedRequest;
import com.meta.cp4m.Identifier;
import com.meta.cp4m.message.webhook.whatsapp.GetMediaIdBody;
import com.meta.cp4m.message.webhook.whatsapp.SendResponse;
import com.meta.cp4m.message.webhook.whatsapp.Utils;
import io.javalin.http.HandlerType;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -271,6 +275,25 @@ void validResponseWithoutContacts() throws IOException {
.isEqualTo("wamid.HBgLMTY1MDUwNzY1MjAVAgARGBI5QTNDQTVCM9Q0Q0Q2RTY3RTcA");
}

@Test
void mediaFetchValid() throws IOException, URISyntaxException {
final String webhookResponse =
"""
{
"messaging_product": "whatsapp",
"url": "https://example.com/image.jpg",
"mime_type": "image/jpeg",
"sha256": "f1234567890",
"file_size": "111",
"id": "1234567890"
}""";
harness.dummyWebServer().response(ctx -> ctx.method().equals(HandlerType.GET), webhookResponse);
harness.start();
WAMessageHandler handler = (WAMessageHandler) harness.handler();
GetMediaIdBody response = handler.mediaDetails("1234567890");
assertThat(response.url()).isEqualTo(new URI("https://example.com/image.jpg"));
}

@Test
void doesNotSendNonTextMessages() throws IOException, InterruptedException {
harness.start();
Expand Down

0 comments on commit ce7e04e

Please sign in to comment.