First upload media files, then attach them to your tweet:
# Upload media and get media IDsmedia_id_1 = await client.upload_media('image1.jpg')media_id_2 = await client.upload_media('video.mp4', wait_for_completion=True)# Create tweet with mediatweet = await client.create_tweet( text='Check out these photos!', media_ids=[media_id_1, media_id_2])
For videos and GIFs, set wait_for_completion=True to ensure the media is fully processed before posting.
import time# Schedule tweet for 1 hour from nowscheduled_time = int(time.time()) + 3600scheduled_id = await client.create_scheduled_tweet( scheduled_at=scheduled_time, text='This will be posted in an hour', media_ids=[media_id])
# Get all scheduled tweetsscheduled_tweets = await client.get_scheduled_tweets()for tweet in scheduled_tweets: print(f'{tweet.text} - scheduled for {tweet.execute_at}')# Delete a scheduled tweetawait client.delete_scheduled_tweet(scheduled_id)
# Add to bookmarksawait tweet.bookmark()# Remove from bookmarksawait tweet.delete_bookmark()# Get all bookmarksbookmarks = await client.get_bookmarks(count=20)for bookmark in bookmarks: print(bookmark.text)
The Tweet object provides access to various attributes:
tweet = await client.get_tweet_by_id('1234567890')# Basic informationprint(tweet.id) # Tweet IDprint(tweet.text) # Tweet textprint(tweet.created_at) # Creation timestampprint(tweet.lang) # Language code# User informationprint(tweet.user.screen_name) # Author's screen nameprint(tweet.user.name) # Author's display name# Engagement metricsprint(tweet.favorite_count) # Number of likesprint(tweet.retweet_count) # Number of retweetsprint(tweet.reply_count) # Number of repliesprint(tweet.view_count) # Number of viewsprint(tweet.bookmark_count) # Number of bookmarks# Media and contentprint(tweet.media) # List of media objectsprint(tweet.hashtags) # List of hashtagsprint(tweet.urls) # List of URLs# Relationshipsprint(tweet.in_reply_to) # ID of tweet being replied toprint(tweet.quote) # Quoted tweet objectprint(tweet.retweeted_tweet) # Retweeted tweet object# Status flagsprint(tweet.favorited) # Whether you've liked itprint(tweet.bookmarked) # Whether you've bookmarked it
tweet = await client.get_tweet_by_id('1234567890')# Get users who retweetedretweeters = await tweet.get_retweeters(count=40)for user in retweeters: print(user.screen_name)# Get users who likedfavoriters = await tweet.get_favoriters(count=40)for user in favoriters: print(user.screen_name)# Paginationmore_retweeters = await retweeters.next()
tweet = await client.get_tweet_by_id('1234567890')# Get direct repliesif tweet.replies: for reply in tweet.replies: print(f'{reply.user.screen_name}: {reply.text}') # Load more replies more_replies = await tweet.replies.next()# Get the tweet thread (tweets this is replying to)if tweet.reply_to: for parent in tweet.reply_to: print(f'Replying to: {parent.text}')
The replies attribute contains top-level replies. Each reply may also have nested replies accessible through its own replies attribute.
media_id = await client.upload_media('image.jpg')# Add alt text for accessibilityawait client.create_media_metadata( media_id, alt_text='A beautiful sunset over the ocean', sensitive_warning=['other'])await client.create_tweet(media_ids=[media_id])