Possibility of attaching files instead of using URLS
Currently the reply factory object will allow only to attach files using URLS (audio, video, images and generic files attachments). Facebook API's also allows to upload a file directly. It would be nice to add this feature to the framework. Here's an instance of how to do it with cURL: https://developers.facebook.com/docs/messenger-platform/send-api-reference/audio-attachment. In order to do this with FaceBot, we should add a method that makes a POST with multipart/data. I've made some tests with Postman and the API works even if you don't specify the extension of file you are attaching, so we don't need to add that.
I'm having some troubles on this. I've tried this:
public static void postFormDataMessage(String recipient,
AttachmentType type, File file) {
String pageToken = FaceBotContext.getInstance().getPageToken();
// If the page token is invalid, returns.
if (!validatePageToken(pageToken)) {
return;
}
// TODO: add checks for valid attachmentTypes (FILE, AUDIO or VIDEO)
HttpPost post = new HttpPost(FaceBotConstants.FACEBOOK_BASE_URL
+ FaceBotConstants.FACEBOOK_MESSAGES_URL + pageToken);
FileBody filedata = new FileBody(file);
StringBody recipientPart = new StringBody("{\"id\":\"" + recipient
+ "\"}", ContentType.MULTIPART_FORM_DATA);
StringBody messagePart = new StringBody("{\"attachment\":{\"type\":\""
+ type.name().toLowerCase() + "\", \"payload\":{}}}",
ContentType.MULTIPART_FORM_DATA);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("recipient", recipientPart);
builder.addPart("message", messagePart);
builder.addBinaryBody("filedata", file);
builder.setContentType(ContentType.MULTIPART_FORM_DATA);
HttpEntity entity = builder.build();
post.setEntity(entity);
send(post);
}
But I'm getting an error about the attached file not supported. If I do the same request from postman it works though. I think the problem is related to the headers. This is the request generated by postman:
HTTP/1.1
Host: graph.facebook.com
Cache-Control: no-cache
Postman-Token: 6469ec4e-4622-7a93-7a9c-f428db6f4fcd
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="recipient"
{"id":"1545768135449437"}
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="message"
{"attachment":{"type":"audio", "payload":{}}}
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="filedata"; filename=""
Content-Type:
------WebKitFormBoundary7MA4YWxkTrZu0gW--
As you can see the Content-Type of the file data (the last part) is empty here, meanwhile the one generated with Apache Http is application/octet-stream and I don't really know how to leave it empty.
Any help is appreciated.
It seems that the UploadAPI should resolve this issue. Please close if so.
I think they are 2 different features but I have to look more into it. Meanwhile, I've opened this question on StackOverflow.