import tweepy import openai import time import os # Authentication for Twitter API (using environment variables for security) def authenticate_twitter(): auth = tweepy.OAuth1UserHandler( consumer_key=os.getenv('TWITTER_API_KEY'), consumer_secret=os.getenv('TWITTER_API_SECRET_KEY'), access_token=os.getenv('TWITTER_ACCESS_TOKEN'), access_token_secret=os.getenv('TWITTER_ACCESS_TOKEN_SECRET') ) api = tweepy.API(auth) return api # Authentication for OpenAI API def authenticate_openai(): openai.api_key = os.getenv('OPENAI_API_KEY') # Function to generate a tweet using GPT-3 def generate_tweet(): response = openai.Completion.create( engine="text-davinci-003", # Choose the model to use prompt="Create a tweet in the style of a humorous AI agent, keeping it casual and engaging.", max_tokens=50 ) tweet = response.choices[0].text.strip() return tweet # Function to send the tweet to Twitter def send_tweet(api, tweet): try: api.update_status(tweet) # Send the generated tweet print(f"Tweet sent: {tweet}") except tweepy.TweepError as e: print(f"Error: {e}") # Main function to control the tweet posting loop def main(): twitter_api = authenticate_twitter() # Get Twitter API instance authenticate_openai() # Authenticate OpenAI API while True: tweet = generate_tweet() # Generate a new tweet print(f"Generated Tweet: {tweet}") send_tweet(twitter_api, tweet) # Send the tweet to Twitter time.sleep(1800) # Wait for 30 minutes before posting the next tweet if __name__ == "__main__": main() # Start the program