Maldus
Maldus

Reputation: 11807

Read facebook messages using python sdk

I'm trying to read facebook conversations of a page using a python script. With this code

import facebook

at = "page access token"
pid = "page id"
api = facebook.GraphAPI( at )
p = api.get_object( 'me/conversations')
print p

I get a dictionary containing the following

{'paging': {'next': 'https://graph.facebook.com/v2.5/1745249635693902/conversations?access_token=<my_access_token>&limit=25&until=1454344040&__paging_token=<my_access_token>', 'previous': 'https://graph.facebook.com/v2.5/1745249635693902/conversations?access_token=<my_access_token>&limit=25&since=1454344040&__paging_token=<my_access_token>'}, 'data': [{'link': '/Python-1745249635693902/manager/messages/?mercurythreadid=user%3A100000386799941&threadid=mid.1454344039847%3A2e3ac25e0302042916&folder=inbox', 'id': 't_mid.1454344039847:2e3ac25e0302042916', 'updated_time': '2016-02-01T16:27:20+0000'}]}

What are those fields? How can I get the text of the message?

Edit: I tried asking for the "messages" field by adding

    msg = api.get_object( p['data'][0]['id']+'/messages')
    print msg

but it just returns the same fields. I've searched in the API docs for a while, but I didn't find anything helpful. Is it even possible to read the message content of a facebook page's conversation using python?

Upvotes: 2

Views: 9863

Answers (1)

Maldus
Maldus

Reputation: 11807

I managed to find the answer myself; the question was not well posed and did not match what I was exactly looking for.

I wanted to get the content of the messages of facebook conversations of a page. Following the facebook graph API documentation, this can be achieved by asking for the conversations ({page-id}/conversations), then the messages in said conversations ({conversation-id}/messages, https://developers.facebook.com/docs/graph-api/reference/v2.5/conversation/messages), and finally asking for the message itself should return a dict with all the fields, content included (/{message-id}, https://developers.facebook.com/docs/graph-api/reference/v2.5/message).

At least this is how I believed it should have been; however the last request returned only the fields 'created_time' and 'id'.

What I was really trying to ask was a way to fetch the 'message' (content) field. I was assuming the function graph.get_object() from the official python facebook sdk should have returned all the fields in any case, since it has only one documented argument (http://facebook-sdk.readthedocs.org/en/latest/api.html) - the graph path for the requested object, and adding additional field request is not allowed.

The answer I was looking for was in this other question, Request fields in Python Facebook SDK. Apparently, it's possible to ask for specific fields ( that are not returned otherwise ) by passing an **args dict with such fields along with the path requested. In a GET request to the Facebook graph that would be the equivalent of adding

?fields=<requested fieds> 

to the object path.

This is the working code:

#!/usr/bin/env python

import facebook

at = <my access token>
pid = <my page id>
api = facebook.GraphAPI( at )
args = {'fields' : 'message'}  #requested fields
conv = api.get_object( 'me/conversations')
msg = api.get_object( conv['data'][0]['id']+'/messages')
for el in msg['data']:
    content = api.get_object( el['id'], **args)   #adding the field request
    print content

Upvotes: 6

Related Questions