File size: 1,698 Bytes
01d832f
 
 
 
7463179
01d832f
31012b0
 
01d832f
 
 
 
31012b0
 
 
7463179
31012b0
 
01d832f
31012b0
 
 
 
244099c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31012b0
7463179
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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