TheDz 78
TheDz 78

Reputation: 43

How do i reply to a certain tweet using Tweepy?

I am trying to reply to create a code to reply to simply reply to a (given) tweet, i only know that api.update_status tweets but not how to reply to one, i did some researches but all are outdated and "in_reply_to_status_id" doesn't seem to work anymore

api = tweepy.API(auth)

api.update_status

Upvotes: 4

Views: 1665

Answers (1)

David Scholz
David Scholz

Reputation: 9856

In order to reply to a tweet using the Twitter API v2, you need to use the in_reply_to_tweet_id. This works as follows.

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)

This is described in the documentation here.

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

Edit: If you are using API v1.1, then you need to include @username in the status text, where username is the author of the referenced Tweet. This is described here.

Hence, a working example looks as follows.

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

It is also possible to use auto_populate_reply_metadata=True as discussed in this Github issue.

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

This is also described in the documentation.

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.

Upvotes: 5

Related Questions