Get Tweet Id using Tweepy TwitterV2

Question:

I am trying to make a bot which makes a post and then self replies. The only issue I am having is that response.id is not fetching the tweet id. Could I get any pointers as to the right way to fetch the id of a tweet that was just posted?

# Post Tweet
response = client.create_tweet(text=tweet_msg)

# Respond to first tweet posted
client.create_tweet(text=reply_msg, in_reply_to_tweet_id=response.id)
Asked By: k3r0

||

Answers:

Was actually wondering how to do this myself and just figured it out. When you create a tweet and print it you should notice a response in your terminal. It should look something like this.

Response(data={‘edit_history_tweet_ids’: [‘ID_IS_HERE’], ‘id’: ‘ID_IS_HERE’, ‘text’: ‘TWEET_TEXT_IS_HERE’.

To access the id you’ll have to say have to say the following

# Post Tweet
response = client.create_tweet(text=tweet_msg)

# Respond to first tweet posted
client.create_tweet(text=reply_msg, in_reply_to_tweet_id=response.data['id'])

You’re accessing the ID of the tweet with the response.data[‘id’] part, hope this helps.

Answered By: codycodes
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.