Skip to content

Commit

Permalink
So I finished the other end of the file-sending, which is sending att…
Browse files Browse the repository at this point in the history
…achment-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.
  • Loading branch information
Raud0 committed Oct 29, 2020
1 parent eb97e9b commit 1c0ca59
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, ReusableFile> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> generateHeaders(MessengerPlatform platform) {
val headers = new HashMap<String, String>();
headers.put(HttpHeaders.AUTHORIZATION, "Bearer " + platform.getAccessToken());
headers.put(HttpHeaders.CONTENT_TYPE, "application/json");
return headers;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> getParams() {
Map<String,Object> params = new LinkedHashMap<>();
params.put("recipient",gson.toJsonTree(new Messaging(recipient)));
params.putAll(super.getParams());
return params;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, Object> getParams() {
Map<String,Object> params = new LinkedHashMap<>();
params.put("message",gson.toJsonTree(new FileSending(attachment)));
params.put("filedata",file);
params.put("type",type);
return params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}

1 comment on commit 1c0ca59

@Raud0
Copy link
Collaborator Author

@Raud0 Raud0 commented on 1c0ca59 Oct 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yea #20

Please sign in to comment.