From 1c0ca592b475b56bffb3b83902492933c3e501b6 Mon Sep 17 00:00:00 2001 From: Raud0 Date: Thu, 29 Oct 2020 19:56:42 +0200 Subject: [PATCH] So I finished the other end of the file-sending, which is sending attachment-ids back, as well as tried to implement a secondary option of sending files directly. Netiher works atm, and my desire to get in touch with Facebook support is increasing. For example, when sending the file with attachment_id, it now gives the error of "wrong number of files sent", even though it doesn't make sense. Such an error should already appear earlier and has. The other method also doesn't work and is telling me that the recipient parameter is required, again, it's there. This leads me to believe that I don't either 1) understand how the Facebook curl examples work or 2) how using the JsonRestRequest in the xatkit rest platform package works. --- .../messenger/platform/FileStorage.java | 46 +++++++++++++++++++ .../messenger/platform/MessengerPlatform.java | 34 ++++++++++++-- .../messenger/platform/action/FileReply.java | 35 ++++++++++++++ .../messenger/platform/entity/DirectFile.java | 30 ++++++++++++ .../messenger/platform/entity/File.java | 12 ++++- .../messenger/platform/entity/Messaging.java | 6 +++ .../platform/entity/ReusableFile.java | 15 ++++++ 7 files changed, 171 insertions(+), 7 deletions(-) create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/FileStorage.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/action/FileReply.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/DirectFile.java create mode 100644 runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/ReusableFile.java diff --git a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/FileStorage.java b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/FileStorage.java new file mode 100644 index 0000000..97526c6 --- /dev/null +++ b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/FileStorage.java @@ -0,0 +1,46 @@ +package com.xatkit.plugins.messenger.platform; + +import com.xatkit.execution.StateContext; +import com.xatkit.plugins.messenger.platform.entity.Attachment; +import com.xatkit.plugins.messenger.platform.entity.File; +import com.xatkit.plugins.messenger.platform.entity.ReusableFile; +import com.xatkit.plugins.messenger.platform.entity.payloads.AttachmentIDPayload; + +import java.util.HashMap; + +public class FileStorage { + MessengerPlatform platform; + HashMap files; + + public FileStorage(MessengerPlatform platform) { + this.platform = platform; + this.files = new HashMap<>(); + } + + public Attachment LazyAttachment(String filepath, StateContext context) { + CreateAttachment(null, filepath, Attachment.AttachmentType.file, null, context); + return RetrieveAttachment(filepath); + } + + public Attachment RetrieveAttachment(String name) { + if (!files.containsKey(name)) return null; + + ReusableFile file = files.get(name); + Attachment.AttachmentType type = file.getFile().getAttachment().getType(); + AttachmentIDPayload payload = new AttachmentIDPayload(file.getAttachmentId()); + Attachment attachment = new Attachment(type,payload); + + return attachment; + } + + public void CreateAttachment(String name, String filepath, Attachment.AttachmentType type, String extension, StateContext context) { + if (files.containsKey(name)) return; + + File file = new File(type,new java.io.File(filepath),extension); + ReusableFile reusableFile = new ReusableFile(file); + if (name == null) name = filepath; + files.put(name,reusableFile); + + platform.sendFile(context,reusableFile); + } +} 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 7f92922..970cfd0 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 @@ -1,5 +1,6 @@ package com.xatkit.plugins.messenger.platform; +import com.google.gson.JsonParser; import com.xatkit.core.XatkitBot; import com.xatkit.core.platform.RuntimePlatform; import com.xatkit.core.server.*; @@ -62,15 +63,38 @@ public void sendAction(@NonNull StateContext context, @NonNull SenderAction send excecuteReply(new Reply(this, context, messaging)); } - public void sendFile(@NonNull StateContext context, File file) { - val senderId = context.getContextId(); - Log.debug("SENDING FILE TO: {0}", senderId); - executeSendFile(new FilePost(this, context, file)); + public void sendFile(@NonNull StateContext context, ReusableFile file) { + executeSendFile(new FilePost(this, context, file.getFile()), file); } - private void executeSendFile(FilePost filePost) { + private void executeSendFile(FilePost filePost, ReusableFile file) { val result = filePost.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()); + + JsonParser jsonParser = new JsonParser(); + String attachment_id = jsonParser.parse(apiResponse.getBody().toString()).getAsJsonObject().get("attachment_id").getAsString(); + file.setAttachmentId(attachment_id); + Log.debug("Saved attachment ID: {0}", attachment_id); + } else { + Log.debug("Unexpected reply result: {0}", result); + } + } + + public void reply(@NonNull StateContext context, File file) { + DirectFile directFile = new DirectFile(context.getContextId(), file); + + excecuteReply(new FileReply( + this, + context, + directFile)); + } + + private void excecuteReply(FileReply reply) { + 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()); diff --git a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/action/FileReply.java b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/action/FileReply.java new file mode 100644 index 0000000..f150b6b --- /dev/null +++ b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/action/FileReply.java @@ -0,0 +1,35 @@ +package com.xatkit.plugins.messenger.platform.action; + +import com.google.gson.Gson; +import com.xatkit.execution.StateContext; +import com.xatkit.plugins.messenger.platform.MessengerPlatform; +import com.xatkit.plugins.messenger.platform.MessengerUtils; +import com.xatkit.plugins.messenger.platform.entity.DirectFile; +import com.xatkit.plugins.rest.platform.action.PostJsonRequestWithFormData; +import lombok.val; +import org.apache.http.HttpHeaders; + +import java.util.HashMap; +import java.util.Map; + +public class FileReply extends PostJsonRequestWithFormData { + private static final Gson gson = new Gson(); + + /** + * Constructs a POST Json request with form data parameters + * + * @param platform the {@link MessengerPlatform} containing this action + * @param context the {@link StateContext} associated to this action + * @param file the information related to the file to be sent; + */ + public FileReply(MessengerPlatform platform, StateContext context, DirectFile file) { + super(platform, context, MessengerUtils.SEND_API_URL, null, null, generateHeaders(platform), file.getParams()); + } + + private static Map generateHeaders(MessengerPlatform platform) { + val headers = new HashMap(); + headers.put(HttpHeaders.AUTHORIZATION, "Bearer " + platform.getAccessToken()); + headers.put(HttpHeaders.CONTENT_TYPE, "application/json"); + return headers; + } +} diff --git a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/DirectFile.java b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/DirectFile.java new file mode 100644 index 0000000..aafeb45 --- /dev/null +++ b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/DirectFile.java @@ -0,0 +1,30 @@ +package com.xatkit.plugins.messenger.platform.entity; +import com.google.gson.Gson; +import com.xatkit.plugins.messenger.platform.entity.payloads.FilePayload; +import fr.inria.atlanmod.commons.log.Log; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class DirectFile extends File { + private static final Gson gson = new Gson(); + + private Recipient recipient; + + public DirectFile(String recipientId, File file) { + super(file); + this.recipient = new Recipient(recipientId); + } + + public DirectFile(String recipientId, Attachment.AttachmentType attachmentType, java.io.File file, String fileExtension) { + super(attachmentType,file,fileExtension); + this.recipient = new Recipient(recipientId); + } + + public Map getParams() { + Map params = new LinkedHashMap<>(); + params.put("recipient",gson.toJsonTree(new Messaging(recipient))); + params.putAll(super.getParams()); + return params; + } +} diff --git a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/File.java b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/File.java index 0c1ab32..c231345 100644 --- a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/File.java +++ b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/File.java @@ -1,6 +1,7 @@ package com.xatkit.plugins.messenger.platform.entity; import com.google.gson.Gson; import com.xatkit.plugins.messenger.platform.entity.payloads.FilePayload; +import fr.inria.atlanmod.commons.log.Log; import java.util.LinkedHashMap; import java.util.Map; @@ -16,19 +17,26 @@ public File(Attachment.AttachmentType attachmentType, java.io.File file, String String type = attachmentType.name(); if (fileExtension != null) type += "/" + fileExtension.toLowerCase(); this.type = type; + Log.debug("TYPE: {0}", type); this.file = file; this.attachment = new Attachment(attachmentType,new FilePayload(true)); } - public String getContentType() { - return type; + public File(File file) { + this.attachment = file.getAttachment(); + this.file = file.getFile(); + this.type = file.getType(); } + public Attachment getAttachment() { return attachment; } + public java.io.File getFile() { return file; } + public String getType() { return type; } public Map getParams() { Map params = new LinkedHashMap<>(); params.put("message",gson.toJsonTree(new FileSending(attachment))); params.put("filedata",file); + params.put("type",type); return params; } } diff --git a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Messaging.java b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Messaging.java index 9a2cd19..1dbf3f4 100644 --- a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Messaging.java +++ b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/Messaging.java @@ -31,4 +31,10 @@ public Messaging(Recipient recipient, Message message) { public Messaging(String recipent, Message message) { this(new Recipient(recipent), message); } + + public Messaging(Recipient recipient) { + this.recipient = recipient; + this.senderAction = null; + this.message = null; + } } diff --git a/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/ReusableFile.java b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/ReusableFile.java new file mode 100644 index 0000000..901463d --- /dev/null +++ b/runtime/src/main/java/com/xatkit/plugins/messenger/platform/entity/ReusableFile.java @@ -0,0 +1,15 @@ +package com.xatkit.plugins.messenger.platform.entity; + +public class ReusableFile { + private String attachmentId; + private String filePath; + private File file; + + public ReusableFile(File file) { this.file = file; } + public String getAttachmentId() { return attachmentId; } + public void setAttachmentId(String attachmentId) { this.attachmentId = attachmentId; } + public String getFilePath() { return filePath; } + public void setFilePath(String filePath) { this.filePath = filePath; } + public File getFile() { return file; } + public void setFile(File file) { this.file = file; } +}