-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.py
78 lines (66 loc) · 2.26 KB
/
app2.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests as r
import json
import random
import tweepy
from dotenv import load_dotenv
import os
from datetime import datetime
timestamp = datetime.now()
formatted_timestamp = timestamp.strftime("%d-%m-%Y %H:%M:%S")
print(formatted_timestamp)
load_dotenv()
# Part 1 - OpenAI
# Set up your OpenAI API key
api_key = os.getenv("openai")
# Define a list of prompts to provide variety in the generated tweets
prompts = [
"Can you give me a motivational tweet that inspires people to overcome challenges?",
"Generate a motivational tweet about the importance of perseverance.",
"Create a motivational tweet encouraging people to follow their dreams.",
"Write a motivational tweet about the power of positive thinking.",
"Give me a motivational tweet about finding strength in adversity.",
"Provide a motivational tweet that emphasizes the value of hard work.",
"Compose a motivational tweet about the benefits of staying focused and determined."
]
# Select a random prompt from the list
selected_prompt = random.choice(prompts)
# Call the OpenAI API to generate a response using GPT-4
response = r.post(
"https://api.openai.com/v1/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
json={
"model": "gpt-4-turbo",
"messages": [
{
"role": "system",
"content": "You write spiritual motivational tweets"
},
{
"role": "user",
"content": selected_prompt
}
]
}
)
# Print the generated response
response = response.json()
tweet_final = response['choices'][0]['message']['content']
print(f'Tweet: {tweet_final}')
# Part 3 - Tweet It
consumer_key = os.getenv("consumer_key")
consumer_secret = os.getenv('consumer_secret')
access_token = os.getenv('access_token')
access_token_secret = os.getenv('access_token_secret')
client = tweepy.Client(
consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret
)
response = client.create_tweet(
text=tweet_final
)
print('Tweet sent, url:')
print(f"https://twitter.com/user/status/{response.data['id']}")
print('---------------------------')