From ad7cdae523b53bcf7a37d2f2d52a2826efeb9b41 Mon Sep 17 00:00:00 2001 From: Mauricio Binda da Costa Date: Sat, 25 Mar 2023 12:53:26 -0300 Subject: [PATCH] fixes #57 --- .../api/domain/messages/DateTime.java | 191 ++++++++++++++++++ .../domain/messages/DateTimeParameter.java | 176 ++-------------- .../domain/messages/type/CalendarType.java | 30 +++ .../SendTemplateButtonMessage2Example.java | 73 +++++++ .../impl/WhatsappBusinessCloudApiTest.java | 56 +++++ .../expected/message/expectedMessage4.json | 77 +++++++ 6 files changed, 445 insertions(+), 158 deletions(-) create mode 100644 src/main/java/com/whatsapp/api/domain/messages/DateTime.java create mode 100644 src/main/java/com/whatsapp/api/domain/messages/type/CalendarType.java create mode 100644 src/test/java/com/whatsapp/api/examples/SendTemplateButtonMessage2Example.java create mode 100644 src/test/resources/expected/message/expectedMessage4.json diff --git a/src/main/java/com/whatsapp/api/domain/messages/DateTime.java b/src/main/java/com/whatsapp/api/domain/messages/DateTime.java new file mode 100644 index 000000000..25a03a853 --- /dev/null +++ b/src/main/java/com/whatsapp/api/domain/messages/DateTime.java @@ -0,0 +1,191 @@ +package com.whatsapp.api.domain.messages; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.whatsapp.api.domain.messages.type.CalendarType; + +/** + * The type Date time. + */ +@JsonInclude(Include.NON_NULL) +public class DateTime { + + @JsonProperty("fallback_value") + private String fallbackValue; + @JsonProperty("calendar") + private CalendarType calendar; + @JsonProperty("month") + private Integer month; + @JsonProperty("hour") + private Integer hour; + @JsonProperty("year") + private Integer year; + @JsonProperty("day_of_month") + private Integer dayOfMonth; + @JsonProperty("day_of_week") + private Integer dayOfWeek; + @JsonProperty("minute") + private Integer minute; + + + /** + * Gets fallback value. + * + * @return the fallback value + */ + public String getFallbackValue() { + return fallbackValue; + } + + /** + * Sets fallback value. + * + * @param fallbackValue the fallback value + * @return the fallback value + */ + public DateTime setFallbackValue(String fallbackValue) { + this.fallbackValue = fallbackValue; + return this; + } + + /** + * Gets calendar. + * + * @return the calendar + */ + public CalendarType getCalendar() { + return calendar; + } + + /** + * Sets calendar. + * + * @param calendar the calendar + * @return the calendar + */ + public DateTime setCalendar(CalendarType calendar) { + this.calendar = calendar; + return this; + } + + /** + * Gets month. + * + * @return the month + */ + public Integer getMonth() { + return month; + } + + /** + * Sets month. + * + * @param month the month + * @return the month + */ + public DateTime setMonth(Integer month) { + this.month = month; + return this; + } + + /** + * Gets hour. + * + * @return the hour + */ + public Integer getHour() { + return hour; + } + + /** + * Sets hour. + * + * @param hour the hour + * @return the hour + */ + public DateTime setHour(Integer hour) { + this.hour = hour; + return this; + } + + /** + * Gets year. + * + * @return the year + */ + public Integer getYear() { + return year; + } + + /** + * Sets year. + * + * @param year the year + * @return the year + */ + public DateTime setYear(Integer year) { + this.year = year; + return this; + } + + /** + * Gets day of month. + * + * @return the day of month + */ + public Integer getDayOfMonth() { + return dayOfMonth; + } + + /** + * Sets day of month. + * + * @param dayOfMonth the day of month + * @return the day of month + */ + public DateTime setDayOfMonth(Integer dayOfMonth) { + this.dayOfMonth = dayOfMonth; + return this; + } + + /** + * Gets day of week. + * + * @return the day of week + */ + public Integer getDayOfWeek() { + return dayOfWeek; + } + + /** + * Sets day of week. + * + * @param dayOfWeek the day of week + * @return the day of week + */ + public DateTime setDayOfWeek(Integer dayOfWeek) { + this.dayOfWeek = dayOfWeek; + return this; + } + + /** + * Gets minute. + * + * @return the minute + */ + public Integer getMinute() { + return minute; + } + + /** + * Sets minute. + * + * @param minute the minute + * @return the minute + */ + public DateTime setMinute(Integer minute) { + this.minute = minute; + return this; + } +} diff --git a/src/main/java/com/whatsapp/api/domain/messages/DateTimeParameter.java b/src/main/java/com/whatsapp/api/domain/messages/DateTimeParameter.java index 1e701fc63..356cef546 100644 --- a/src/main/java/com/whatsapp/api/domain/messages/DateTimeParameter.java +++ b/src/main/java/com/whatsapp/api/domain/messages/DateTimeParameter.java @@ -9,22 +9,10 @@ */ @JsonInclude(JsonInclude.Include.NON_NULL) public class DateTimeParameter extends Parameter { - @JsonProperty("fallback_value") - private String fallbackValue; - @JsonProperty("calendar") - private String calendar; - @JsonProperty("month") - private int month; - @JsonProperty("hour") - private int hour; - @JsonProperty("year") - private int year; - @JsonProperty("day_of_month") - private int dayOfMonth; - @JsonProperty("day_of_week") - private int dayOfWeek; - @JsonProperty("minute") - private int minute; + + @JsonProperty("date_time") + private DateTime dateTime; + /** * Instantiates a new Date time parameter. @@ -33,163 +21,35 @@ public DateTimeParameter() { super(ParameterType.DATE_TIME); } - /** - * Gets fallback value. - * - * @return the fallback value - */ - public String getFallbackValue() { - return fallbackValue; - } - - /** - * Sets fallback value. - * - * @param fallbackValue the fallback value - * @return the fallback value - */ - public DateTimeParameter setFallbackValue(String fallbackValue) { - this.fallbackValue = fallbackValue; - return this; - } - - /** - * Gets calendar. - * - * @return the calendar - */ - public String getCalendar() { - return calendar; - } - - /** - * Sets calendar. - * - * @param calendar the calendar - * @return the calendar - */ - public DateTimeParameter setCalendar(String calendar) { - this.calendar = calendar; - return this; - } - - /** - * Gets month. - * - * @return the month - */ - public int getMonth() { - return month; - } - - /** - * Sets month. - * - * @param month the month - * @return the month - */ - public DateTimeParameter setMonth(int month) { - this.month = month; - return this; - } - - /** - * Gets hour. - * - * @return the hour - */ - public int getHour() { - return hour; - } - - /** - * Sets hour. - * - * @param hour the hour - * @return the hour - */ - public DateTimeParameter setHour(int hour) { - this.hour = hour; - return this; - } - - /** - * Gets year. - * - * @return the year - */ - public int getYear() { - return year; - } - - /** - * Sets year. - * - * @param year the year - * @return the year - */ - public DateTimeParameter setYear(int year) { - this.year = year; - return this; - } /** - * Gets day of month. - * - * @return the day of month - */ - public int getDayOfMonth() { - return dayOfMonth; - } - - /** - * Sets day of month. - * - * @param dayOfMonth the day of month - * @return the day of month - */ - public DateTimeParameter setDayOfMonth(int dayOfMonth) { - this.dayOfMonth = dayOfMonth; - return this; - } - - /** - * Gets day of week. + * Instantiates a new Date time parameter. * - * @return the day of week + * @param dateTime the date time */ - public int getDayOfWeek() { - return dayOfWeek; - } + public DateTimeParameter(DateTime dateTime) { + super(ParameterType.DATE_TIME); - /** - * Sets day of week. - * - * @param dayOfWeek the day of week - * @return the day of week - */ - public DateTimeParameter setDayOfWeek(int dayOfWeek) { - this.dayOfWeek = dayOfWeek; - return this; + this.dateTime = dateTime; } /** - * Gets minute. + * Gets date time. * - * @return the minute + * @return the date time */ - public int getMinute() { - return minute; + public DateTime getDateTime() { + return dateTime; } /** - * Sets minute. + * Sets date time. * - * @param minute the minute - * @return the minute + * @param dateTime the date time + * @return the date time */ - public DateTimeParameter setMinute(int minute) { - this.minute = minute; + public DateTimeParameter setDateTime(DateTime dateTime) { + this.dateTime = dateTime; return this; } } diff --git a/src/main/java/com/whatsapp/api/domain/messages/type/CalendarType.java b/src/main/java/com/whatsapp/api/domain/messages/type/CalendarType.java new file mode 100644 index 000000000..8c757095c --- /dev/null +++ b/src/main/java/com/whatsapp/api/domain/messages/type/CalendarType.java @@ -0,0 +1,30 @@ +package com.whatsapp.api.domain.messages.type; + +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * The enum Calendar type. + */ +public enum CalendarType { + + /** + * Gregorian calendar type. + */ + GREGORIAN("GREGORIAN"); + + private final String value; + + CalendarType(String value) { + this.value = value; + } + + /** + * Gets value. + * + * @return the value + */ + @JsonValue + public String getValue() { + return value; + } +} diff --git a/src/test/java/com/whatsapp/api/examples/SendTemplateButtonMessage2Example.java b/src/test/java/com/whatsapp/api/examples/SendTemplateButtonMessage2Example.java new file mode 100644 index 000000000..f66969002 --- /dev/null +++ b/src/test/java/com/whatsapp/api/examples/SendTemplateButtonMessage2Example.java @@ -0,0 +1,73 @@ +package com.whatsapp.api.examples; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.whatsapp.api.TestConstants; +import com.whatsapp.api.WhatsappApiFactory; +import com.whatsapp.api.domain.messages.BodyComponent; +import com.whatsapp.api.domain.messages.ButtonComponent; +import com.whatsapp.api.domain.messages.ButtonPayloadParameter; +import com.whatsapp.api.domain.messages.DateTime; +import com.whatsapp.api.domain.messages.DateTimeParameter; +import com.whatsapp.api.domain.messages.Language; +import com.whatsapp.api.domain.messages.Message.MessageBuilder; +import com.whatsapp.api.domain.messages.TemplateMessage; +import com.whatsapp.api.domain.messages.TextParameter; +import com.whatsapp.api.domain.messages.type.ButtonSubType; +import com.whatsapp.api.domain.messages.type.CalendarType; +import com.whatsapp.api.domain.templates.type.LanguageType; +import com.whatsapp.api.impl.WhatsappBusinessCloudApi; + +import static com.whatsapp.api.TestConstants.PHONE_NUMBER_1; +import static com.whatsapp.api.TestConstants.PHONE_NUMBER_ID; + +public class SendTemplateButtonMessage2Example { + public static void main(String[] args) throws JsonProcessingException { + WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN); + + WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi(); + + var message = MessageBuilder.builder()// + .setTo(PHONE_NUMBER_1)// + .buildTemplateMessage(// + new TemplateMessage()// + .setLanguage(new Language(LanguageType.PT_BR))// + .setName("schedule_confirmation3")// + .addComponent(new BodyComponent()// + .addParameter(new TextParameter("Mauricio"))// + .addParameter(new DateTimeParameter()// + .setDateTime(new DateTime()// + .setCalendar(CalendarType.GREGORIAN)// + .setDayOfMonth(25)// + .setMonth(3)// + .setYear(2023)// + .setHour(13) + .setMinute(50) + .setDayOfWeek(7) + .setFallbackValue("25/03/2023")// + ))// + .addParameter(new DateTimeParameter()// + .setDateTime(new DateTime()// + .setHour(14)// + .setMinute(30)// + .setFallbackValue("14:34")// + )))// + + .addComponent(new ButtonComponent()// + .setIndex(0)// + .setSubType(ButtonSubType.QUICK_REPLY)// + .addParameter(new ButtonPayloadParameter("OP_YES_48547")))// + .addComponent(new ButtonComponent()// + .setIndex(1)// + .setSubType(ButtonSubType.QUICK_REPLY)// + .addParameter(new ButtonPayloadParameter("OP_NO_48548")))// + .addComponent(new ButtonComponent(2, ButtonSubType.QUICK_REPLY)// + .addParameter(new ButtonPayloadParameter("OP_CH_48549")))// + + + ); + System.out.println(new ObjectMapper().writeValueAsString(message)); + + whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message); + } +} diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java index 9b9cf27cd..ab088bd88 100644 --- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java +++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java @@ -6,6 +6,8 @@ import com.whatsapp.api.domain.messages.BodyComponent; import com.whatsapp.api.domain.messages.ButtonComponent; import com.whatsapp.api.domain.messages.ButtonPayloadParameter; +import com.whatsapp.api.domain.messages.DateTime; +import com.whatsapp.api.domain.messages.DateTimeParameter; import com.whatsapp.api.domain.messages.DocumentMessage; import com.whatsapp.api.domain.messages.ImageMessage; import com.whatsapp.api.domain.messages.Language; @@ -16,6 +18,7 @@ import com.whatsapp.api.domain.messages.TextParameter; import com.whatsapp.api.domain.messages.VideoMessage; import com.whatsapp.api.domain.messages.type.ButtonSubType; +import com.whatsapp.api.domain.messages.type.CalendarType; import com.whatsapp.api.domain.templates.type.LanguageType; import com.whatsapp.api.exception.WhatsappApiException; import com.whatsapp.api.utils.Formatter; @@ -181,6 +184,59 @@ void testSendTemplateButtonMessage() throws IOException, URISyntaxException, Int } + @Test + void testSendTemplateButtonMessageWithDateTimeParam() throws IOException, URISyntaxException, InterruptedException, JSONException { + mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE)); + + var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage4.json"); + + var templateMessage = new TemplateMessage()// + .setLanguage(new Language(LanguageType.PT_BR))// + .setName("schedule_confirmation3")// + .addComponent(new BodyComponent()// + .addParameter(new TextParameter("Mauricio"))// + .addParameter(new DateTimeParameter()// + .setDateTime(new DateTime()// + .setCalendar(CalendarType.GREGORIAN)// + .setDayOfMonth(25)// + .setMonth(3)// + .setYear(2023)// + .setHour(13).setMinute(50).setDayOfWeek(7).setFallbackValue("25/03/2023")// + ))// + .addParameter(new DateTimeParameter()// + .setDateTime(new DateTime()// + .setHour(14)// + .setMinute(30)// + .setFallbackValue("14:34")// + )))// + + .addComponent(new ButtonComponent()// + .setIndex(0)// + .setSubType(ButtonSubType.QUICK_REPLY)// + .addParameter(new ButtonPayloadParameter("OP_YES_48547")))// + .addComponent(new ButtonComponent()// + .setIndex(1)// + .setSubType(ButtonSubType.QUICK_REPLY)// + .addParameter(new ButtonPayloadParameter("OP_NO_48548")))// + .addComponent(new ButtonComponent(2, ButtonSubType.QUICK_REPLY)// + .addParameter(new ButtonPayloadParameter("OP_CH_48549"))); + + + var message = MessageBuilder.builder()// + .setTo(PHONE_NUMBER_1)// + .buildTemplateMessage(templateMessage); + + 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()); + //System.out.println(recordedRequest.getBody().readUtf8()); + + JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT); + + } + @Test void testSendAudioMessage() throws IOException, URISyntaxException, InterruptedException { diff --git a/src/test/resources/expected/message/expectedMessage4.json b/src/test/resources/expected/message/expectedMessage4.json new file mode 100644 index 000000000..b1b57ff07 --- /dev/null +++ b/src/test/resources/expected/message/expectedMessage4.json @@ -0,0 +1,77 @@ +{ + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": "121212121212", + "type": "template", + "template": { + "components": [ + { + "type": "body", + "parameters": [ + { + "type": "text", + "text": "Mauricio" + }, + { + "type": "date_time", + "date_time": { + "fallback_value": "25/03/2023", + "calendar": "GREGORIAN", + "month": 3, + "hour": 13, + "year": 2023, + "day_of_month": 25, + "day_of_week": 7, + "minute": 50 + } + }, + { + "type": "date_time", + "date_time": { + "fallback_value": "14:34", + "hour": 14, + "minute": 30 + } + } + ] + }, + { + "type": "button", + "parameters": [ + { + "type": "payload", + "payload": "OP_YES_48547" + } + ], + "index": 0, + "sub_type": "quick_reply" + }, + { + "type": "button", + "parameters": [ + { + "type": "payload", + "payload": "OP_NO_48548" + } + ], + "index": 1, + "sub_type": "quick_reply" + }, + { + "type": "button", + "parameters": [ + { + "type": "payload", + "payload": "OP_CH_48549" + } + ], + "index": 2, + "sub_type": "quick_reply" + } + ], + "name": "schedule_confirmation3", + "language": { + "code": "pt_BR" + } + } +} \ No newline at end of file