Skip to content

Commit

Permalink
Added Location message.
Browse files Browse the repository at this point in the history
  • Loading branch information
PratikLaweb committed Mar 31, 2023
1 parent dd8b450 commit ea2274b
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/whatsapp/api/domain/messages/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class Message {
@JsonProperty("reaction")
private ReactionMessage reactionMessage;

@JsonProperty("location")
private LocationMessage locationMessage;


private Message() {
}
Expand Down Expand Up @@ -255,6 +258,20 @@ public Message buildReactionMessage(ReactionMessage reactionMessage) {

}

/**
* Build location message
*
* @param locationMessage the location message
* @return the message
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#location-messages">API documentation</a>
*/
public Message buildLocationMessage(LocationMessage locationMessage) {
var message = new Message(to, MessageType.LOCATION);
message.locationMessage = locationMessage;
return message;

}


}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}


}

12 changes: 12 additions & 0 deletions src/test/resources/expected/message/expectedMessage13.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit ea2274b

Please sign in to comment.