【问题标题】:Get latest direct message from Twitter using python/Tweepy使用 python/Tweepy 从 Twitter 获取最新的直接消息
【发布时间】:2021-02-15 17:19:55
【问题描述】:

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

我假设我可以使用 API.get_direct_messages() 方法,但它需要一个消息 ID(我不知道)。谁能告诉我正确的方法来做到这一点?我正在使用 Python3

谢谢!

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    您似乎混淆了两种不同的方法。 direct_messages() method(没有get_)应该给你一个直接消息列表。

    get_direct_message()(单数)从其 ID 返回一条直接消息。

    【讨论】:

    • 有同样的问题。我使用的是最新的 tweepy 文档,看不到 direct_messages() 方法,好像它已被删除,但对于 V3.5 等较低版本,它是可用的。
    【解决方案2】:

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

    my_dms = api.list_direct_messages()
    

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

    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)
    

    【讨论】:

      猜你喜欢
      • 2019-02-21
      • 2021-07-25
      • 2020-12-17
      • 2017-05-22
      • 1970-01-01
      • 2016-04-05
      • 2020-05-01
      • 2012-08-27
      • 1970-01-01
      相关资源
      最近更新 更多