我如何使用 Tweepy 回复某条推文?

How do i reply to a certain tweet using Tweepy?

我正在尝试回复以创建一个代码来回复简单地回复(给定的)推文,我只知道 api.update_status 推文但不知道如何回复推文,我做了一些研究但所有已过时,“in_reply_to_status_id”似乎不再有效

api = tweepy.API(auth)

api.update_status

要使用 Twitter API v2 回复推文,您需要使用 in_reply_to_tweet_id。其工作方式如下。

import tweepy

client = tweepy.Client(consumer_key='YOUR_CONSUMER_KEY',
                       consumer_secret='YOUR_CONSUMER_SECRET',
                       access_token='YOUR_ACCESS_TOKEN',
                       access_token_secret='YOUR_ACCESS_TOKEN_SECRET')

client.create_tweet(text='Some reply', in_reply_to_tweet_id=42)

这在文档 here 中有描述。

in_reply_to_tweet_id (Optional[Union[int, str]]) – Tweet ID of the Tweet being replied to.

编辑: 如果您使用的是API v1.1,那么您需要在状态文本中包含@username,其中用户名是引用推文的作者。这是描述 here.

因此,一个工作示例如下所示。

comment = '@usernameOfTweet Test Reply'
res = api.update_status(comment, in_reply_to_status_id=42)

也可以使用 auto_populate_reply_metadata=True,如 this Github issue 中所述。

comment = 'Test reply'
api.update_status(comment, in_reply_to_status_id=42, auto_populate_reply_metadata=True)

文档中也有描述。

auto_populate_reply_metadata – If set to true and used with in_reply_to_status_id, leading @mentions will be looked up from the original Tweet, and added to the new Tweet from there.