Search code examples
javascriptmultithreadingfacebook-javascript-sdkmessageinbox

Getting Messages from Inbox


I have a question about the Graph API.

I use Javascript for the API and make a little test website ,where you can log in ,look for new messages and write a new status.

My problem is that I can't get the messages or the thread.

FB.api('/me/inbox',function(response) {  alert(response.id); } ); don't work.

Have somebody an example for getting the messages in the inbox??

Thanks


Solution

  • The /me/inbox request requires that you have the read_mailbox permission granted.

    Once you've got that, the /me/inbox request will return an array of Thread's, which will look something like this;

    {
      "data": [
        {
          "id": "1126884978255", 
          "from": {
            "name": "Someone's Name", 
            "id": "34723472"
          }, 
          "to": {
            "data": [
              {
                "name": "Someone's Name", 
                "id": "34723472"
              }, 
              {
                "name": "Matt Lunn", 
                "id": "560914724"
              }
            ]
          }, 
          "message": "Testing the one-ness.", 
          "updated_time": "2012-01-31T12:13:00+0000", 
          "unread": 0, 
          "unseen": 0, 
          "comments": {
            "data": [
              {
                "id": "1126884978255_6769", 
                "from": {
                  "name": "Someone's Name", 
                  "id": "34723472"
                }, 
                "message": "£140!?", 
                "created_time": "2012-01-31T11:33:15+0000"
              },
              {
                "id": "1126884978255_6771", 
                "from": {
                  "name": "Matt Lunn", 
                  "id": "560914724"
                }, 
                "message": "^^ month in advance as well", 
                "created_time": "2012-01-31T11:33:26+0000"
              }
            ]
          }, 
          "type": "thread"
        }
      ], 
      "summary": {
        "unseen_count": 0, 
        "unread_count": 21, 
        "updated_time": "2012-01-31T13:19:31+0000"
      }
    }
    

    So depending which ID you're after, you'll have to do;

    for (var i=0;i<response.data.length;i++) {
        var thread = response.data[i];
    
        for (var j=0;j<thread.comments.data.length;j++) {
            var comment = thread.comments.data[j];
    
            console.log(comment.message);
        }
    } 
    

    Hopefully you get the idea...