Moshe
Moshe

Reputation: 228

Facebook chat using graph API

I'm using graph API in order to use Facebook chat. I'm using the http://restfb.com/ framework. The issue is that with the following code I can read all conversations and messages.

public void initClient()
    {

        m_facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN, MY_APP_SECRET);
    }
public void readPage()
    {
        Page page = m_facebookClient.fetchObject("352179081632935", Page.class);
        System.out.println("Page likes = " + page.getLikes());


        Connection<Conversation> conversations = m_facebookClient.fetchConnection("me/conversations", Conversation.class);

    for(List<Conversation> conversationPage : conversations) {
        for(Conversation conversation : conversationPage) {
            System.out.println(conversation);
            System.out.println(conversation.getUnreadCount());

            Message lastMessage = null;
            for (Message message : conversation.getMessages())
            {
                System.out.println("Message text = " + message.getMessage());
                System.out.println("Message unread = " + message.getUnread());
                System.out.println("Message from = " + message.getFrom().getName());
                System.out.println("Message to = " + message.getTo().get(0).getName());
                System.out.println("Message unseen = " + message.getUnseen());
                lastMessage = message;
            }
        }    
    }

I would like to know how to send reply messages or new messages using this framework or Graph API?

Thank you, Moshe

Upvotes: 2

Views: 4265

Answers (2)

andyrandy
andyrandy

Reputation: 73984

First of all, there is no me/conversations endpoint for users. Conversations are only available for Pages so it would be /{page-id}/conversations: https://developers.facebook.com/docs/graph-api/reference/v2.2/page/conversations

The Facebook docs explain in detail how it works, including some example code. I suggest using one of the official SDKs instead (JS SDK, PHP SDK, ...).

The Chat API (which is what you would want to use) is deprecated: https://developers.facebook.com/docs/chat/

Meaning, it is not possible anymore to use the Facebook chat in your App.

Upvotes: 1

Moshe
Moshe

Reputation: 228

I found the way yo solve it by replying based on the conversation id.

For example:

m_facebookClient.publish("t_mid.1420120490471:36257b35667389d257/messages", FacebookType.class, Parameter.with("message", "RestFB test")); 

t_mid.1420120490471:36257b35667389d257 - is the conversation id

Upvotes: 0

Related Questions