Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python/Tweepy从Twitter获取最新的直接消息_Python_Twitter_Tweepy - Fatal编程技术网

使用python/Tweepy从Twitter获取最新的直接消息

使用python/Tweepy从Twitter获取最新的直接消息,python,twitter,tweepy,Python,Twitter,Tweepy,我刚刚开始使用Tweepy,我正在尝试构建一个非常简单的机器人,它将使用Twitter来自动化我家中的一些事情(主要是为了好玩和学习Tweepy)。我浏览了Tweepy文档,在不知道消息ID的情况下,无法找到如何从我自己的帐户检索最新的直接消息 我假设我可以使用API.get\u direct\u messages()方法,但它需要消息ID(我不知道)。有人能告诉我做这件事的正确方法吗?我在用蟒蛇3 谢谢 你似乎混淆了两种不同的方法。(不带get_u)应该为您提供一个直接消息列表 (单数)从其I

我刚刚开始使用Tweepy,我正在尝试构建一个非常简单的机器人,它将使用Twitter来自动化我家中的一些事情(主要是为了好玩和学习Tweepy)。我浏览了Tweepy文档,在不知道消息ID的情况下,无法找到如何从我自己的帐户检索最新的直接消息

我假设我可以使用
API.get\u direct\u messages()
方法,但它需要消息ID(我不知道)。有人能告诉我做这件事的正确方法吗?我在用蟒蛇3


谢谢

你似乎混淆了两种不同的方法。(不带
get_u
)应该为您提供一个直接消息列表

(单数)从其ID返回单个直接消息。

来自Tweepy Docs~“API.list_direct_messages([count][,cursor])返回过去30天内所有直接消息事件(发送和接收)。按逆时间顺序排序。”

要获取最新的消息对象(已发送和已接收):

如果您特别想要最后收到的信息:

def get_last_received(my_dms):
    for dm in my_dms:
        if dm.message_create['target']['recipient_id'] == 'my_user_id':
            return dm  # We will return when we encounter the first received message object

get_last_received(my_dms)

我也有同样的问题。我使用的是最新的tweepy文档,无法看到direct_messages()方法,它似乎已被删除,但对于V3.5等较低版本,它是可用的。
my_dms[0]
def get_last_received(my_dms):
    for dm in my_dms:
        if dm.message_create['target']['recipient_id'] == 'my_user_id':
            return dm  # We will return when we encounter the first received message object

get_last_received(my_dms)