Reputation: 3
I'm creating my first twitter bot and have been successful in creating the tweet. Now I'm trying to get the tweet's id to add to an xml file. I'm able to get an uploaded video's id but having issues getting the created tweet's id.
Snippet of the code (copied from a stackoverflow answer)
import tweepy
def get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret) -> tweepy.API:
"""Get twitter conn 1.1"""
auth = tweepy.OAuth1UserHandler(api_key, api_secret)
auth.set_access_token(
access_token,
access_token_secret,
)
return tweepy.API(auth)
def get_twitter_conn_v2(api_key, api_secret, access_token, access_token_secret) -> tweepy.Client:
"""Get twitter conn 2.0"""
client = tweepy.Client(
consumer_key = api_key,
consumer_secret = api_secret,
access_token = access_token,
access_token_secret = access_token_secret,
)
return client
client_v1 = get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret)
client_v2 = get_twitter_conn_v2(api_key, api_secret, access_token, access_token_secret)
media = client_v1.media_upload(media_path, chunked=True, media_category='tweet_video')
media_id = media.media_id
tweetText = 'created a tweet'
tweet = client_v2.create_tweet(text=tweetText)
removed the addition of the video to the tweet for faster testing.
I can't figure out how to get id of the tweet just created. The tweepy documentation is going over my head. Searched google, only to get results on how to get a tweet via id or get tweets and parse through them.
I tried
tweetID = tweet.id
and get the following error
AttributeError: 'Response' object has no attribute 'id'
when I add
print(tweet)
I see the following
Response(data={'edit_history_tweet_ids': ['1696308550870671480'], 'id': '1696308550870671480', 'text': .....)
It's there just don't know how to get it.
Any pointers would be appreciated.
Upvotes: 0
Views: 246
Reputation: 516
The structure of the response.json file is like this:
So, to get the Tweet ID, replace:
tweetID = tweet.id
with the following line of code:
tweetID = tweet.data['id']
# For integer data type
# tweetID = int(tweet.data['id'])
For more information:
I hope this helps.
Upvotes: 1