Page private messages and image attachments

524 views
Skip to first unread message

S13_Alan

unread,
Jan 9, 2013, 4:25:54 AM1/9/13
to res...@googlegroups.com
Hi,

I've got a requirement to be able to manage pages private messages, which is fine, I can get the conversations, I can reply etc. When it came to testing what happens when I add a photo I fetched the JSON to find they are in the result for each message as 'attachments', and that I couldn't get to this by default in RestFB. I also found you can view attachments separately as m_id.xxxxx/attachments but that's not required as they seem to come with the regular message/conversation call.

"attachments": {
      "data": [
         {
            "id": "b78ffab9a306e8b0a12c57f939b76",
            "mime_type": "image/jpeg",
            "name": "13765735334.jpg",
            "size": 350567
         }
      ]
   }

I tried for a while to find an inbuilt way to do it and then settled on modifying the Message class and adding a new class called Attachment, just keeping it in line with how Comments are included with a Post.

It works fine as far as I can see it and is allowing me to get back enough info to create a URL pointing to the image but I just wonder if I'm missing something, or is this how it would be expected to do to?

I've included the additions I made below, just in case anyone else reading this has the same requirement.

Thanks,
Alan.

Attachment.java

package com.restfb.types;

import com.restfb.Facebook;

/**
 * Represents an attached picture that you may find on a private message
 * @author alockhart
 *
 */
public class Attachment extends FacebookType {

    private static final long serialVersionUID = 1L;
     
      @Facebook
      private String name;
     
      @Facebook("mime_type")
      private String mimeType;
     
      @Facebook
      private Long size;
     
      /**
       * Returns the size of the attachment, like 350567
       * @return
       */
      public Long getSize(){
          return size;
      }
     
      /**
       * Name of the attachment, like 121423423.jpg
       * @return
       */
      public String getName(){
          return name;
      }
     
      /**
       * Mime type of attachment, like image/jpeg
       * @return
       */
      public String getMimeType(){
          return mimeType;
      }
   
}

Addition to Message.java

  @Facebook
  private Attachments attachments;
 
  /**
   * Represents picture attachments etc
   * @author alockhart
   *
   */
  public static class Attachments implements Serializable {
        @Facebook
        private Long count;

        @Facebook
        private List<Attachment> data = new ArrayList<Attachment>();

        private static final long serialVersionUID = 1L;

        /**
         * @see java.lang.Object#hashCode()
         */
        @Override
        public int hashCode() {
          return ReflectionUtils.hashCode(this);
        }

        /**
         * @see java.lang.Object#equals(java.lang.Object)
         */
        @Override
        public boolean equals(Object that) {
          return ReflectionUtils.equals(this, that);
        }

        /**
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
          return ReflectionUtils.toString(this);
        }

        /**
         * The number of comments.
         *
         * @return The number of comments.
         */
        public Long getCount() {
          return count;
        }

        /**
         * The comments.
         *
         * @return The comments.
         */
        public List<Attachment> getData() {
          return unmodifiableList(data);
        }
  }


Mark Allen

unread,
Feb 6, 2013, 5:10:09 PM2/6/13
to res...@googlegroups.com
Alan, thanks, what you've done looks correct to me.

I've opened https://github.com/revetkn/restfb/issues/39 so I can add this to RestFB and will credit you as a contributor.

Mark

S13_Alan

unread,
Feb 13, 2013, 9:37:53 AM2/13/13
to res...@googlegroups.com
Thanks very much Mark, I only realised after posting this that I should perhaps have put it on Github instead.

For anyone else reading this, it works fine, and I'm getting them stored ok, but unfortunately I couldn't really find a way to get a hold of the attachment data itself and had ran out of time (it was just an optional part I wanted to support as well as text).

Found various suggested work arounds online about it, but the gist seemed to be that FB hadn't yet provided a way to get the data back despite including the attachment id and type in the JSON. While I managed to make up a url to images for example, this only worked while logged in. The suggestions around alternate urls to hit, including IDs, tokens etc in the request, just did not work for me.

Will revisit the problem later on and hopefully it will be a bit clearer by then in the docs, or I will realise I've missed something obvious.

Alan.


S13_Alan

unread,
May 15, 2013, 7:39:43 AM5/15/13
to res...@googlegroups.com
Just wanted to add, it now looks possible to fetch the attachments as long as you have the access token.

This wasn't working before for me. Eg:

https://api.facebook.com/method/messaging.getattachment?access_token=TOKEN&mid=MESSAGE_ID&aid=ATTACHMENT)_ID&format=json
Reply all
Reply to author
Forward
0 new messages