You can download this code by clicking the button below.
This code is now available for download.
This function uses the Tweepy library to randomly select a status and post it to Twitter.
Technology Stack : Tweepy
Code Type : Function
Code Difficulty : Intermediate
import tweepy
import random
def tweet_random_status(api_key, api_secret_key, access_token, access_token_secret):
# Authenticate to Twitter
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Generate a random status
statuses = ["Hello Twitter!", "Just had a great day!", "What's up Twitter?", "Enjoying a cup of coffee.", "Feeling creative!"]
random_status = random.choice(statuses)
# Post the random status
api.update_status(random_status)
return random_status