From ea2274b7503c907e24c44e4fb62c92c18c0401a3 Mon Sep 17 00:00:00 2001 From: Pratik Soni <100229748+PratikLaweb@users.noreply.github.com> Date: Thu, 30 Mar 2023 21:49:35 -0400 Subject: [PATCH] Added Location message. --- .../api/domain/messages/LocationMessage.java | 98 +++++++++++++++++++ .../whatsapp/api/domain/messages/Message.java | 17 ++++ .../examples/SendLocationMessageExample.java | 34 +++++++ .../impl/WhatsappBusinessCloudApiTest.java | 27 +++++ .../expected/message/expectedMessage13.json | 12 +++ 5 files changed, 188 insertions(+) create mode 100644 src/main/java/com/whatsapp/api/domain/messages/LocationMessage.java create mode 100644 src/test/java/com/whatsapp/api/examples/SendLocationMessageExample.java create mode 100644 src/test/resources/expected/message/expectedMessage13.json diff --git a/src/main/java/com/whatsapp/api/domain/messages/LocationMessage.java b/src/main/java/com/whatsapp/api/domain/messages/LocationMessage.java new file mode 100644 index 000000000..24141eb24 --- /dev/null +++ b/src/main/java/com/whatsapp/api/domain/messages/LocationMessage.java @@ -0,0 +1,98 @@ +package com.whatsapp.api.domain.messages; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + + +/** + * The type Template message. + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +public class LocationMessage { + + @JsonProperty("longitude") + private String longitude; + @JsonProperty("latitude") + private String latitude; + @JsonProperty("name") + private String name; + @JsonProperty("address") + private String address; + + /** + * Gets name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Sets name. + * + * @param name the name + * @return the LocationMessage + */ + public LocationMessage setName(String name) { + this.name = name; + return this; + } + + /** + * Gets longitude. + * @return the longitude + */ + public String getLongitude() { + return longitude; + } + + /** + * Sets longitude. + * + * @param longitude + * @return LocationMessage + */ + public LocationMessage setLongitude(String longitude) { + this.longitude = longitude; + return this; + } + + /** + * Gets latitude. + * @return the latitude + */ + public String getLatitude() { + return latitude; + } + + /** + * Sets latitude. + * + * @param latitude + * @return LocationMessage + */ + public LocationMessage setLatitude(String latitude) { + this.latitude = latitude; + return this; + } + + /** + * Gets address. + * @return the address + */ + public String getAddress() { + return address; + } + + /** + * Sets address. + * + * @param address + * @return LocationMessage + */ + public LocationMessage setAddress(String address) { + this.address = address; + return this; + } +} \ No newline at end of file diff --git a/src/main/java/com/whatsapp/api/domain/messages/Message.java b/src/main/java/com/whatsapp/api/domain/messages/Message.java index ed0831038..112c0a8f3 100644 --- a/src/main/java/com/whatsapp/api/domain/messages/Message.java +++ b/src/main/java/com/whatsapp/api/domain/messages/Message.java @@ -51,6 +51,9 @@ public class Message { @JsonProperty("reaction") private ReactionMessage reactionMessage; + @JsonProperty("location") + private LocationMessage locationMessage; + private Message() { } @@ -255,6 +258,20 @@ public Message buildReactionMessage(ReactionMessage reactionMessage) { } + /** + * Build location message + * + * @param locationMessage the location message + * @return the message + * @see API documentation + */ + public Message buildLocationMessage(LocationMessage locationMessage) { + var message = new Message(to, MessageType.LOCATION); + message.locationMessage = locationMessage; + return message; + + } + } diff --git a/src/test/java/com/whatsapp/api/examples/SendLocationMessageExample.java b/src/test/java/com/whatsapp/api/examples/SendLocationMessageExample.java new file mode 100644 index 000000000..e3eb2418b --- /dev/null +++ b/src/test/java/com/whatsapp/api/examples/SendLocationMessageExample.java @@ -0,0 +1,34 @@ +package com.whatsapp.api.examples; + +import static com.whatsapp.api.TestConstants.PHONE_NUMBER_1; +import static com.whatsapp.api.TestConstants.PHONE_NUMBER_ID; +import static com.whatsapp.api.TestConstants.TOKEN; + +import com.whatsapp.api.WhatsappApiFactory; +import com.whatsapp.api.domain.messages.LocationMessage; +import com.whatsapp.api.domain.messages.Message.MessageBuilder; +import com.whatsapp.api.domain.messages.response.MessageResponse; +import com.whatsapp.api.impl.WhatsappBusinessCloudApi; + +public class SendLocationMessageExample { + + public static void main(String[] args) { + + WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN); + + WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi(); + var locationMessage = new LocationMessage()// + .setLongitude("38.819830") + .setLatitude("-77.151700"); + + var message = MessageBuilder.builder()// + .setTo(PHONE_NUMBER_1)// + .buildLocationMessage(locationMessage); + + MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); + + System.out.println(messageResponse); + + } + +} diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java index dd19fa1e7..61cb400a5 100644 --- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java +++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java @@ -29,6 +29,7 @@ import com.whatsapp.api.domain.messages.ImageParameter; import com.whatsapp.api.domain.messages.InteractiveMessage; import com.whatsapp.api.domain.messages.Language; +import com.whatsapp.api.domain.messages.LocationMessage; import com.whatsapp.api.domain.messages.Message.MessageBuilder; import com.whatsapp.api.domain.messages.Name; import com.whatsapp.api.domain.messages.Org; @@ -970,6 +971,32 @@ void testDeleteMedia() throws IOException, URISyntaxException, InterruptedExcept } + @Test + void testLocationMessage() throws IOException, URISyntaxException, InterruptedException, JSONException { + mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + + var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage13.json"); + + var message = MessageBuilder.builder()// + .setTo(PHONE_NUMBER_1)// + .buildLocationMessage(new LocationMessage()// + .setLatitude("39.284064") + .setLongitude("-84.265742") + .setName("Loveland Castle Museum") + .setAddress("12025 Shore Dr, Loveland, OH 45140")); + + + var response = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); + + RecordedRequest recordedRequest = mockWebServer.takeRequest(); + Assertions.assertEquals("POST", recordedRequest.getMethod()); + Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath()); + + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); + + Assertions.assertEquals("wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww", response.messages().get(0).id()); + } + } diff --git a/src/test/resources/expected/message/expectedMessage13.json b/src/test/resources/expected/message/expectedMessage13.json new file mode 100644 index 000000000..d6987b264 --- /dev/null +++ b/src/test/resources/expected/message/expectedMessage13.json @@ -0,0 +1,12 @@ +{ + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "location", + "location": { + "longitude": "-84.265742", + "latitude": "39.284064", + "name": "Loveland Castle Museum", + "address": "12025 Shore Dr, Loveland, OH 45140" + } +} \ No newline at end of file