Here is the curl From Facebook Docs to Upload a file to their servers for sending to the specified user:
curl
-F recipient='{"id":"USER_ID"}'
-F message='{"attachment":{"type":"file", "payload":{}}}'
-F filedata=@/tmp/receipt.pdf
"https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
curl
-F 'recipient={"id":"USER_ID"}'
-F 'message={"attachment":{"type":"audio", "payload":{}}}'
-F 'filedata=@/tmp/clip.mp3;type=audio/mp3'
"https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
I am able to do this via Curl but I would like to do that using Java and Spring-Boot.
How to go about it?
The file I am trying to send is in my src-main-resource-docs folder.
Edit 1
I wanted to know what’s this filedata=@/tmp/clip.mp3;type=audio/MP3
Should this just be the link to my resource file?
Or do I have to change it to MultipartFile to send it?
Edit 2
Currently this is what I am doing:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost("https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("recipient", "{"id":"USER_ID"}", ContentType.APPLICATION_JSON);
File f = new File("./src/main/resources/docs/test.pdf");
builder.addBinaryBody(
"file",
new FileInputStream(f),
ContentType.APPLICATION_OCTET_STREAM,
f.getName()
);
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
HttpEntity responseEntity = response.getEntity();
This works fine for pdfs
But in audio/mp3 there is a type=audio/mp3 . Where do I put that?
2
Answers
Finally was able to upload Docs/Media to Facebook via the Send Api Reference,here's what I did:
I used OkHttp3 library to build the request,
One needs to add exception handling for IOException
Take a look into restFb library. With it you will be able to do: