Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility of attaching files instead of using URLS #2

Open
aurasphere opened this issue Nov 22, 2016 · 3 comments
Open

Possibility of attaching files instead of using URLS #2

aurasphere opened this issue Nov 22, 2016 · 3 comments
Assignees

Comments

@aurasphere
Copy link
Collaborator

aurasphere commented Nov 22, 2016

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.

@aurasphere aurasphere changed the title Possibility of attaching files instead of using links Possibility of attaching files instead of using URLS Nov 22, 2016
@aurasphere
Copy link
Collaborator Author

aurasphere commented Nov 26, 2016

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.

@alvin-reyes alvin-reyes self-assigned this Dec 26, 2016
@alvin-reyes alvin-reyes modified the milestones: v1.1.0, v1.1.0-RC1 Dec 30, 2016
@alvin-reyes
Copy link
Collaborator

It seems that the UploadAPI should resolve this issue. Please close if so.

@aurasphere
Copy link
Collaborator Author

aurasphere commented May 22, 2017

I think they are 2 different features but I have to look more into it. Meanwhile, I've opened this question on StackOverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants