2

I'm trying to figure out how to, or if it is allowed or possible to send messages using the Facebook API.

I am using the Facebook graph API from Facebook4j.

It seems that I can get a user's messages from getInbox(), but I can't see anyway to create a message?

I really only want to respond to Page messages using a Page access token, user messages are not required, but would be nice too.

Anyone know how, or if it is possible?

James
  • 17,965
  • 11
  • 91
  • 146

2 Answers2

3

Those links will tell you how to reply to messages with a Page Token:

In general, just do a POST request to this endpoint: /{conversation-id}/messages

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • Any idea when Facebook4j will support this? – James Aug 17 '15 at 17:52
  • well, it´s a simple API call. i am sure you can do simple API calls with Facebook4js, else it would not be useful anyway ;) – andyrandy Aug 17 '15 at 18:14
  • i´ve never used Facebook4j, but it took me 1 minute to find example code with a raw POST request: http://facebook4j.org/en/code-examples.html – andyrandy Aug 17 '15 at 18:14
  • Thanks this works, ugly because you need to walk the JSON objects yourself, but okay for now. Hopefully Facebook4j will support this soon. – James Aug 25 '15 at 14:49
1

The code I ended up with is,

to get all messages,

RawAPIResponse res = getConnection().callGetAPI("/" + connection.getPage().getId() + "/conversations");
JSONObject result = res.asJSONObject();
JSONArray conversations = result.getJSONArray("data");

and to send a message,

Map<String, String> params = new HashMap<String, String>();
params.put("message", text);
connection.callPostAPI("/" + conversationId + "/messages", params);

It works, but a little ugly. Hopefully Facebook4J will support this API soon.

James
  • 17,965
  • 11
  • 91
  • 146