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

MediaAttachment in Send API should work without URL or ResuseId #1062

Closed
nbartels opened this issue Mar 5, 2020 · 3 comments
Closed

MediaAttachment in Send API should work without URL or ResuseId #1062

nbartels opened this issue Mar 5, 2020 · 3 comments
Assignees
Milestone

Comments

@nbartels
Copy link
Contributor

nbartels commented Mar 5, 2020

According to the Facebook docs it is possible to send an attachment without providing an URL or a ReuseId. The binary data is directly send to the me/messages endpoint.

We have to support this, too.
https://developers.facebook.com/docs/messenger-platform/send-messages#file

@nbartels
Copy link
Contributor Author

nbartels commented Mar 6, 2020

Here is some workaround and the information for the documentation:

First we need a simple new MessageAttachment object. In the version we will publish to our repository the payload will be more sophisticated, this is just the shortest version that works.

import com.restfb.Facebook;
import com.restfb.types.send.MediaAttachment.Type;
import com.restfb.types.send.MessageAttachment;

public class UploadMessageAttachment extends MessageAttachment {

  @Facebook
  private Object payload = new Object(); 
  // payload is important, without publishing will not work

  private static final long serialVersionUID = 1L;

  public UploadMessageAttachment(Type type) {
    setType(type.toString().toLowerCase());
  }

}

Now we have the code in the application. We need a recipient, an attachment and the we can publish the answer. This will look like:

IdMessageRecipient recipient = new IdMessageRecipient("receiving user id");
BinaryAttachment attachment = BinaryAttachment.with("filename", getIs());

// we have to tell FB what kind of attachment we are going to publish, 
// in this example it is an image, but you can use file, video and audio, too
UploadMessageAttachment messageAttachment = new UploadMessageAttachment(Type.IMAGE);

// the message can only contain text OR an attachment
Message message = new Message(messageAttachment);

//Now, we publish this stuff
SendResponse resp = client.publish("me/messages", SendResponse.class, attachment,
        Parameter.with("recipient", recipient),
        Parameter.with("message", message));

// in the resp object you find some useful information

@torbenw
Copy link

torbenw commented Mar 9, 2020

There is no way to send more than 1 attachment or to include a text, right? So we will have publish each attachment and the text individually.

@nbartels
Copy link
Contributor Author

nbartels commented Mar 9, 2020

Yes, this is how the Facebook API works...
https://developers.facebook.com/docs/messenger-platform/reference/send-api/#message

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants