Twitter API - Tweepy 图书馆 - send_direct_message

Twitter API - Tweepy Library - send_direct_message

我是 Tweepy (v3.7.0) 的新手,我正在尝试向我的一些关注者发送直接消息。但是,send_direct_message(user_id/screen_name,text) 函数似乎不起作用。我发现了一些旧线程,其中提到 Twitter 的旧直接消息端点已被弃用,这是导致问题的原因。关于如何使用新端点向我的关注者发送直接消息的任何建议?最新版本的 Tweepy 是否解决了这个问题?

我试过以下代码:

api.send_direct_message(follower.screen_name,"Hi @"+follower.screen_name)

正在检索关注者列表:

for follower in limit_handled(tweepy.Cursor(api.followers).items()):

错误信息:

Error: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]

您必须更改官方 tweepy api 代码的一些代码。

第一个变化是 this commit in api.py and the second change will be thisbinder.py

之后,您可以在代码中执行以下方法,并使用这种新方法发送消息。

event = {
  "event": {
    "type": "message_create",
    "message_create": {
      "target": {
        "recipient_id": '434259741'
      },
      "message_data": {
        "text": 'This is a new test'
      }
    }
  }
}

api.send_direct_message_new(event)

如果您想了解有关此问题的更多信息,here 是讨论此问题的 link。