使用python SDK阅读Facebook消息 [英] Read facebook messages using python sdk

查看:53
本文介绍了使用python SDK阅读Facebook消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python脚本读取页面的Facebook对话.有了这段代码

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?

我尝试通过添加来询问邮件"字段

I tried asking for the "messages" field by adding

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

,但它只返回相同的字段.我已经在API文档中搜索了一段时间,但没有发现任何帮助.甚至可以使用python阅读Facebook页面对话的消息内容吗?

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?

推荐答案

我设法自己找到了答案;这个问题提出的条件不正确,与我的要求完全不符.

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

我想获取页面Facebook对话消息的内容.遵循facebook graph API文档,这可以通过先询问对话({page-id}/conversations),然后询问所述对话中的消息({conversation-id}/messages,

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).

至少这是我认为应该的样子;但是最后一个请求仅返回字段"created_time"和"id".

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

我真正想问的是一种获取消息"(内容)字段的方法.我假设官方python facebook sdk中的函数graph.get_object()在任何情况下都应该返回所有字段,因为它只有一个文档化的参数(

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.

我正在寻找的答案是另一个问题,在Python Facebook中请求字段SDK . 显然,可以通过传递带有此类字段的** args dict以及所请求的路径来请求特定字段(否则不会返回). 在对Facebook图形的GET请求中,相当于添加

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> 

到对象路径.

这是工作代码:

#!/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

这篇关于使用python SDK阅读Facebook消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆