Facebook API get new comments to posts

2.1k views Asked by At

I am using Facebook4j to access the Facebook API for a Page.

I can get a list of new posts for a Page, using

connection.getFeed()

and get the comments for a Post using:

post.getComments()

But I also want to be able to get new comments to the page posts (while ignoring comments that I've already fetched). Any idea of how to do this, other than searching through the comments of every post all over again?

2

There are 2 answers

4
Laurentiu L. On

There is no way of doing it with the current Facebook4j API.

If you look at the list of the unsupported features on their page you will see:

What you are looking for is real-time updates for the new comments.

You can take a look at this guide about subscribing to real-time updates.

If you'll look at the real-time updates link i provided above you will notice that real-time updates are limited to certain types of objects and a subset of their fields which are also listed there.

The valid types of objects for subscriptions available are the user and page Graph API object (with the feed field amongst others).

The real-time updates only indicate that a particular field has changed, they do not include the value of those fields. So this only makes apps more efficient, as they know exactly when a change has happened, and don't need to rely on continuous or even periodic Graph API requests when changes aren't happening.

You will know which field of the object (either user/page/permissions/payments) has changed, in your case the feed.

But other than that you will have to go through all the posts that you are interested in and the comments - though you could probably do it efficiently. For this you can probably keep your Facebook4j API code which gets the feed and recall it on upon updates. Or better yet upgrade the code to only track the changes you want and so on.

There is this example here (SO) on how to get facebook real time update in java. You'll get the point and probably build something better.

This answer suggests using Spring Social Facebook as it has a real-time update controller for handling real-time update callbacks from Facebook , whereas RestFB and Facebook4j can't do that.

0
Parker On

To answer the question, if Facebook4j supports this, no, it does not.

This isn't done easily with the official graph API either, as comments use cursor based pagination; so filters like since and until will not work (they will work on things like feed though. (Source)

In order to do what you want, you would need to get all comments from now until the time you last got them. You can have comments returned ordered (most recent first, to oldest last) by using ?filter=stream

You can execute a raw fb request via fb4j for this:

// GET
Map<String, String> params = new HashMap<String, String>();
params.put("filter", "stream"); //add the ?filter=stream

//res will be the RAW response
RawAPIResponse res = facebook.callGetAPI("POST_ID/comments");
//you can get it as a JSONObj with:
JSONObject jsonObject = actual.asJSONObject();

OR, to do it without a raw request, you can use comment.getCreatedTime() to get the time for each comment, and then filter the old ones out.

Either way, you'll have to filter them yourself, as neither facebook4j nor the graph API support this natively.