From 89117bca94d5db1fc00570c31ac767295e70ba06 Mon Sep 17 00:00:00 2001 From: Taido Purason Date: Wed, 14 Oct 2020 14:26:31 +0300 Subject: [PATCH 01/23] Fixed #42 #43 tests. Removed counter from platform. This shouldn't be there. --- .../plugins/messenger/platform/MessengerPlatform.java | 8 -------- .../platform/io/MessengerIntentProviderTest.java | 8 ++++---- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/MessengerPlatform.java b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/MessengerPlatform.java index 5eb9128..4736822 100644 --- a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/MessengerPlatform.java +++ b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/MessengerPlatform.java @@ -28,16 +28,12 @@ public class MessengerPlatform extends RestPlatform { private String verifyToken; private String accessToken; private String appSecret; - private int messagesSent; - private int messagesSentSuccessfully; @Override public void start(@NonNull XatkitBot xatkitBot, @NonNull Configuration configuration) { verifyToken = requireNonNull(configuration.getString(MessengerUtils.VERIFY_TOKEN_KEY)); accessToken = requireNonNull(configuration.getString(MessengerUtils.ACCESS_TOKEN_KEY)); appSecret = requireNonNull(configuration.getString(MessengerUtils.APP_SECRET_KEY)); - messagesSent = 0; - messagesSentSuccessfully = 0; super.start(xatkitBot, configuration); xatkitBot.getXatkitServer().registerRestEndpoint(HttpMethod.GET, "/messenger/webhook", @@ -81,13 +77,11 @@ public void reply(@NonNull StateContext context, @NonNull Message message) { } private void excecuteReply(Reply reply) { - messagesSent++; val result = reply.call().getResult(); if (result instanceof ApiResponse) { val apiResponse = (ApiResponse) result; Log.debug("REPLY RESPONSE STATUS: {0} {1}\n BODY: {2}", apiResponse.getStatus(), apiResponse.getStatusText(), apiResponse.getBody().toString()); - if (apiResponse.getStatus() == 200) messagesSentSuccessfully++; } else { Log.debug("Unexpected reply result: {0}", result); } @@ -100,6 +94,4 @@ public String getAppSecret() { public String getAccessToken() { return accessToken; } - - public int getMessagesSent() { return messagesSent; } } diff --git a/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java b/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java index fbdfaf8..e4954b8 100644 --- a/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java +++ b/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java @@ -10,10 +10,11 @@ import com.xatkit.intent.*; import com.xatkit.plugins.messenger.platform.MessengerPlatform; import com.xatkit.plugins.messenger.platform.MessengerUtils; -import com.xatkit.plugins.messenger.platform.entity.SenderAction; import lombok.SneakyThrows; +import lombok.val; import org.apache.commons.configuration2.BaseConfiguration; import org.apache.commons.configuration2.Configuration; +import org.apache.http.HttpEntity; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; @@ -151,12 +152,11 @@ public void verifyMessageHasRawText() throws RestHandlerException, NoSuchAlgorit public void verify200ResponseForTextlessMessage() throws RestHandlerException, NoSuchAlgorithmException, InvalidKeyException { provider = new MessengerIntentProvider(platform); provider.start(configuration); - int expectedCount = platform.getMessagesSent() + 1; - provider.createRestHandler().handleContent( + val response = provider.createRestHandler().handleContent( generateHeaders(TEXTLESS_MESSAGE, platform.getAppSecret()), new ArrayList<>(), TEXTLESS_MESSAGE); - assertThat(platform.getMessagesSent()).isEqualTo(expectedCount); + assertThat(response).isInstanceOf(HttpEntity.class); } @Test From 06c6ba154072b7dec4c7e6372a8737ff906784ac Mon Sep 17 00:00:00 2001 From: Taido Purason Date: Wed, 14 Oct 2020 15:01:16 +0300 Subject: [PATCH 02/23] Additional checks to #42 #43 #14 #15 tests. --- .../platform/io/MessengerIntentProviderTest.java | 8 ++++++++ .../platform/io/MessengerIntentProviderTestUtils.java | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java b/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java index e4954b8..aace7ab 100644 --- a/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java +++ b/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java @@ -28,6 +28,7 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; +import java.util.List; import static java.util.Objects.nonNull; import static org.mockito.Mockito.*; @@ -201,6 +202,9 @@ public void handleReaction() throws RestHandlerException, NoSuchAlgorithmExcepti verify(mockedExecutionService, times(1)).handleEventInstance(eventCaptor.capture(), any(StateContext.class)); EventInstance sentEvent = eventCaptor.getValue(); assertThat(sentEvent.getDefinition()).isEqualTo(MessengerIntentProvider.MessageReact); + assertThat(sentEvent.getPlatformData().get(MessengerUtils.MESSAGE_ID_KEY)).isEqualTo(MESSAGE_ID); + assertThat(sentEvent.getPlatformData().get(MessengerUtils.REACTION_KEY)).isEqualTo(REACTION); + assertThat(sentEvent.getPlatformData().get(MessengerUtils.EMOJI_KEY)).isEqualTo(EMOJI); verify(mockedXatkitBot, times(1)).getOrCreateContext(eq(SENDER_ID)); } @@ -216,6 +220,7 @@ public void handleUnreaction() throws RestHandlerException, NoSuchAlgorithmExcep verify(mockedExecutionService, times(1)).handleEventInstance(eventCaptor.capture(), any(StateContext.class)); EventInstance sentEvent = eventCaptor.getValue(); assertThat(sentEvent.getDefinition()).isEqualTo(MessengerIntentProvider.MessageUnreact); + assertThat(sentEvent.getPlatformData().get(MessengerUtils.MESSAGE_ID_KEY)).isEqualTo(MESSAGE_ID); verify(mockedXatkitBot, times(1)).getOrCreateContext(eq(SENDER_ID)); } @@ -232,6 +237,7 @@ public void handleRead() throws NoSuchAlgorithmException, InvalidKeyException, R verify(mockedExecutionService, times(1)).handleEventInstance(eventCaptor.capture(), any(StateContext.class)); EventInstance sentEvent = eventCaptor.getValue(); assertThat(sentEvent.getDefinition()).isEqualTo(MessengerIntentProvider.MessageRead); + assertThat(sentEvent.getPlatformData().get(MessengerUtils.WATERMARK_KEY)).isEqualTo(WATERMARK); verify(mockedXatkitBot, times(1)).getOrCreateContext(eq(SENDER_ID)); } @@ -248,6 +254,8 @@ public void handleDelivered() throws NoSuchAlgorithmException, InvalidKeyExcepti verify(mockedExecutionService, times(1)).handleEventInstance(eventCaptor.capture(), any(StateContext.class)); EventInstance sentEvent = eventCaptor.getValue(); assertThat(sentEvent.getDefinition()).isEqualTo(MessengerIntentProvider.MessageDelivered); + assertThat(sentEvent.getPlatformData().get(MessengerUtils.WATERMARK_KEY)).isEqualTo(WATERMARK); + assertThat(sentEvent.getPlatformData().get(MessengerUtils.MESSAGE_IDS_KEY)).asList().contains(MESSAGE_ID); verify(mockedXatkitBot, times(1)).getOrCreateContext(eq(SENDER_ID)); } diff --git a/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTestUtils.java b/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTestUtils.java index 1d038ef..0d3b15c 100644 --- a/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTestUtils.java +++ b/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTestUtils.java @@ -16,6 +16,8 @@ public class MessengerIntentProviderTestUtils { public static final String APP_SECRET = "TEST_APP_SECRET"; public static final String MESSAGE_TEXT = "hello"; public static final long WATERMARK = 1458668856253L; + public static final String REACTION = "wow"; + public static final String EMOJI = "\uD83D\uDE2F"; public static final String CORRECT_CONTENT = "{\n" + " \"object\":\"page\",\n" + @@ -156,8 +158,8 @@ public class MessengerIntentProviderTestUtils { " \"id\":\"" + RECIPIENT_PSID + "\"\n" + " },\n" + " \"reaction\":{\n" + - " \"reaction\":\"wow\",\n" + - " \"emoji\":\"\uD83D\uDE2F\",\n" + + " \"reaction\":\"" + REACTION + "\",\n" + + " \"emoji\":\"" + EMOJI + "\",\n" + " \"action\":\"react\",\n" + " \"mid\":\"" + MESSAGE_ID + "\"\n" + " }\n" + From a94cf2d3a415bae9e9428c7168b3debb403991f8 Mon Sep 17 00:00:00 2001 From: Taido Purason Date: Wed, 14 Oct 2020 15:02:20 +0300 Subject: [PATCH 03/23] Removed unnecessary configuration property --- .../messenger/platform/io/MessengerIntentProviderTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java b/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java index aace7ab..6d9e080 100644 --- a/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java +++ b/runtime/src/test/java/com/xatkit/plugins/messenger/platform/io/MessengerIntentProviderTest.java @@ -86,8 +86,6 @@ protected MessengerPlatform getPlatform() { configuration.addProperty(MessengerUtils.HANDLE_REACTIONS_KEY, true); configuration.addProperty(MessengerUtils.HANDLE_DELIVERIES_KEY, true); configuration.addProperty(MessengerUtils.HANDLE_READ_KEY, true); - configuration.addProperty(MessengerUtils.AUTO_MARK_SEEN_KEY, true); - MessengerPlatform messengerPlatform = new MessengerPlatform(); messengerPlatform.start(mockedXatkitBot, configuration); return messengerPlatform; From bdf5d8161dc0f609b298a8062169a84f2ca61a34 Mon Sep 17 00:00:00 2001 From: Andreas Teder Date: Sun, 18 Oct 2020 15:58:31 +0300 Subject: [PATCH 04/23] Implemented initial General Templates. Implemented URL Button. Due to not seeing any tests in other Xatkit platforms when it comes to sent messages, tests have not yet been written. See #21 #58 --- .../messenger/platform/entity/Attachment.java | 16 ++++++-- .../messenger/platform/entity/Element.java | 32 ++++++++++++++++ .../messenger/platform/entity/Message.java | 3 +- .../platform/entity/buttons/Button.java | 4 ++ .../entity/buttons/DefaultActionButton.java | 35 ++++++++++++++++++ .../entity/buttons/PostbackButton.java | 5 +++ .../platform/entity/buttons/URLButton.java | 21 +++++++++++ .../entity/buttons/WebviewHeightRatio.java | 7 ++++ .../payloads/ButtonTemplatePayload.java | 5 +++ .../entity/payloads/GeneralPayload.java | 4 ++ .../payloads/GenericTemplatePayload.java | 37 +++++++++++++++++++ .../entity/payloads/MediaTemplatePayload.java | 5 +++ .../NonTemplatePayload.java} | 7 ++-- 13 files changed, 174 insertions(+), 7 deletions(-) create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Element.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/buttons/Button.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/buttons/DefaultActionButton.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/buttons/PostbackButton.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/buttons/URLButton.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/buttons/WebviewHeightRatio.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/payloads/ButtonTemplatePayload.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/payloads/GeneralPayload.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/payloads/GenericTemplatePayload.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/payloads/MediaTemplatePayload.java rename runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/{Payload.java => payloads/NonTemplatePayload.java} (50%) diff --git a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Attachment.java b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Attachment.java index bfaeed8..73207eb 100644 --- a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Attachment.java +++ b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Attachment.java @@ -1,13 +1,16 @@ package com.xatkit.plugins.messenger.platform.entity; import com.google.gson.annotations.SerializedName; +import com.xatkit.plugins.messenger.platform.entity.payloads.GeneralPayload; +import com.xatkit.plugins.messenger.platform.entity.payloads.GenericTemplatePayload; +import com.xatkit.plugins.messenger.platform.entity.payloads.NonTemplatePayload; import lombok.Getter; public class Attachment { @Getter private final AttachmentType type; @Getter - private final Payload payload; + private final GeneralPayload payload; // TODO: Support files and templates as well public enum AttachmentType { @@ -16,11 +19,18 @@ public enum AttachmentType { @SerializedName("video") video, @SerializedName("image") - image + image, + @SerializedName("template") + template } - public Attachment(final AttachmentType type, final Payload payload) { + public Attachment(final AttachmentType type, final NonTemplatePayload payload) { this.type = type; this.payload = payload; } + + public Attachment(final GenericTemplatePayload genericTemplatePayload) { + this.type = AttachmentType.template; + this.payload = genericTemplatePayload; + } } diff --git a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Element.java b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Element.java new file mode 100644 index 0000000..123bcc2 --- /dev/null +++ b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Element.java @@ -0,0 +1,32 @@ +package com.xatkit.plugins.messenger.platform.entity; + +import com.google.gson.annotations.SerializedName; +import com.xatkit.plugins.messenger.platform.entity.buttons.Button; +import com.xatkit.plugins.messenger.platform.entity.buttons.DefaultActionButton; +import lombok.Getter; + +import java.util.List; + +//TODO Add javadoc once Media templates (which also use Element, but with different fields) are done. +public class Element { + @Getter + private final String title; //Mandatory along with 1 more field | 80 character limit (the rest will get shortened with ...) + @Getter + private final String subtitle; //80 character limit (the rest will get shortened with ...) + @SerializedName(value = "image_url") + @Getter + private final String imageURL; + @SerializedName(value = "default_action") + @Getter + private final DefaultActionButton defaultActionButton; + @Getter + private final List